我的Django URL出了什么问题?

3

urls.py文件中的代码行:

url(r'^(?P<customer_profile_id>\d+)/case/(?P<account_type>\w+)/$', view='case', name='case'),

来自HTML的行:

<form method="POST" action="{% url 'case/cash/' %}" id="create_collection_case" target="_blank">

错误:

NoReverseMatch at /admin/customers/1/
Reverse for 'case/cash/' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

我只有一个HTML表单,它会提交到Django的URL,然后加载Django视图。当页面加载时,我遇到了上述错误。如果您需要任何其他信息来进行故障排除,请告诉我!

2个回答

1
请指定url的名称(case),而不是url的一部分:
<form method="POST" action="{% url 'case' %}" id="create_collection_case" target="_blank">

0

如果你想让 Django 确定路由,你的正则表达式需要与表单操作完全匹配。请注意,\d+ 表示整数,我更喜欢使用 ([-\w+]+) 来匹配文本/字符串。

为了最佳实践,我建议你将 URL 重写为以下形式(始终将整数查询值放在末尾最好)

url(r'^case/(?P<account_type>[-\w+]+)/(?P<customer_profile_id>\d+)/$', view='case', name='case'),

你的表单操作应该采用这种方式,其中profile_id是一个整数

{% url 'case/cash/profile_id/' %}
#e.g. http://127.0.0.1:8000/case/cash/1/

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接