Django NoReverseMatch url问题 [英] Django NoReverseMatch url issue

查看:163
本文介绍了Django NoReverseMatch url问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到错误

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

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

When I try to submit a form. Here is my 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是从学习视图继承的模型对象:

"ordered_group" is a model object that is carried over from the 'learn' view:

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'),
)

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

I am trying to use the ordered_group model object that is submitted in the learn view context to the html, back to the recall view as an argument. Can one do this? It makes sense to me, but what is the correct way of doing this?

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})

我想提交表单与

推荐答案

在你的HTML中,你在做:

In you HTML, you are doing:

{% url 'associate:recall' ordered_group %}

Django希望回忆url位于关联命名空间中,因为:。但是,您需要在urls.py中声明命名空间,如:

Django expects that "recall" url is in "associate" namespace, because of the ":". But, you need to declare the namespace in urls.py, like:

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

如果你不想命名空间,只需执行以下操作:

If you don't want the namespace, just do:

{% url 'recall' ordered_group %}

而且,关于ordered_group,您需要在您的网址中声明它,例如:

And, about "ordered_group", you need to declare it in your url, like:

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

您正在HTML中传递ordered_group,您希望在views.py中,但您不是期待您的URL。

You are passing ordered_group in HTML, youare expecting this in views.py, but you are not expecting this on you URL.

这篇关于Django NoReverseMatch url问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆