Django Reverse with arguments '()' and keyword arguments '{}' not found

前端 未结 4 1715
遥遥无期
遥遥无期 2020-12-02 07:17

Hi I have an infuriating problem.

I have a url pattern like this:

# mproject/myapp.urls.py

url(r\'^project/(?P\\d+)/$\',\'user_pro         


        
相关标签:
4条回答
  • 2020-12-02 07:29

    This problems gave me great headache when i tried to use reverse for generating activation link and send it via email of course. So i think from tests.py it will be same. The correct way to do this is following:

    from django.test import Client
    from django.core.urlresolvers import reverse
    
    #app name - name of the app where the url is defined
    client= Client()
    response = client.get(reverse('app_name:edit_project', project_id=4)) 
    
    0 讨论(0)
  • 2020-12-02 07:29

    Resolve is also more straightforward

    from django.urls import resolve
    
    resolve('edit_project', project_id=4)
    

    Documentation on this shortcut

    0 讨论(0)
  • 2020-12-02 07:34

    You have to specify project_id:

    reverse('edit_project', kwargs={'project_id':4})
    

    Doc here

    0 讨论(0)
  • 2020-12-02 07:41

    The solution @miki725 is absolutely correct. Alternatively, if you would like to use the args attribute as opposed to kwargs, then you can simply modify your code as follows:

    project_id = 4
    reverse('edit_project', args=(project_id,))
    

    An example of this can be found in the documentation. This essentially does the same thing, but the attributes are passed as arguments. Remember that any arguments that are passed need to be assigned a value before being reversed. Just use the correct namespace, which in this case is 'edit_project'.

    0 讨论(0)
提交回复
热议问题