abhay gurrala
abhay gurrala

Reputation: 171

Page not found (404) Error in Django

My urls.py is

 from django.conf.urls import patterns,url
 from rango import views
 urlpatterns=patterns('',url(r'^$',views.index,name='index'))
 urlpatterns=patterns('',url(r'^about/$',views.about,name='about'))

My views.py is

 from django.shortcuts import render
 from rango.models import Category
 # Create your views here.
 from django.http import HttpResponse
 def index(request):
     category_list = Category.objects.order_by('-likes')[:5]
     context_dict={'categories':category_list}
     return render(request, 'rango/index.html', context_dict)
 def about(request):
     return HttpResponse("go to index")

When I am trying to go to the address http://127.0.0.1:8000/rango I am getting page not found. But I am able to go to the address http://127.0.0.1:8000/rango/about.

When I remove the about url pattern in urls.py, I am able to go to the address http://127.0.0.1:8000/rango but not http://127.0.0.1:8000/rango/about, as the about url pattern does not exist.

I am unable to access both urls at once.

Upvotes: 0

Views: 220

Answers (1)

Alasdair
Alasdair

Reputation: 309089

You have defined urlpatterns twice. The second patterns containing the about view replaces the first, which stops you accessing the index view.

Instead of,

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

it should be:

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

In Django 1.7+, you don't need to use patterns any more, so you can simplify it to

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^about/$', views.about, name='about'),
]

Upvotes: 4

Related Questions