Why do you show me the error 'NoReserveMatch' in the reverse method by passing it by parameters the name of a url that supposedly is correct?

0

Previously I was working on Django in its version 1.11 and go to version 2.0

This brought with it that some tests that I had defined do not work for me.

Below I put the code of one:

from rest_framework.test import APITestCase

from django.urls import reverse

class PingApiTestCase(APITestCase):

    def setUp(self):

        self.access_token = self.create_access_token() # metodo que me crea un token para el test

    def test_increase_token_time(self):

        url = reverse('ping')
        data = { 'token': self.access_token.token }
        response = self.client.patch(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)

This is the definition of the url corresponding to the name 'ping':

from django.conf.urls import url

urlpatterns = [

url(r'^ping/$', 
    ping_view.PingView.as_view(), 
    name='ping')

]

In the test, when it reaches the line to invoke the reverse, it sends the following error:

  

{NoReverseMatch} Reverse for 'ping' not found. 'ping' is not valid   view function or pattern name.

Any ideas?

Thank you very much in advance

    
asked by Peter Paul 06.03.2018 в 21:16
source

1 answer

0

The question is:

  • In the file urls.py you have to do a small refactorization due to the migration of Django to its version 2.0, that is:

    from django.urls import path
    
    urlpatterns = [
    
    path(r'^ping/$', 
        ping_view.PingView.as_view(), 
        name='ping')
    ]
    
  • Because in my project I have more than one application, in the line that I invoke the method reverse , in the parameter I must also define the application where the route comes from, that is:

    def test_increase_token_time(self):
        url = reverse('adminapp:ping')
        data = { 'token': self.access_token.token }
        response = self.client.patch(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)    
    
  • answered by 07.03.2018 в 23:11