Django urlconf issue (know there has to be a simple answer)
Вопрос
Hi I am a beginner with django and all I have left to deploy the site is a stupid url conf problem. I have a simple site with 5 pages that are home,contact,about,reasons, and benefits
. Simple right, url conf will be (r'^home/$',index)
, and as follows for the rest of the pages. Here is the problem when I go to the home page /home/
fine. Now go to any other page on the navigation like /reasons/
or /about/
,they dont get called like that, instead get called /home/reasons/
or /home/about/
. Even more so if from /home/about/
I click back to home the call is /home/about/home
. As you see this goes on forever. How can it be where every request to a page is a simple /about/
or /contact/
instead of /home/contact or /home/about
.
I had defined one that went /home/contact/home/home/about/reasons/home
I cant possibly put all those in urlconf
Note: this is all being ran on django dev server
Решение
If your urls look like this:
(r'^home/$', myapp.views.index)
then use it in template as:
<a href="{% url myapp.views.index %}>some text</a>
to avoid wrong interpretation. Django will do the rest.
For Django 1.5 use
<a href="{% url 'myapp.views.index' %}>some text</a>
Другие советы
Make sure that in your templates you're using absolute URLs, so instead of
<a href="contacts">Contacts</a>
it should be
<a href="/contacts/">Contacts</a>
and there is no base tag in your template
Make sure you're links on your page look like
<a href="/home/">...</a> <-- this will take you to home page
and NOT
<a href="home/">...</a> <-- this will take you to {current_url}/home/