我正在 Django 中制作一个简单的博客应用程序作为学习练习。我可以添加帖子,在首页上看到它们,所以到目前为止它正在工作。我遇到的问题是创建一个在单独的页面上显示整个帖子的视图。我想单击标题,然后转到具有标题和正文的 url /post/primary 键的页面。当我点击链接时,我得到一个只有 base.html 的页面。不知道我在哪里遗漏了任何东西,这里是 urls.py、views.py 和 post.html:
来自 django.conf.urls 的 urls.py 导入模式、包含、url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('blog.views',
url(r'^$', 'frontpage'),
url(r'^post/(\d+)/$', 'post'),
)
urlpatterns += patterns('',
url(r'^admin/', include(admin.site.urls)),
)
from django.core.paginator import Paginator, InvalidPage, EmptyPage
from django.core.urlresolvers import reverse
from django.shortcuts import get_object_or_404, render_to_response
from blog.models import *
视图.py
def frontpage(request):
posts = Post.objects.all().order_by("-created")
paginator = Paginator(posts, 5)
page = request.GET.get('page', '1')
try:
posts = paginator.page(page)
except (InvalidPage, EmptyPage):
posts = paginator.page(paginator.num_pages)
return render_to_response("list.html", dict(posts=posts, user=request.user))
def post(request, pk):
"""Single Post"""
post = Post.objects.get(pk = pk)
d = dict(post=post, user=request.user)
return render_to_response("post.html", d)
post.html
{% extends "base.html" %}
{% block content %}
<div class ="main">
<ul>
{% for post in posts.object_list %}
<div class = "title">{{ post.title }}</div>
<ul>
<div class="time"> {{ post.created }}</div>
<div class ="body"> {{ post.body|linebreaks }}</div>
</ul>
{% endfor %}
</ul>
</div>
{% endblock %}
在此先感谢您的帮助。