在Django 1.7中,我正在尝试创建一个用户面板,其中将显示userprofile信息(如头像,国家等)及其主题。以下是观点:
def topic(request, topic_id):
"""Listing of posts in a thread."""
posts = Post.objects.select_related('creator') \
.filter(topic=topic_id).order_by("created")
posts = mk_paginator(request, posts, DJANGO_SIMPLE_FORUM_REPLIES_PER_PAGE)
topic = Topic.objects.get(pk=topic_id)
topic.visits += 1
topic.save()
return render_to_response("myforum/topic.html", add_csrf(request, posts=posts, pk=topic_id,
topic=topic), context_instance=RequestContext(request))
主题模型是:
class Topic(models.Model):
title = models.CharField(max_length=100)
description = models.TextField(max_length=10000, null=True)
forum = models.ForeignKey(Forum)
created = models.DateTimeField()
creator = models.ForeignKey(User, blank=True, null=True)
visits = models.IntegerField(default = 0)
UserProfile模型:
class UserProfile(models.Model):
username = models.OneToOneField(User)
name = models.CharField(max_length=30, blank=True)
city = models.CharField(max_length=30, blank=True)
country = models.CharField(
max_length=20, choices= COUTNRY_CHOICES, blank=True)
avatar = ImageWithThumbsField(), upload_to='images', sizes=((32,32),(150,150),(200,200)), blank=True)
created_at = models.DateTimeField(auto_now_add=True, blank=True)
updated_at = models.DateTimeField(auto_now=True, blank=True)
但是,在topic.html中,我尝试在模板中捕获userprofile信息,例如
{{topic.creator.userprofile.name}}
或
{{topic.creator.userprofile.city}}
没有显示任何内容。这种情况发生在我尝试从UserProfile模型获取的所有字段中,尽管在数据库中用户的userprofile行不为空,我可以在其他视图中获取字段。
我已经坚持了好几天,所以非常感谢你的帮助。
更新:这是add_csrf,以防它可能是相关的
def add_csrf(request, ** kwargs):
d = dict(user=request.user, ** kwargs)
d.update(csrf(request))
return d