代码部分:
models.py:
from django.db import models
class Articles(models.Model):
headline = models.CharField('标题',max_length=256)
content = models.TextField('内容')
pub_date = models.DateField('发表时间',auto_now_add=True,editable=True)
update_time = models.DateTimeField('更新时间',auto_now=True,null=True)
def __str__(self):
return self.headline
views.py:
from .models import Articles
from django.views.generic.detail import DetailView
class HomePageView(DetailView):
template_name = 'home.html'
model = Articles
context_object_name = 'articles'
pk_url_kwarg = 'id'
def get_object(self, queryset=None):
if queryset is None:
queryset = self.get_queryset()
id = self.kwargs.get(self.pk_url_kwarg, None)
if id is not None:
queryset = queryset.get(id=id)
return queryset
for i in queryset:
print(i.headline)
urls.py:
from django.contrib import admin
from django.views.generic.base import RedirectView
from view_app.views import HomePageView
urlpatterns = [
url(r'^articles/(?P<id>\d+)/', HomePageView.as_view(), name="home"),
url(r'^$', HomePageView.as_view(), name='home'),
url(r'^favicon.ico$',RedirectView.as_view(url=r'static/favicon.ico')),
url(r'^admin/', admin.site.urls),
]
模板 home.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>homepage</title>
</head>
<body>
{% for i in queryset %}
{{i.headline}}}
{% endfor %}
</body>
</html>
运行后网页一片空白,也不报错。
但是
views.py 那段 print 能显示。我的代码如何修改???
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
https://www.v2ex.com/t/530716
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.