0
我想創建一個論壇主題的回覆頁面。論壇的意見和線程視圖工作正常,但我無法設置答覆和啓動新的線程頁面。設法創建了一個線程和論壇。Django NoReverseMatch新手
這裏是我的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
對不起,我不明白,我的反向查找反向(「webnotes.forum.views。%s」%ptype,args = [pk])? – Neeran 2011-12-23 15:51:31
是的,你的定義網址爲「forum.views.reply」,但你反轉「webnotes.forum.views.reply」。這是兩種不同的字符串。 – jpic 2011-12-23 16:04:55
我正在使用url(r「^ post /(new_thread | reply)/(\ d +)/ $」,「forums.views.post」,name ='forum_post')和reverse(「forum_post」,args = [ptype ,pk]),但沒有運氣。 – Neeran 2011-12-23 16:15:36