Ove*_*eat 3 django django-templates django-cms
我想制作博客,里面有类别和帖子。应该显示类别,当您单击它时,您将被重定向到显示此类别文章的另一个站点。
模型.py:
class Category(CMSPlugin):
title = models.CharField(max_length=20, default='category')
def __unicode__(self):
return self.title
class Blog_post(CMSPlugin):
category = models.ForeignKey(Category)
style = models.ForeignKey(Blog_style)
title = models.CharField(max_length=200, default='title')
description = models.CharField(max_length=200,default='description')
image = models.ImageField(upload_to='static', null=True, blank=True)
text = models.TextField()
created_date = models.DateTimeField(default=timezone.now)
published_date = models.DateTimeField(blank=True, null=True)
def publish(self):
self.published_date = timezone.now()
self.save()
def __unicode__(self):
return self.title
Run Code Online (Sandbox Code Playgroud)
视图.py
def Blog_list(request):
posts = Blog_post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
category = Category.objects.all()
return render(request, 'blogspot.html', {'posts': posts, 'category':category})
def post_detail(request, pk):
post = get_object_or_404(Blog_post, pk=pk)
return render(request, 'post_detail.html', {'post': post})
def category_detail(request, pk):
cat = get_object_or_404(Category, id=pk)
post_with_category = Blog_post.objects.filter(category=cat)
return render(request, 'articles.html', {'post_with_category': post_with_category})
Run Code Online (Sandbox Code Playgroud)
blogspot.html
{% for post in posts %}
<h1><a href="{% url 'post_detail' pk=post.pk %}">{{post.title}}</a></h1>
<a href="{% url 'category_detail' pk=post.category.id %}" >{{ post.category }}</a>
{{post.title}}
{{ post.description }}
{{ post.image }}
{{ post.text }}{{ post.published_date }}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
到目前为止一切正常。我可以点击 {{post.title}} 并重定向到 post_detail。现在我想对类别进行相同的逻辑。当我点击 {{post.category}} 时,我想重定向到articles.html,您可以在其中查看特定类别中的所有文章。
编辑:
我插入了代码来显示类别中的帖子。我坚持使用 for 循环。如果我使用帖子中提到的循环,我会得到所有帖子和类别。问题是,如果我在一个类别中有 2 个帖子,并且此循环将在模板中显示 2 个“类别”。
所以我编辑了我的 for 循环。
{% for post in category %}
{{post.title}}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
如果我<a href="{% url 'category_detail' pk=post.category.id %}" >{{post.title}}在这个循环中插入 ,我不会得到反向匹配。我试图修改 views.py category_detail
并且 url 应该看起来像localhost/<category>/
另一个问题是,什么是 QRM 替代命令"select*from Table Where Column_id= id ;
网址.py
url(r'^blog/$', views.Blog_list, name='Blog_list'),
url(r'^blog/(?P<pk>\d+)/$', views.post_detail, name='post_detail'),
Run Code Online (Sandbox Code Playgroud)
如果我理解你的问题,django 允许你通过主对象引用 FK 对象。因此,由于您post.category是Category模型的一个实例,您应该能够使用post.category.id它进行反向查找,因此您的模板将具有以下内容:
<a href="{% url 'category_detail' pk=post.category.id %}" >{{ post.category }}</a>
Run Code Online (Sandbox Code Playgroud)
然后,在您category_detail看来,您只需使用pk来进行查找:
cat = get_object_or_404(Category, id=pk)
post_with_category = Blog_post.objects.filter(category=cat)
Run Code Online (Sandbox Code Playgroud)
然后,您可以通过新post_with_category对象列表显示具有相同类别的新 url 链接上的帖子列表。
编辑:
您的 url 需要包含以下内容,以使上述 html href 标签起作用:
url(r'^cat/(?P<pk>[0-9]+)/$', views.category_detail, name='category_detail'),
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6658 次 |
| 最近记录: |