Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django url pattern regex to pass a email as a parameter in the url

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'[email protected]',)' 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 ?

like image 862
user1481793 Avatar asked Jul 02 '13 15:07

user1481793


Video Answer


1 Answers

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 protected]/
like image 158
miki725 Avatar answered Nov 02 '22 07:11

miki725