Django NoReverseMatch网址问题

时间:2014-05-14 14:43:19

标签: django url reverse kwargs

我收到了错误

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

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

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

我想用

提交表单

1 个答案:

答案 0 :(得分:3)

在你的HTML中,你正在做:

{% url 'associate:recall' ordered_group %}

Django希望&#34;召回&#34;网址是&#34;关联&#34;命名空间,因为&#34;:&#34;。但是,您需要在urls.py中声明名称空间,例如:

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

如果您不想要名称空间,请执行以下操作:

{% url 'recall' ordered_group %}

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

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

您正在HTML中传递ordered_group,您希望在views.py中使用它,但您不希望在您的网址上使用此内容。