【问题标题】:Django NoReverseMatch newbieDjango NoReverseMatch 新手
【发布时间】:2012-01-26 21:22:14
【问题描述】:

我正在尝试为论坛主题创建回复页面。论坛视图和线程视图工作正常,但我在设置回复和启动新线程页面时遇到问题。已成功创建话题和论坛。

这是我的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/ 时,我得到:

未找到带有参数“(u'1',)”和关键字参数“{}”的“webnotes.forum.views.reply”的反向操作。

我还发现这是在回溯中:

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

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

附:我正在关注这个网站上的教程:http://www.lightbird.net/dbe/forum1.html

【问题讨论】:

    标签: django view django-urls


    【解决方案1】:

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

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

    【讨论】:

    • 对不起,我不明白,我的反向不是查找 reverse("webnotes.forum.views.%s" % ptype, args=[pk])?
    • 是的,您定义的网址“forum.views.reply”,但您反转了“webnotes.forum.views.reply”。这是两个不同的字符串。
    • 我正在使用 url(r"^post/(new_thread|reply)/(\d+)/$","forums.views.post",name ='forum_post') 与 reverse(" forum_post", args=[ptype,pk]) 但没有运气。
    • args=['reply', 23] 怎么样?如果可行,则您的 ptype 或 pk 值与 regxp 不匹配。也请更新您的问题中的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-04
    • 1970-01-01
    • 2014-02-10
    • 2017-05-21
    • 2013-09-26
    相关资源
    最近更新 更多