我遵循django关于如何创建博客的教程,我们在模板标签处,问题是,只有头部出现,而不是我放入模板中的文章,这是我的代码:
视图
from django.shortcuts import render
from.models import Narticle
def narticle_list(request):
narticle= Narticle.objects.all().order_by('date')
return render(request,'narticle/narticle_list', {'narticle': narticle})
模板条目列表
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Narticle</title>
</head>
<body>
<h1>narticle_list</h1>
<div class="narticle">
<h2> <a href="#">{{Narticle.title}}</a> </h2>
<p>{{Narticle.body}}</p>
<p>{{Narticle.date}}</p>
</div>
</body>
</html>
如果你想看到我的网址
from django.conf.urls import url, include
from django.contrib import admin
from. import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^narticle/', include ('narticle.urls')),
url(r'^about/$', views.about),
url(r'^$',views.homepage),
narticle的url
from django.conf.urls import url
from. import views
urlpatterns = [
url(r'^$',views.narticle_list),
]
当我请求narticle url时,我的文章没有显示,只是标题“narticle\u list”
您正在将集合传递到模板的上下文中。此集合的行为类似于python列表,因此需要对其进行迭代。可以使用模板逻辑执行此操作:
要澄清的是,集合是您从
Narticle.objects.all().order_by('date')
获得的。在{% for a in narticle %}
行中引用模板上下文中的narticle
。一定要用{% endfor %}
关闭循环。您可以访问属性或属性,就像您在示例中使用点表示法所做的那样。其他一切看起来都应该管用。你知道吗相关问题 更多 >
编程相关推荐