Error message on 'django.urls.exceptions.NoReverseMatch'

1,335 views
Skip to first unread message

ron_...@yahoo.co.uk

unread,
Jul 30, 2017, 3:06:38 AM7/30/17
to Django users

I have started writing a page to to display a photo. This page correctly display the photo data (nb. no photo added yet) as well as the ‘Invalid item ID’ message if an invalid value has been requested. The HTML code is in ‘photos.html’ is as follows:


{% extends 'personal/header.html' %}

 

{% block content %}

     <!-- 'index' from urls.py -->

     <a href="{% url 'indexurl' %}">&lt; Back</a>

     {% if pht != None %}

           <h2>{{ pht.name }}</h2>

           <h4>{{ pht.location.name }}</h4>

           <p>{{ pht.description }}</p>

     {% else %}

           <h1>Invalid item ID</h1>

     {% endif %}

{% endblock %}



 

The above html page is called by the following found in ‘views.py’:


def photo(request, photo_id):

     try:

           pht = Photo.objects.get(id=photo_id)

     except Photo.DoesNotExist:

           pht = None

    

     template = loader.get_template('wlp_app/photo.html')

     context = {

           'pht' : pht

     }

     return HttpResponse(template.render(context, request))


 


I am now trying to start testing the above function by using the following test function in tests_views.py:


class PhotosByLocationIndexViewTests(TestCase):

     """

     testing 'def photo(requeset)'

     """

     def test_for_no_photos_in_photos(self):

           # 'photourl' is in 'wlp_app/urls.py'

           response = self.client.get(reverse('photourl'))

           self.assertEqual(response.status_code, 200)

                                # '"Invalid item ID"' is in 'wlp_app/photos.html'

           self.assertContains(response, "Invalid item ID")

 


 

The ‘photourl’ name that is called by the ‘reverse’ function above is in the urls.py:


urlpatterns = [

     url(r'^$', views.index, name='indexurl'),

     url(r'^photo/(?P<photo_id>[0-9]+)/$', views.photo, name='photourl')

]

 


 

However, when I run the test I get the following error message on the ‘response = self.client.get(reverse('photourl'))’ line:


>python manage.py test wlp_app

Creating test database for alias 'default'...

System check identified no issues (0 silenced).

..F..E

======================================================================

ERROR: test_for_no_photos_in_photos (wlp_app.tests_views.PhotosByLocationIndexViewTests)

----------------------------------------------------------------------

Traceback (most recent call last):

  File "C:\Users\Ron\OneDrive\Web-design\RWS008\mysite\wlp_app\tests_views.py", line 28, in test_for_no_photos_in_photos

    response = self.client.get(reverse('photourl'))

  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\urls\base.py", line 91, in reverse

    return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))

  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\urls\resolvers.py", line 497, in _reverse_with_prefix

    raise NoReverseMatch(msg)

django.urls.exceptions.NoReverseMatch: Reverse for 'photourl' with no arguments not found. 1 pattern(s) tried: ['wlp_app/photo/(?P<photo_id>[0-9]+)/$']

 


Can anyone advise me on what I am doing incorrectly, please?


Thanks


Ron






James Schneider

unread,
Jul 30, 2017, 7:33:01 AM7/30/17
to django...@googlegroups.com


On Jul 30, 2017 12:06 AM, "ron_w_add via Django users" <django...@googlegroups.com> wrote:

I have started writing a page to to display a photo. This page correctly display the photo data (nb. no photo added yet) as well as the ‘Invalid item ID’ message if an invalid value has been requested. The HTML code is in ‘photos.html’ is as follows:

<snip>


I am now trying to start testing the above function by using the following test function in tests_views.py:


class PhotosByLocationIndexViewTests(TestCase):

     """

     testing 'def photo(requeset)'

     """

     def test_for_no_photos_in_photos(self):

           # 'photourl' is in 'wlp_app/urls.py'

           response = self.client.get(reverse('photourl'))

           


The above line is your issue. See below.


The ‘photourl’ name that is called by the ‘reverse’ function above is in the urls.py:


urlpatterns = [

     url(r'^$', views.index, name='indexurl'),

     url(r'^photo/(?P<photo_id>[0-9]+)/$', views.photo, name='photourl')

]


Per the urls.py above, you need to provide a value for the photo_id to reverse() as an arg or kwarg, otherwise Django has no way to know which photo you are referring to, and therefore can't generate the URL.

See the following for proper usage of reverse() with examples.


-James

ron_...@yahoo.co.uk

unread,
Jul 31, 2017, 11:20:36 AM7/31/17
to Django users
Hello James

Thanks for your help. That solved the problem.

Ron

Reply all
Reply to author
Forward
0 new messages