Search code examples
djangoincludedjango-urlsdjango-flatpages

include() and flatpages confusion


I'm following Apress: Practical Django Projects and I've come across something that confuses me a little.

When I set up my url.py to point to flatpages it works fine if I do this:

...
(r'', include('django.contrib.flatpages.urls')),
...

But it doesn't work If I do this:

from django.contrib import flatpages
...
(r'', include(flatpages.urls)),
...

It tells me that:

'module' object has no attribute 'urls'

My knowledge of both Django and Python is pretty limited, so this may be really obvious, but it would be nice to understand what's going on :)

Thanks


Solution

  • It needs the urlpatterns variable from the other module. So try:

    from django.contrib import flatpages
    ...
    (r'', include(flatpages.urls.urlpatterns)),
    ...
    

    This is inline with the example in the django docs here.

    Edit:

    I found the issue. There is something messing up the imports from django.contrib, I am still looking into that. Change your import to from django.contrib.flatpages import urls.

    So your code will be:

    from django.contrib.flatpages import urls
    ...
    (r'', include(urls.urlpatterns)),
    ...