abhishekworld
abhishekworld

Reputation: 115

how to include urlpatterns in django?

In my project I have one app which have its own urls.py like this

urlpatterns = patterns('',
(r'^(?P<language>\w+)/$', 'MainSite.views.home_page'),)

(above file is in my application )

I am trying include this file in main(project's) urls.py like this :

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    (r'', include('myproject.MainSite.urls')),
    url(r'^admin/', include(admin.site.urls)),
)

if settings.DEBUG :
    urlpatterns += patterns('',
        (r'^media/(?P<path>.*)$', 'django.views.static.serve', 
{'document_root': settings.MEDIA_ROOT}),
        )

but after this I can able to call the MainSite's (app's) view but my admin url is not working I tried

urlpatterns = patterns('',
        (r'^$', include('myproject.MainSite.urls')),
        url(r'^admin/', include(admin.site.urls)),
    )

but after this this makes admin work but my app's view won't get called, how do I solve this.

Upvotes: 2

Views: 5215

Answers (1)

Chris Pratt
Chris Pratt

Reputation: 239440

You're including your views at the root level. Since it comes before the urlpattern for the admin, the first urlpattern catches everything, so nothing is ever passed to the admin views.

The simplest fix is to simply reverse the order:

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    (r'', include('myproject.MainSite.urls')),
)

Then, your views will only catch anything the admin doesn't.

Upvotes: 10

Related Questions