在DetailView模板上,我无法打印姓名,仅发布日期' |'正在打印标志,但在ListView上它工作正常。这是用文件写的代码。谢谢!
blog / urls.py
urlpatterns = patterns('',url(r'^$', ListView.as_view(queryset =
Blog.objects.all(), template_name ='blog.html')),
url(r'^(?P<pk>\d+)/$', DetailView.as_view(model=Blog, template_name='detail.html')), )
blog.html
{% extends 'base.html' %}
{% block content %}
{% for post in object_list %}
<ul> <a href="/blog/{{ post.id }}">{{ post.name }} </a>||{{ post.date }}</ul>
{% endfor %}
{% endblock %}
detial.html
{% extends 'base.html' %}
{% block content %} <h2> <a href="/blog/{{ post.id }}">{{ post.name }}
</a>||{{ post.date }}</h2> {% endblock %}
答案 0 :(得分:2)
context_object_name指定要在上下文中使用的变量的名称。默认为&#34;对象&#34;
尝试
{% extends 'base.html' %}
{% block content %} <h2> <a href="/blog/{{ object.id }}">{{ object.name }}
</a>||{{ object.date }}</h2> {% endblock %}