Sunday, July 30, 2017

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

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






--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/2fc2f221-c972-4b09-8555-c188ab9535e8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment