0

My view's function are not called by urls.py patterns. I can only call them explicitly.

Basic Layout is

--project
----persons
----project

project/urls.py is:

from django.conf.urls import url, include, patterns
from .views import page   #irrelevant
# from persons import views as person_views

urlpatterns = patterns('',
    url(r'^(?P<slug>[\w./-]+)/$', page, name='page'), #irrelevant
    url(r'^$', page, name='homepage'),                #irrelevant
    url(r'^persons/', include('persons.urls', namespace='persons')),  # WORKS
#   url(r'^persons/$', person_views.persons, name='persons'),   #wont work
#   url(r'^persons/add/$', person_views.add_person, name='add_person'),  #wont work
)

Everything is ok until this point, since persons.urls is included successfully... But inside:

persons/urls.py:

from django.conf.urls import patterns, url
#from persons.views import index_persons, add_person
from persons import views

#views.index_persons('GET / HTTP/1.0')  # >>> WORKS - function called <<< !!!

urlpatterns = patterns('',
    url(r'.', views.index_persons, name='index_persons'), # DOES NOT WORK
    url(r'^add/', views.add_person, name='add_person'),   # DOES NOT WORK
)

I have also tried other regex like:

    url(r'*', views.index_persons, name='index_persons'), # DOES NOT WORK
    url(r'^$', views.index_persons, name='index_persons'), # DOES NOT WORK

no luck...

My persons/views.py file contains:

def index_persons(request):
    print 'WHY???'

def add_person(request):
    print 'WHY???'

'WHY???' is normally printed in the console (stdout - since I execute from manage.py runserver), when the index_persons function is called explicitly from persons/urls.py

Any thoughts?

2 Answers 2

5

In project/urls.py, move the page url pattern below the other ones. Otherwise, a request to /persons/ will be matched by the page url pattern first.

url(r'^$', page, name='homepage'),
url(r'^persons/', include('persons.urls', namespace='persons')),
url(r'^(?P<slug>[\w./-]+)/$', page, name='page'),

Inside persons/urls.py, you should have:

urlpatterns = patterns('',
    url(r'^$', views.index_persons, name='index_persons'),
    url(r'^add/$', views.add_person, name='add_person'),
)
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much Alasdair. To be precise I had to put the r'^persons/ rule below both the other statements.
Glad you got it working. I got the order wrong when I copy-pasted before, I've fixed it now.
1

Your url rule should be

url(r'^/?$, views.index_persons, name='index_persons'), 

NOTE 1: Don't forget to restart the server.

NOTE 2: namespace='persons' is equal to not set namespace, because your url is persons/ is the same.

3 Comments

That second note is not true, Django doesn't take the namespace from the url pattern. If you don't set it, the urls are not namespaced.
@knbk I mean, is the same because your url pattern and namespace will be the same.
Ok, maybe I just misunderstood. If you mean that the matching path will be the same regardless of namespace, you're right. Not (explicitly) setting the namespace or setting it to 'persons' makes a huge difference when reversing urls, though.

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.