Django url pattern regex to pass email as parameter in url

I am writing a view that takes an email as a parameter passed to a url like

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"), 

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 access the URL, I get an error

 everse for 'consultant_service_detail' with arguments '(u' rahul183@gmail.com ',)' and keyword arguments '{}' not found. 

Please help me. What am I doing wrong in my regular expression, why is it not accepting this mail. Is this a problem or something else?

+4
source share
2 answers

You pass a positional argument instead of a keyword argument. The following should work:

 {% url consultant_service_detail consultant_id=consultant.id %} 

Also, if you use Django> = 1.5, you should use url as follows:

 {% url 'consultant_service_detail' consultant_id=consultant.id %} 

And since this is a new behavior, you can (recommended) use in earlier versions of Django (think> = 1.3):

 {% load url from future %} {% url 'view_name' ... %} 

The regular expression, I think, is in order here , so the problem is most likely related to the definition itself. Try the following:

 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 the actual url will look like:

 foo.com/admin/detail_consultant_service/ email.address@here.com / 
+11
source

If there are several applications in the project, which is most likely the case here, your URL should be placed in the names. The URL of the template should be in this format as described here.

 {% url 'My-App-Name:my-url-name' args or kwargs %} 

Therefore, your shoul code looks like this:

 {% url 'Your-app-Name:consultant_service_detail' consultant_id=consultant.id %} 
0
source

Source: https://habr.com/ru/post/1489296/


All Articles