Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including a Django app's url.py is resulting in a 404

I have the following code in the urls.py in mysite project.

/mysite/urls.py

from django.conf.urls.defaults import *
urlpatterns = patterns('',
    (r'^gallery/$', include('mysite.gallery.urls')),
)

This results in a 404 page when I try to access a url set in gallery/urls.py.

/mysite/gallery/urls.py

from django.conf.urls.defaults import *
urlpatterns = patterns('',  
    (r'^gallery/browse/$', 'mysite.gallery.views.browse'),
    (r'^gallery/photo/$', 'mysite.gallery.views.photo'),
)

404 error

Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^gallery/$
The current URL, gallery/browse/, didn't match any of these.

Also, the site is hosted on a media temple (dv) server and using mod_wsgi

like image 999
828 Avatar asked May 26 '10 06:05

828


People also ask

How do I fix 404 error in Django?

You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page. Let's do that by turning off Django debug mode. For this, we need to update the settings.py file.

How does urls py work in Django?

A request in Django first comes to urls.py and then goes to the matching function in views.py. Python functions in views.py take the web request from urls.py and give the web response to templates. It may go to the data access layer in models.py as per the queryset.

How do I add a URL to a Django project?

project/urls.py is the one that django uses, so you have to import to project/urls.py. Which means, that you have to state your imports in project/urls.py. The imported urls.py file must also define a valid urlconf, named urlpatterns. I think I had to restart the server (plus forgot the include), works now!

Is URL deprecated in Django?

Yes, if they upgrade to django-4.0, url will no longer be available.


1 Answers

Remove the $ from the regex of main urls.py

urlpatterns = patterns('',
    (r'^gallery/', include('mysite.gallery.urls')),
)

You don't need gallery in the included Urlconf.

urlpatterns = patterns('',  
    (r'^browse/$', 'mysite.gallery.views.browse'),
    (r'^photo/$', 'mysite.gallery.views.photo'),
)

Read the django docs for more information

Note that the regular expressions in this example don't have a $ (end-of-string match character) but do include a trailing slash. Whenever Django encounters include(), it chops off whatever part of the URL matched up to that point and sends the remaining string to the included URLconf for further processing.

like image 59
Amarghosh Avatar answered Nov 15 '22 21:11

Amarghosh