dev*_*ev9 2 django django-urls
我有一个带有一个可选参数的网址格式:
# urls.py :
url(r'^(page/(?P<page>\w+))?$', MyIndexView.as_view(), name='index'),
Run Code Online (Sandbox Code Playgroud)
分页和其他所有方法都可以正常工作,直到我在模板中创建指向特定页面的网址为止:
# templates/mysite.html
{% url 'index' 54 %}
Run Code Online (Sandbox Code Playgroud)
然后我得到一个错误:
Reverse for 'index' with arguments '(54,)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'(page/(?P<page>\\w+))?$']
Run Code Online (Sandbox Code Playgroud)
没有该参数,它将起作用:
{% url 'index' %}
Run Code Online (Sandbox Code Playgroud)
我也尝试过:
{% url 'index' page=54 %}
Run Code Online (Sandbox Code Playgroud)
并得到类似的错误。
您可以创建两种URL模式,一种具有默认参数1,另一种具有pageURL中的匹配项:
# urls.py :
url(r'^page$', MyIndexView.as_view(), {'page': 1}, name='index'),
url(r'^page/(?P<page>\w+)$', MyIndexView.as_view(), name='index'),
Run Code Online (Sandbox Code Playgroud)
的第三个参数的url功能是kwargs,因此,kwargs['page']将1中的第一壳体和通过在第二网址限定。
| 归档时间: |
|
| 查看次数: |
5019 次 |
| 最近记录: |