简体   繁体   English

URL模板标记中的多个参数

[英]Multiple arguments in URL Template Tag

I'm trying to add a couple arguments to my url, if I have only a single argument everything works great. 我正在尝试向我的网址添加几个参数,如果我只有一个参数,那么一切都很好。 However, if I try to use 2 it converts them to a dict and throws an error. 但是,如果我尝试使用2,它将把它们转换成字典并引发错误。 Below are a couple thing that I have tried without success, any help would be awesome. 以下是我尝试未成功的几件事,任何帮助都会很棒。 I'm running Django 1.8.4 我正在运行Django 1.8.4

            {% autoescape off %}
            <a href={% url "products.views.display_product_list" page=previous keyword=current_keyword %}>
                Previous</a>
            Current Page
            <a href={% url "products.views.display_product_list" page=next keyword=current_keyword %}>
                Next</a>
            {% endautoescape %}

            <a href={% url "products.views.display_product_list" page=previous,keyword=current_keyword %}>
                Previous</a>
            Current Page
            <a href={% url "products.views.display_product_list" page=next,keyword=current_keyword %}>
                Next</a>

The first gives an error of NoReverseMatch at /results/page-0/ 第一个在/ results / page-0 /处给出NoReverseMatch错误

While the second is TemplateSyntaxError at /results/page-0/ Could not parse the remainder 第二个是/ results / page-0 /处的TemplateSyntaxError无法解析其余部分

My URL looks like: 我的网址看起来像:

r'^(?:page-(?P<page>[0-9]*)/)(?:keyword-(?P<keyword>[0-9A-Z]*)/)?$'

Webpage Traceback: 网页回溯:

Reverse for 'products.views.display_product_list' with arguments '()' and keyword arguments '{'page': 0, 'keyword': 'dress'}' not found. 1 pattern(s) tried: ['results/(?:page-(?P<page>[0-9]*)/)(?:keyword-(?P<keyword>[0-9A-Z]*)/)?$']

Request Method: GET
Request URL:    http://127.0.0.1:8888/results/page-0/?csrfmiddlewaretoken=CxlgETtyGPQpKa9pG276SZ0zzPQky9JA&keywords=dress
Django Version: 1.8.4
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'products.views.display_product_list' with arguments '()' and keyword arguments '{'page': 0, 'keyword': 'dress'}' not found. 1 pattern(s) tried: ['results/(?:page-(?P<page>[0-9]*)/)(?:keyword-(?P<keyword>[0-9A-Z]*)/)?$']
Exception Location: C:\Users\William\AppData\Roaming\Python\Python34\site-packages\django\core\urlresolvers.py in _reverse_with_prefix, line 496
(?P<keyword>[0-9A-Z]*)

This part of your regex only matches digits and uppercase letters, but you pass a lowercased word to the {% url %} tag. 正则表达式的此部分仅匹配数字和大写字母,但是您将小写字母的单词传递给{% url %}标记。 You need to change the pattern accordingly: 您需要相应地更改模式:

(?P<keyword>[0-9a-zA-Z]*)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM