我有这样的模特
class Blog(models.Model):
name = models.CharField(max_length=100)
tagline = models.TextField()
def __unicode__(self):
return self.name
class Entry(models.Model):
blog = models.ForeignKey(Blog)
headline = models.CharField(max_length=255)
Run Code Online (Sandbox Code Playgroud)
我想列出页面中的所有博客.我写了一个这样的观点
def listAllBlogs(request):
blogs= Blog.objects.all()
return object_list(
request,
blogs,
template_object_name = "blog",
allow_empty = True,
)
Run Code Online (Sandbox Code Playgroud)
并且能在视野中显示博客的标语
{% extends "base.html" %}
{% block title %}{% endblock %}
{% block extrahead %}
{% endblock %}
{% block content %}
{% for blog in blog_list %}
{{ blog.tagline }}
{% endfor %}
{% endblock %}
Run Code Online (Sandbox Code Playgroud)
但我想表明,这样的事情,blog__entry__name但我不知道我怎么能在模板中实现这一点.此外,博客中可能没有条目.如何在模板中检测?
谢谢
man*_*nji 28
要访问博客条目(相关经理):blog.entry_set.all
要在博客没有条目时执行其他操作,您将拥有在该集为空时执行的{%empty%}标记.
{% block content %}
{% for blog in blog_list %}
{{ blog.tagline }}
{% for entry in blog.entry_set.all %}
{{entry.name}}
{% empty %}
<!-- no entries -->
{% endfor %}
{% endfor %}
{% endblock %}
Run Code Online (Sandbox Code Playgroud)
根据您的代码,您可以执行以下操作.
{% block content %}
{% for blog in blog_list %}
{{ blog.tagline }}
{% for entry in blog.entry_set.all %}
{{entry.name}}
{% endfor %}
{% endfor %}
{% endblock %}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9397 次 |
| 最近记录: |