Django NoReverseMatch新手

时间:2011-12-23 14:51:05

标签: django view django-urls

我正在尝试为论坛帖子创建回复页面。论坛视图和线程视图工作正常但我无法设置回复并启动新的线程页面。设法创建了一个线程和论坛。

这是我的views.py:

def post(request, ptype, pk):
    """Display a post form."""
    action = reverse("webnotes.forum.views.%s" % ptype, args=[pk])
    if ptype == "new_thread":
        title = "Start New Topic"
        subject = ''
    elif ptype == "reply":
        title = "Reply"
        subject = "Re: " + Thread.objects.get(pk=pk).title

    return render_to_response("forum/post.html",add_csrf(request,subject=subject,action=action, title=title))


def new_thread(request, pk):
    """Start a new thread."""
     p = request.POST
    if p["subject"] and p["body"]:
        forum = Forum.objects.get(pk=pk)
        thread = Thread.objects.create(forum=forum,title=p["subject"],creator=request.user)
        Post.objects.create(thread=thread, title=p["subject"],body=p["body"],creator=request.user)
     return HttpResponseRedirect(reverse("webnotes.forum.views.forum", args=[pk]))


def reply(request, pk):
    """Reply to a thread."""
    p = request.POST
    if p["body"]:
        thread = Thread.objects.get(pk=pk)
        post = Post.objects.create(thread=thread,title=p["subject"],body=p["body"],creator=request.user)
    return HttpResponseRedirect(reverse("webnotes.forum.views.thread", args=[pk])+"?page=last")

这是我的urls.py:

url(r"^post/(new_thread|reply)/(\d+)/$", "forum.views.post"),
url(r"^post/reply/(\d+)/$", "forum.views.reply"),
url(r"^post/new_thread/(\d+)/$", "forum.views.new_thread"),

当我去http://localhost:8000/post/reply/1/时,我得到:

反向'webnotes.forum.views.reply',参数'(u'1',)'和关键字参数'{}'找不到。

我还发现这是追溯:

action = reverse("webnotes.forum.views.%s" % ptype, args=[pk])

与views.py对应。可能有什么不对?我希望我已经说清楚了。如果需要任何其他信息,我将很乐意提供。

P.S。我正在关注本网站上的教程:http://www.lightbird.net/dbe/forum1.html

1 个答案:

答案 0 :(得分:2)

您的反向正在查找"webnotes.forum.views.thread",但您的URLconf有"forum.views.new_thread"

通常,您应该提供您的网址names,并使用调用中的网址进行反转。

相关问题