与x27相反的Django错误;论坛';带参数';(';';,)';和关键字参数';{}';找不到

与x27相反的Django错误;论坛';带参数';(';';,)';和关键字参数';{}';找不到,django,Django,我正在使用本教程制作一个简单的论坛 我遇到了这个错误,我不知道该怎么办。你能帮我修复这个错误吗 NoReverseMatch at /forum/post/reply/1/ Reverse for 'forum' with arguments '('',)' and keyword arguments '{}' not found. Request Method: GET Request URL: http://127.0.0.1:8000/forum/post/rep

我正在使用本教程制作一个简单的论坛

我遇到了这个错误,我不知道该怎么办。你能帮我修复这个错误吗

  NoReverseMatch at /forum/post/reply/1/

  Reverse for 'forum' with arguments '('',)' and keyword arguments '{}' not found.

  Request Method:   GET
  Request URL:  http://127.0.0.1:8000/forum/post/reply/1/
  Django Version:   1.4.3
  Exception Type:   NoReverseMatch
  Exception Value:  

  Reverse for 'forum' with arguments '('',)' and keyword arguments '{}' not found.

  Exception Location:   C:\Python26\Lib\site-packages\django\template\defaulttags.py in render, line 424
  Python Executable:    C:\Python26\python.exe

  Error during template rendering

  In template C:\djcode\mysite\forum\templates\forum\post.html, error at line 1
  Reverse for 'forum' with arguments '('',)' and keyword arguments '{}' not found.


  1     <a href="{% url ben:forum forum_pk %}">&lt;&lt; back to list of topics</a>
我的网址:

 from django.conf.urls import patterns,include,url
 from django.contrib import admin
 from django.conf import settings

 urlpatterns = patterns('forum.views',
                        url(r'^$','main',name='main'),
                        url("^forum/(\d+)/$", "forum",name ="forum"),
                        url("^thread/(\d+)/$","thread",name = "thread"),
                        url(r"^post/(new_thread|reply)/(\d+)/$", "post",name = "post"),
                        url(r"^reply/(\d+)/$", "reply" , name ="reply"),
                        url(r"^new_thread/(\d+)/$", "new_thread" , name ="new_thread"),
 )

这有点让人困惑的
(“”,)”
意味着
url
参数列表由一个空字符串组成,就好像模板是
{%url:forum'%}
。如果没有视图代码,就不可能知道它是如何发生的

现在您发布了所有视图,我们仍然不知道是哪一个导致了错误:)但是,我们知道的是,没有一个将
论坛pk
上下文变量传递到
呈现到响应

是不是
post
?您需要找出论坛pk并将其传递给您的模板:

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

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

我不赞成这种
post
视图设计,其中
pk
根据
ptype
的不同含义。然而,如果
ptype
new\u-thread
,我们已经有了
forum\u-pk
作为
pk
。如果是
reply
,除了添加
forum\u pk=pk

我将回复的一个调整为:


forum_pk=Thread.objects.get(pk=pk).forum.pk

我想你需要包括你的url配置。不管怎样,你的
forum_pk
是一个空字符串,修复它。好吧,这听起来很难。我能把forum_pk%改成其他的吗?你是在学习教程还是自己做实验?本教程告诉您将此反向链接添加到
threads.html
thread
视图。您的意思是将此添加到threads.html吗@帕维尔·阿诺索夫
 from django.conf.urls import patterns,include,url
 from django.contrib import admin
 from django.conf import settings

 urlpatterns = patterns('forum.views',
                        url(r'^$','main',name='main'),
                        url("^forum/(\d+)/$", "forum",name ="forum"),
                        url("^thread/(\d+)/$","thread",name = "thread"),
                        url(r"^post/(new_thread|reply)/(\d+)/$", "post",name = "post"),
                        url(r"^reply/(\d+)/$", "reply" , name ="reply"),
                        url(r"^new_thread/(\d+)/$", "new_thread" , name ="new_thread"),
 )
 def post(request, ptype, pk):
     """Display a post form."""
     action = reverse("ben:%s" % ptype, args=[pk])
     if ptype == "new_thread":
         title = "Start New Topic"
         subject = ''
         forum_pk = pk
     elif ptype == "reply":
         title = "Reply"
         thread = Thread.objects.get(pk=pk)
         forum_pk = thread.forum.pk
         subject = "Re: " + thread.title

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