Django URLS, how to map root to app?

Hoof picture Hoof · Sep 28, 2011 · Viewed 54.8k times · Source

I am pretty new to django but experienced in Python and java web programming with different frameworks. I have made myself a nice little django app, but I cant seem to make it match www.mysite.com as opposed to www.mysite.com/myapp.

I have defined urls and views in my urls.conf which is currently not decoupled from the app (don't mind that).

urlpatterns = patterns('myapp.views',
  (r'^myapp/$', 'index'),
  (r'^myapp/(?P<some_id>\d+)/global_stats/$', 'global_stats'),
  (r'^myapp/(?P<some_id>\d+)/player/(?P<player_id>\d+)/$', 'player_stats'),
)

All this works like a charm. If someone goes to www.mysite.com/myapp they will hit my index view which causes a http redirect to my "correct" default url.

So, how can I add a pattern that will do the same as (r'^myapp/$', 'index') but without the /myapp--that is, www.mysite.com should suffice?

I would think this would be very basic stuff... I tried adding a line like:

(r'^$', 'index'),

however this throws me in a loop...

Hope you django gurus out there can clarify this for me!

Answer

Kamal Alseisy picture Kamal Alseisy · Mar 6, 2014

I know that this question was asked 2 years ago, but I've faced the same problem and found a solution:

In the project urls.py:

urlpatterns = patterns('',
    url(r'^', include('my_app.urls')), #NOTE: without $
)

In my_app.urls.py:

urlpatterns = patterns('',
    url(r'^$', 'my_app.views.home', name='home'),
    url(r'^v1/$', 'my_app.views.v1', name='name_1'),
    url(r'^v2/$', 'my_app.views.v2', name='name_2'),
    url(r'^v3/$', 'my_app.views.v3', name='name_3'),
)