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

后端 未结 2 1786
甜味超标
甜味超标 2021-02-16 00:21

I am writing a view which is accepting a email as parameter passed by url like

url(r\'^admin/detail_consultant_service/((?P\\[A-Z0-9._%+-]+         


        
相关标签:
2条回答
  • 2021-02-16 00:47

    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/
    
    0 讨论(0)
  • 2021-02-16 00:59

    If you have more than one application in a project, which is most likely the case here, your url should be namespaced. The url in the template should be in this format as explained Here

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

    Hence your code shoul look like this,

    {% url 'Your-app-Name:consultant_service_detail' consultant_id=consultant.id %}
    
    0 讨论(0)
提交回复
热议问题