Adrian Calin
Adrian Calin

Reputation: 3

404 error Django

I'm beginner in Django programing, and I want to show a view on my browser. My code is:

polls/view.py:

from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")

def animal(request):
    return HttpResponse("Hello cat")

polls/urls.py:

from django.conf.urls import patterns, url

from polls import views

urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
    url(r'^animal$', views.animal, name='animal'),
)

When I show index view, everything in okay. But, when I show animal view, my browser not found the page, and I dont know why. Thank you !

Upvotes: 0

Views: 83

Answers (1)

Brandon Taylor
Brandon Taylor

Reputation: 34553

Your second URL pattern isn't terminated. It needs to be:

url(r'^animal/$', views.animal, name='animal')

also, you want to list your URLs from most specific to least specific. Also note that you can use the patterns prefix to keep your code a bit more DRY.

urlpatterns = patterns('views',
    url(r'^animal/$', 'animal', name='animal'),
    url(r'^$', 'index', name='index'),
)

as Django will try to match them top-down

Upvotes: 1

Related Questions