灵活的正则表达式 url,Django
我尝试做灵活的网址。我是这样做的
url(r'^(&\w*)?/?$', direct_to_template,
{'template': 'basic.djhtml'}),
get_absolute_url
def get_absolute_url(self):
return "/&%s" % self.human_redble_url
问题是错误:
direct_to_template() 获得关键字参数“template”的多个值
这是什么意思?我该如何解决它?
在Python交互式解释器中,这个正则表达式可以工作
>>> import re
>>> reg = re.compile('^(&\w*)?/?$')
>>> result = reg.match('&post1')
>>> result
<_sre.SRE_Match object at 0xb77098a0>
>>> wrong = reg.match('aergsr')
>>> print wrong
None
>>> reg.match('post1')
>>> print reg.match('post1')
None
>>> print reg.match('&post1/')
<_sre.SRE_Match object at 0xb77097a0>
>>> print reg.match('&post1:')
None
I try to do flexible url. I did it this way
url(r'^(&\w*)?/?
get_absolute_url
def get_absolute_url(self):
return "/&%s" % self.human_redble_url
The problem is an error:
direct_to_template() got multiple values for keyword argument 'template'
What does it mean? How could I fix it?
In Python interactive interpretor this regexp works
>>> import re
>>> reg = re.compile('^(&\w*)?/?
, direct_to_template,
{'template': 'basic.djhtml'}),
get_absolute_url
The problem is an error:
direct_to_template() got multiple values for keyword argument 'template'
What does it mean? How could I fix it?
In Python interactive interpretor this regexp works
)
>>> result = reg.match('&post1')
>>> result
<_sre.SRE_Match object at 0xb77098a0>
>>> wrong = reg.match('aergsr')
>>> print wrong
None
>>> reg.match('post1')
>>> print reg.match('post1')
None
>>> print reg.match('&post1/')
<_sre.SRE_Match object at 0xb77097a0>
>>> print reg.match('&post1:')
None
, direct_to_template, {'template': 'basic.djhtml'}),get_absolute_url
The problem is an error:
direct_to_template() got multiple values for keyword argument 'template'
What does it mean? How could I fix it?
In Python interactive interpretor this regexp works
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不明白那个&符号在那里做什么,但没关系。
我怀疑问题是您没有在 URL 中使用命名组。因此捕获的字符串作为第一个位置参数发送到视图函数,它实际上是
template
,因此它与 template 关键字 arg 冲突。使用命名组 -
&(?P\w*)?/?$
- 它应该可以工作。I don't understand what that ampersand is doing there, but never mind.
I suspect the problem is that you have not used named groups in your URL. So the captured string is being sent through to the view function as the first positional argument, which is actually
template
, so it conflicts with the template keyword arg.Use a named group -
&(?P<my_arg>\w*)?/?$
- and it should work.