Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django how to define default app for home URL?

I'm learning Django, and so far I always had to use URL's like

projectname/appname/viewname

but what if I don't want appname to appear in the URLs for the "default" app, how can I configure my urls so that

projectname/viewname

will load the view viewname from my default app?

P.S. : Of course my primary goal is to be able to use the URL projectname/ to load the default view for the default app.

Details

Currently my ProjectName/urls.py has this:

urlpatterns = patterns('',
url(r'^static/(?P<path>.*)$', 'django.views.static.serve',
    {'document_root', settings.STATIC_ROOT}
  ),                                          
    url(r'^admin/', include(admin.site.urls)),
    url(r'^myapp1/', include('myapp1.urls', namespace='myapp1', app_name='myapp1')),
    url(r'^myapp2/', include('myapp2.urls', namespace='myapp2', app_name='myapp2')),    
)

so when I deploy my project to Heroku, and visit myproject.heroku.com, I get the error :

Page not found (404)
Request Method: GET
Request URL:    https://myproject.herokuapp.com/
Using the URLconf defined in MyProject.urls, Django tried these URL patterns, in this order:
^static/(?P<path>.*)$
^admin/
^myapp1/
^myapp2/

I know this is supposed to be, but how do I fix (or hack) this to get myproject.heroku.com to work?

If not possible, how can I redirect the homepage to myproject/myapp1/defaultview ? Thanks in advance !

my app's urls.py looks like this :

urlpatterns = patterns('myapp1.views',
    url(r'^view1/$', 'view1', name='view1'), # the default view
    url(r'^view2/(?P<oid>\d+)/(?P<pid>\d+)/$', 'view2', name='view2'),
)

Edit

After trying @Wallace 's suggestion url(r'^$', include('myapp1.urls', namespace='myapp1', app_name='myapp1')), and hitting the homepage, I now get the error:

Using the URLconf defined in myproject.urls, Django tried these URL patterns, in this order:
^static/(?P<path>.*)$
^admin/
^$ ^view1/$ [name='view1']
^$ ^view2/(?P<oid>\d+)/(?P<pid>\d+)/$ [name='view2']
^myapp2/
like image 630
jeff Avatar asked Dec 12 '22 00:12

jeff


1 Answers

Tried changing your project urls.py with:

url(r'', include('myapp1.urls', ...)

This will include all urls from myapp1.urls where they all append to /.

The reason why r'^$' won't work is because the regex ends with $ which means there can only be 1 x url /, and because your app1.urls only has 2 urls defined and without a / or ^$ equivalent, the url resolver will then fail.

But be aware of url clashes, if your project has a ^view1/$ url it will clash to your app1's view1 for example.

like image 85
Anzel Avatar answered Jan 06 '23 20:01

Anzel