2014-05-14 19 views
1

,我发现了错误Django的NoReverseMatch URL问题

"Reverse for 'recall' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'associate/recall/']" 

当我尝试提交表单。这里是我的html:

<form action="{% url 'associate:recall' ordered_group %}" method="post"> 
     {% csrf_token %} 

     <div> 
      <label for="recall">enter as many members of {{ ordered_group }} as you can recall </label> 
      <input type="text" id="recall" name="recall"> 
     </div> 
     <div id="enter_button"> 
      <input type="submit" value="enter" name="enter" /> 
     </div> 
     <div id="done_button"> 
      <input type="submit" value="done" name="done" /> 
     </div> 
    </form> 

“ordered_group” 是从 '学习' 视图结转模型对象:

urls.py:

urlpatterns = patterns('', 
    url(r'^learn/', "associate.views.learn", name='learn'), 
    url(r'^recall/', 'associate.views.recall', name='recall'), 
    url(r'^$', "associate.views.index", name='index'), 
) 

我想使用在学习视图上下文中提交给html的ordered_group模型对象,回到召回视图作为参数。可以这样做吗?这对我来说很有意义,但是做这件事的正确方法是什么?

views.py

def recall(request, ordered_group): 
    ... 


def learn(request): 
... 
ordered_group = ordered_groups[index] 

return render(request, 'associate/learn.html', {'dataset':model, 'ordered_group':ordered_group}) 

我想与

回答

3

在你的HTML,你正在做的提交形式:

{% url 'associate:recall' ordered_group %} 

Django的预计, “召回” 的网址是在“关联”命名空间中,因为“:”。但是,你需要声明的命名空间中urls.py,如:

url(r'^recall/', 'associate.views.recall', namespace='associate', name='recall') 

如果你不想命名空间,只是做:

{% url 'recall' ordered_group %} 

而且,关于 “ordered_group”,你需要声明它在你的网址,如:

url(r'^recall/(?P<ordered_group>\w+)', 'associate.views.recall', namespace='associate', name='recall') 

你传入ordered_group在HTML,youare在views.py期待这一点,但你是不是在你的网址期待这一点。

+0

我试过你的建议,我不认为这是问题所在。我仍然得到一个NoReverseMatch,但现在带有一个参数:“未找到带有参数'(,)'和关键字参数'{}'的'召回'的倒退。 “你能想到代码中的其他明显区域可能会出错吗? –