Django how to define default app for home URL?
Solution 1
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.
Solution 2
Try not including your appname in the regular expression.
url(r'', include('myapp1.urls', namespace='myapp1', app_name='myapp1')),
Related videos on Youtube
jeff
Updated on July 09, 2022Comments
-
jeff almost 2 years
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 thatprojectname/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/
-
Anzelhave you tried
url(r'', include('myapp1.urls', ...)
?
-
-
jeff about 9 yearsThanks, I tried this and pasted the result at the end of question.
-
Wallace about 9 yearsTry Anzel's answer, I did not test mine b4 posting.
-
Anzel about 9 years@Wallace,
^$
won't work as the regex means only 1 url i.e/
. Changed it tor''
will append allapp1.urls
to/
and so it will work