我有两个模型,分别是 Post 和 Profile。我保留博客数据,这些数据是 Post 中的标题、正文、所有者、slug 等。并保留用户个人资料设置,即个人资料中的口号、电子邮件、网站等
在我的 index.html 页面中,我在同一页面中显示用户个人资料信息和发布列表。所以 ; 我需要将这两个模型相互连接。当有人访问 127.0.0.1/blog/username(无论是否登录)时,属于名为“用户名”的用户的所有数据都必须存在。
这是我的models.py:
class Profile(models.Model):
slogan = models.TextField(blank=True)
twitter = models.CharField(max_length = 100,blank=True)
web_site = models.CharField(max_length=100,blank=True)
email = models.CharField(max_length = 100,blank=True)
def __unicode__(self):
return self.slogan
class Post(models.Model):
owner = models.ForeignKey(User)
title = models.CharField(max_length = 100)
body = models.TextField()
bodyPreview = models.TextField() #preview için body alanı
titlePreview = models.CharField(max_length=100) # preview için title alanı
slug = AutoSlugField(populate_from='title' ,unique=True)
posted = models.DateField(auto_now_add=True)
isdraft = models.BooleanField(default=False)
def __unicode__(self):
return self.title
@permalink
def get_absolute_url(self):
return ('view_blog_post',None,{'postslug':self.slug})
和我的索引视图:
def index(request,username):
post_list = Post.objects.filter(owner__username=username).filter(isdraft=False).order_by("-posted")
p_index = Paginator(post_list,3) #anasayfa için pagination.[her sayfada 3 post]
page = request.GET.get('page')
try:
indexPag = p_index.page(page)
except PageNotAnInteger:
indexPag = p_index.page(1)
except EmptyPage:
indexPag = p_index.page(p_index.num_pages)
## i need to get user's profile datas here. ??
return render_to_response('index.html',
{'post_list':post_list,'p_index':indexPag,'profile':query},
context_instance = RequestContext(request))