Django URLS, how to map root to app?

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'),
)

Just put an empty raw regular expression: r''

I tested here and it worked perfectly.

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

Hope it help!


This sounds strange.

Your latest attempt should work, but what I normally do - put

urlpatterns = patterns('',
    (r'^$', lambda r: HttpResponseRedirect('myapp/')),
    ...
)

This scales better when you start adding new apps.


As I didn't see any answer for django 2.0 I thought I'll provide one. you have to use '' as the root url. Here is an example from django 2.0 docs

urlpatterns = [
    path('', main_views.homepage),
    path('help/', include('apps.help.urls')),
    path('credit/', include(extra_patterns)),
]