2013-05-18 78 views
0

我創建的博客,文章只發布login.so有四個表,如用戶,JSdetails,博客,評論。這四張表格是相互關聯的。這裏我想在評論中顯示個人資料圖片。我曾試過,但無法顯示。給我一些想法。我加了模型,視圖模板下面...從關係表Django濾波器查詢

models.py

class User(models.Model): 
    first_name=models.CharField() 
    last_name=models.CharField() 
    username=models.CharField() 

class JSdetails(models.Model): 
    user=models.ForeignKey(User) 
    profilepic=models.ImageField(upload_to='Image') 

class Blog(models.Model): 
    user=models.ForeignKey(User) 
    btitle=models.CharField() 
    bcontent=models.TextField(blank=True) 
    bposted=models.DateTimeField(auto_now = True) 

class BComments(models.Model): 
    user=models.ForeignKey(User) 
    blog=models.ForeignKey(Blog) 
    comments=models.TextField() 
    commentpost=models.DateTimeField(auto_now = True) 

views.py

def blogArticle(request): 
    articleid=1 
    article=Blog.objects.filter(id=articleid) 
    com=BComments.objects.filter(blog_id=articleid) 
    return render_to_response('registration/BlogArticle.html',{'article':article,'com':com},context_instance=RequestContext(request)) 

模板

/* Here Article will display */  

{% for article in article %} 
<h2>{{article.btitle}}</h2> 
<p style="text-align:justify;">{{article.bcontent}}</p> 
{% endfor %} 

/* Here Comments get displayed which is posted by user */ 

{% for com in com %} 
<img style="width:50px;height:50px;" src="Here I need to Display Profile picture" > 
<span>{{com.user.username}}</span> 
<p style="word-wrap:break-word;"> 
{{com.comments}} 
</p> 
{% endfor %} 
+0

你不使用JSdetails任何地方 – rjv

回答

1

由於您爲用戶設置了ForeignKey,用戶模型將有jsdetails_set這是一個RelatedManager。你可以嘗試在你的模板是:

{{ com.user.jsdetails_set.all.0.profilepic.url }} 

這將嘗試返回第一個jsdetails實例。如果沒有設置,當試圖訪問0索引時,不知道它是否默默地失敗。

+0

相同的結果尚未顯示 – user