186

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?

Darwin Tech
  • 18,449
  • 38
  • 112
  • 187
  • 1
    in [lastest django](https://docs.djangoproject.com/en/dev/topics/http/urls/#reverse-resolution-of-urls), reverse is imported from urls. ie `from django.urls import reverse` – suhailvs Apr 18 '20 at 01:17

5 Answers5

359

You have to specify project_id:

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

Doc here

miki725
  • 27,207
  • 17
  • 105
  • 121
23

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'.

Srivats Shankar
  • 2,364
  • 1
  • 15
  • 19
4

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)) 
KRH
  • 130
  • 1
  • 6
2

Easiest way is by using kwargs with reverse() function:

from django.test import Client
from django.urls import reverse

url = reverse("edit_project", kwargs={"project_id": 4})
response = Client().get(url)
serfer2
  • 2,573
  • 1
  • 23
  • 17
1

The function resolve_url is also more straightforward

from django.shortcuts import resolve_url

resolve_url('edit_project', project_id=4)

Documentation on this shortcut

SebCorbin
  • 1,645
  • 1
  • 12
  • 23
  • respectfully tried and not working for me. im going to stick with reverse. 2018 django 2 – Kermit Jun 04 '18 at 21:33
  • @SebCorbin, literally you do not understand the `resolve` function. Its first parameter is a path not url_name – Wariored Aug 17 '21 at 09:55
  • 1
    @Wariored thank you for your comment, I noticed I was referring to the wrong function, and it appears it is not documented in the official documentation, only in the source code. Edited to fix this – SebCorbin Aug 18 '21 at 12:44