-1
全部。如何通過Listview獲取用戶的照片列表django
我想使用Listview僅顯示用戶的照片列表而不是其他用戶的照片列表。但它不起作用。用戶可以獲取其他用戶的照片。
這是views.py
class PhotoListView(ListView):
model = Photo
def get(self, *args, **kwargs):
#if user connect to other user's page, go to the user's page.
if kwargs['username'] != self.request.user.username:
return redirect('index')
#to get only user's photo list.
username = self.request.user.username
return super(PhotoListView, self).get(username=username, *args, **kwargs)
這是model.py
class Photo(models.Model):
user = models.ForeignKey(User)
image_file = ProcessedImageField(upload_to='%Y/%m/%d/orig', format='JPEG', options={'quality': 100})
source = TaggableManager(through=Taggedsource, related_name='source')
subject = TaggableManager(through=Taggedsubject, related_name='subject')
description = models.TextField(max_length=500, blank=True)
comments = models.PositiveSmallIntegerField(default=0, null=True)
posted_on = models.DateTimeField(default=datetime.now)