仅显示创建者的文件 - Django/Python
Display only creator's files - Django/Python
我想只显示创作者(用户)上传的图片到他们的个人资料中。
我该如何修改我的代码来显示它?
谢谢!
models.py:
class Photo(models.Model):
creator = models.ForeignKey(MyUser, null=False, blank=False)
category = models.ForeignKey("Category", default=1, null=True, blank=True)
title = models.CharField(max_length=30, null=True, blank=True)
description = models.TextField(max_length=120, null=True, blank=True)
image = models.ImageField(upload_to='user/photos/', null=True, blank=True)
slug = models.SlugField(null=False, blank=False)
active = models.BooleanField(default=True)
timestamp = models.DateTimeField(auto_now_add=True, auto_now=False, null=True)
updated = models.DateTimeField(auto_now_add=False, auto_now=True, null=True)
class Meta:
unique_together = ('slug', 'category')
ordering = ['-timestamp']
def __unicode__(self):
return "%s" %(self.creator)
def get_image_url(self):
return "%s/%s" %(settings.MEDIA_URL, self.image)
def get_absolute_url(self):
return "%s/%s" %(self.creator, self.slug)
views.py:
@login_required
def account_home(request, username):
try:
u = MyUser.objects.get(username=username)
except:
u = None
photo_set = Photo.objects.all()
context = {
"photo_set": photo_set,
"notifications": notifications,
"transactions": transactions
}
return render(request, "accounts/account_home.html", context)
.html:
{% for photo in photo_set %}
<a href="{{ photo.get_absolute_url }}"><img src="{{ photo.get_image_url }}" class='img-responsive'></a>
<hr/>
{% endfor %}
你有一个用户的外键,所以你可以通过它来过滤照片:
photo_set = Photo.objects.filter(creator=u)
或者更好地使用反向关系:
photo_set = u.photo_set.all()
此外,永远不要在代码中使用空白的 except
语句。您期望 get
引发的唯一异常是 MyUser.DoesNotExist
,因此您应该只捕获它。
我想只显示创作者(用户)上传的图片到他们的个人资料中。
我该如何修改我的代码来显示它?
谢谢!
models.py:
class Photo(models.Model):
creator = models.ForeignKey(MyUser, null=False, blank=False)
category = models.ForeignKey("Category", default=1, null=True, blank=True)
title = models.CharField(max_length=30, null=True, blank=True)
description = models.TextField(max_length=120, null=True, blank=True)
image = models.ImageField(upload_to='user/photos/', null=True, blank=True)
slug = models.SlugField(null=False, blank=False)
active = models.BooleanField(default=True)
timestamp = models.DateTimeField(auto_now_add=True, auto_now=False, null=True)
updated = models.DateTimeField(auto_now_add=False, auto_now=True, null=True)
class Meta:
unique_together = ('slug', 'category')
ordering = ['-timestamp']
def __unicode__(self):
return "%s" %(self.creator)
def get_image_url(self):
return "%s/%s" %(settings.MEDIA_URL, self.image)
def get_absolute_url(self):
return "%s/%s" %(self.creator, self.slug)
views.py:
@login_required
def account_home(request, username):
try:
u = MyUser.objects.get(username=username)
except:
u = None
photo_set = Photo.objects.all()
context = {
"photo_set": photo_set,
"notifications": notifications,
"transactions": transactions
}
return render(request, "accounts/account_home.html", context)
.html:
{% for photo in photo_set %}
<a href="{{ photo.get_absolute_url }}"><img src="{{ photo.get_image_url }}" class='img-responsive'></a>
<hr/>
{% endfor %}
你有一个用户的外键,所以你可以通过它来过滤照片:
photo_set = Photo.objects.filter(creator=u)
或者更好地使用反向关系:
photo_set = u.photo_set.all()
此外,永远不要在代码中使用空白的 except
语句。您期望 get
引发的唯一异常是 MyUser.DoesNotExist
,因此您应该只捕获它。