0

我正在使用本教程http://lightbird.net/dbe/blog.html创建一个简单的博客。我是 django 新手,按照本教程中的一些步骤操作时遇到问题。我尽力去理解,有时我就是不明白。我尝试获取源代码,但文件已损坏。

我需要解释的步骤是:

发布页面

对于每个帖子,我们还需要一个单独的页面,其中包含访问者的评论和完整的帖子文本(如果我们稍后决定限制在首页上显示帖子正文)。这是我计划设置的方式: url 将是 /blog/post/{pk}/postpk对象的主键;模板将被调用post.html,视图函数将是 post().

主列表将显示一个简单的链接:

<div class="commentlink">
    <a href="{% url blog.views.post post.pk %}">Comments</a>
</div>

如果这不是一个合适的问题,我很抱歉。

我已经到了发布页面的步骤,我只是不知道该怎么做!

我的意见.py

 from django.core.paginator import Paginator, InvalidPage, EmptyPage
 from django.core.urlresolvers import reverse
 from django.shortcuts import render_to_response
 from blog.models import *
 from django.core.context_processors import csrf
 from django.http import Http404, HttpResponseRedirect
 from django.core.urlresolvers import reverse

 def main(request):
     """Main listing."""
     posts = Post.objects.all().order_by("-created")
     paginator = Paginator(posts, 2)

     try: page = int(request.GET.get("page", '1'))
     except ValueError: 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 with comments and a comment form."""
     post = Post.objects.get(pk=int(pk))
     comments = Comment.objects.filter(post=post)
     d = dict(post=post, comments=comments, form=CommentForm(), user=request.user)
     d.update(csrf(request))
     return render_to_response("post.html", d)
 def add_comment(request, pk):
     """Add a new comment."""
     p = request.POST

     if p.has_key("body") and p["body"]:
         author = "Anonymous"
         if p["author"]: author = p["author"]

         comment = Comment(post=Post.objects.get(pk=pk))
         cf = CommentForm(p, instance=comment)
         cf.fields["author"].required = False

         comment = cf.save(commit=False)
         comment.author = author
         comment.save()
     return HttpResponseRedirect(reverse('blog:add_comment'))
4

1 回答 1

0

对于模板:

{% url blog:add_comment id_here %}

供查看

return HttpResponseRedirect(reverse('blog:view_name'))


def add_comment(request, pk):
    .......
    return HttpResponseRedirect(reverse("blog:post", args=[pk]))
于 2013-02-16T13:35:50.777 回答