2015-06-27 34 views
0

我想要製作一個簡單的django應用程序,它將在主頁面上顯示論壇列表,以及當用戶點擊論壇標題時,屬於該論壇顯示引用另一個模型的對象列表

下面是樁模型代碼:

class Post(models.Model): 
author = models.ForeignKey('auth.User') 
title = models.CharField(max_length=200) 
text = models.TextField() 
created_date = models.DateTimeField(
    default=timezone.now) 
published_date = models.DateTimeField(
    blank=True, null=True) 
forum = models.ForeignKey('Forum') # referinta la Forum 
upload = models.FileField("Upload a file", upload_to = 'media', null=True, blank=True) 

def publish(self): 
    self.published_date = timezone.now() 
    self.save() 
def __str__(self): 
    return self.title 

而對於論壇模型的代碼:

class Forum(models.Model): 
    title = models.CharField(max_length=200) 
    published_date = models.DateTimeField(blank=True, null=True) 

    def __str__(self): 
     return self.title 

在views.py代碼:

def forum_list(request): 
forums= Forum.objects.filter(published_date__lte=timezone.now()).order_by('published_date') 
return render(request, 'students_platform/post_list.html', {'forums': forums}) 

def post_list(request): 
    posts=Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date') 
return render(request, 'students_platform/post_list.html', {'posts': posts}) 

最後,我有一個post_list.html文件看起來像這樣:

<html> 
<head> 
    <title>Informatii despre examene</title> 
</head> 
<body> 
    <div> 
     <h1> Informatii despre examene</h1> 

    <h2> Bine ati venit! </h2> 
    </div> 
{% for forum in forums %} 
<div> 


    <h1><a href="">{{ forum.title }}</a></h1> 
    <p>{{ forum.text|linebreaks }}</p> 

</div> 
{% endfor %} 

</body> 
</html> 

如何編輯HTML文件,這樣每次我點擊論壇標題帶我去已添加到該論壇的帖子?

+0

在模板中,你需要添加網址在href您的論壇= 「」 看到這樣的:HTTP:/ /stackoverflow.com/questions/1777612/url-template-tag-in-django-template –

+0

@ joelgoldstick我認爲她的意思是網址管理 – madzohan

回答

相關問題