Jon
Jon

Reputation: 21

Django url confs won't match

I cannot for the life of me figure out why my URL configurations will not point to a page in django.

I am trying to access a URL shown in the error message at the bottom (couldn't get the link to escape, sorry)

YellowOrangeFoxZebra is a valid imgID, defined correctly in the models.

imgApp/urls.py

from django.conf.urls import patterns, url
from imgApp import views
urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
    url(r'^(?P<imgID>\d+)/$', views.detail, name='detail'),
)

views.py

def detail(request, given_image_ID):
image = get_object_or_404(imgAppImage, imgID=given_image_ID)
return render(request, 'imgApp/detail.html', image)

detail.html

<img src="{{ image.imgFile.url }}" >

The error message I get back out of django is:

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/imgApp/YellowOrangeFoxZebra/
Using the URLconf defined in thatSite.urls, Django tried these URL patterns, in this order:
^imgApp/ ^$ [name='index']
^imgApp/ ^(?P<imgID>\d+)/$ [name='detail']
^imgApp/ ^(?P<pk>\d+)/results/$ [name='results']
^imgApp/ ^(?P<question_id>\d+)/vote/$ [name='vote']
^admin/
^media/(?P<path>.*)$
The current URL, imgApp/YellowOrangeFoxZebra/, didn't match any of these.

Upvotes: 1

Views: 69

Answers (1)

Jon
Jon

Reputation: 21

Thanks to Avinash Raj above for correcting the error in my regex. I've fixed all the issues that popped up in the code after fixing that, a couple type errors, and for posterity have posted the correct versions of the code in question below:

imgApp/urls.py

from django.conf.urls import patterns, url
from imgApp import views
urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
    url(r'^(?P<inputID>\w+)/$', views.detail, name='detail'),
)

I swapped the d+ with a w+, and that fixed it! It was looking for digits when it needed to be looking for words.


views.py

def detail(request, inputID):
    image = get_object_or_404(imgAppImage, imgID=inputID)
    context = {'image': image}
    return render(request, 'imgApp/detail.html', context)

I renamed the given_image_ID for clarity to inputID, added the line defining context, and replaced image with context in the render call


detail.html

<img src="{{ image.imgFile.url }}" >

I had this one right! Now however it shows the image it's full size, which unfortunately may be MUCH larger than the window, so that's the next problem to solve!

Upvotes: 1

Related Questions