我在Django中製作一個簡單的博客應用程序作爲學習練習。我可以添加帖子,將它們全部放在首頁上,所以目前運作良好。我遇到問題的地方是創建一個視圖,將整個帖子顯示在單獨的頁面上。我想單擊標題,然後轉到具有標題和正文的url/post /主鍵的頁面。當我點擊鏈接時,我會看到一個只有base.html的頁面。不知道在那裏我缺少什麼,這裏有urls.py,views.py,並post.html:將視圖鏈接到鏈接Django
urls.py 從django.conf.urls導入模式,包括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 *
views.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 %}
感謝您的幫助。
你的'base.html'是什麼樣的? – voithos