0

Website has a homepage in addition to several data-driven apps. No problem. But as I'm trying to add in other non-data-driven pages (about, mission statement, etc) I'm having trouble with by url directs.

settings.py url patterns includes:

url(r'^$', include('home.urls')),
url(r'^mission/$', include('home.urls')),

home/urls.py includes:

url(r'^$', views.index, name='index'),
url(r'^mission/$', views.mission, name='mission'),

Directing the browser to the homepage loads the index view as it should, but directing the browser to /mission/ also loads the index view.

I realize I'm probably missing something small (and fundamental) here, but I've read over the docs for the hundredth time and read about a lot of other people's questions about urlpatterns, but I just can't figure out what's going on. The include() statement in settings.py doesn't seem to be the problem. Since the home index view loads it's obviously being directed to home/urls.py, and that file is so simple that I just can't see what the problem would be.

Could someone please educate me so I can move on to my next Django brick to the face? I appreciate it.


SOLVED - Thank you Sohan Jain

Actual issue was use of r'^$' in settings URLPATTERNS rather than r''. Use of the second include() statement was attempt to work around actual issue.

3
  • 2
    You have two urls with the same name, that's not how it should be done. Name should be unique, or if you have same name, you need to place them in different namespaces. Also you can't include(...) with the pattern that ends with $ Commented Feb 28, 2016 at 18:56
  • 1
    I don't understand why you've used include twice here. Commented Feb 28, 2016 at 19:00
  • @Daniel, I was getting a 404 without it. Commented Feb 28, 2016 at 19:05

1 Answer 1

2

When you include urls from another directory, their path must start with the first parameter.

So when you say url(r'^$', include('home.urls')), that means: for each url in home.urls, make its path start with ^$, i.e nothing.

And when you say url(r'^mission/$', include('home.urls')), that means: for each url in home.urls, make its path start with 'mission'.

And urls are matched in order. So navigating to /mission/ matches url(r'^mission/$', include('home.urls')) and then url(r'^$', views.index, name='index'), which yields the index page.

Here's what you want:

settings.py 
url(r'', include('home.urls'))

home/urls.py
url(r'^$', views.index, name='index'),
url(r'^mission/$', views.mission, name='mission'),
Sign up to request clarification or add additional context in comments.

4 Comments

@Jefe go to /mission/
Last comment was incorrect. Typo. When I originally had it similar to this I had r'^$' as the expression my settings.py urlpatterns. Thanks for the help, this is making sense now.
@Jefe do you still have problems?
@vishes_shell Got it now. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.