Django url pattern regex将电子邮件作为参数传递给url [英] Django url pattern regex to pass a email as a parameter in the url
问题描述
url(r'^ admin / detail_consultant_service /((P< consultant_id> \ [A-Z0-9 ._%+ - ] + @ [A-Z0-9 .-] + \ [AZ] {2,4}))/ $ ',
'admin_tool.views.consultant_service_detail',name =consultant_service_detail),
这里是我的模板的内容
{%for list_onsultants%}
< li>
{{consultant.id}}
< a href ={%url consultant_service_detail consultant.id%}> {{consultant | disp_info}}< / a> <峰; br />
< / li>
{%endfor%}
BUt当我访问网址时,我收到错误
everse for'consultant_service_detail'with arguments'(u'rahul183@gmail.com',)'和关键字参数'{} ' 未找到。
请帮助我在正则表达式中我做错了为什么不接受这个邮件。这个问题还是别的什么?
你传递一个位置参数而不是一个关键字参数。以下内容应该起作用:
{%url consultant_service_detail consultant_id = consultant.id%}
另外如果你使用Django> = 1.5,你应该使用 url
如下: / p>
{%url'consultant_service_detail'consultant_id = consultant.id%}
由于这是一个新的行为,实际上(推荐)在早期版本的Django(我认为> = 1.3)中使用:
{%从未来%加载url%
{%url'view_name'...%}
根据这里,所以问题最有可能是定义本身。尝试这样:
url(r'^ admin / detail_consultant_service /(?P< consultant_id> [\w。%+ - ] + @ [A-Za-z0-9 .-] + \。[A-Za-z] {2,4})/ $',
'admin_tool.views.consultant_service_detail',
name =consultant_service_detail),
所以一个有效的URL将类似于:
foo.com/admin/detail_consultant_service/email.address@here.com/
I am writing a view which is accepting a email as parameter passed by url like
url(r'^admin/detail_consultant_service/((?P<consultant_id>\[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}))/$',
'admin_tool.views.consultant_service_detail', name="consultant_service_detail"),
And here is the content of my template
{% for consultant in list_consultants %}
<li>
{{consultant.id}}
<a href="{% url consultant_service_detail consultant.id %}">{{ consultant|disp_info }}</a> <br/>
</li>
{% endfor %}
BUt When I am accessing the url I am getting the error
everse for 'consultant_service_detail' with arguments '(u'rahul183@gmail.com',)' and keyword arguments '{}' not found.
Please help me out What I am doing wrong in my regex why it is not accepting this mail .Is this the problem or something else ?
You are passing a positional argument instead of a keyword argument. The following should work:
{% url consultant_service_detail consultant_id=consultant.id %}
Also if you are using Django>=1.5 you should use the url
as follows:
{% url 'consultant_service_detail' consultant_id=consultant.id %}
And since that is a new behavior, you can actually (which is recommended) use in in earlier versions of Django (I think >=1.3) by:
{% load url from future %}
{% url 'view_name' ... %}
The regex I think is fine according to here so the problem is most likely with the definition itself. Try this:
url(r'^admin/detail_consultant_service/(?P<consultant_id>[\w.%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4})/$',
'admin_tool.views.consultant_service_detail',
name="consultant_service_detail"),
so a valid url will be similar to:
foo.com/admin/detail_consultant_service/email.address@here.com/
这篇关于Django url pattern regex将电子邮件作为参数传递给url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!