Hi I have an infuriating problem.
I have a url pattern like this:
# mproject/myapp.urls.py
url(r'^project/(?P<project_id>\d+)/$','user_profile.views.EditProject',name='edit_project'),
it works fine in the browser but for testing, when I do this in the shell:
from django.test import Client
from django.core.urlresolvers import reverse
client= Client()
response = client.get(reverse('edit_project'), project_id=4)
I get the dreaded:
NoReverseMatch: Reverse for 'edit_project' with arguments '()' and keyword arguments '{}' not found.
What am I missing here?
You have to specify project_id
:
reverse('edit_project', kwargs={'project_id':4})
Doc here
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))
Resolve is also more straightforward
from django.urls import resolve
resolve('edit_project', project_id=4)
Documentation on this shortcut