我正在尝试创建一个简单的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 :(得分:0)
添加到您的PostModelAdmin list_filter = ('forum',)
https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_filter
<a href="{% url 'admin:yourappname_post_changelist' %}?forum__id__exact={{ forum.id }}">{{ forum.title }}</a>
https://docs.djangoproject.com/en/dev/ref/contrib/admin/#reversing-admin-urls