代码部分:
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 能显示。我的代码如何修改???