Mažas
Mažas

Reputation: 397

Django included urls doesn't work

Can't figure out why included urls isn't working. Project has 1 my app. So in myproject/mysite/urls.py i have:

urlpatterns = i18n_patterns('',
    url(r'^admin/', include(admin.site.urls)),
    ......
    url(r'^sub/', include('subscription.urls')),
    url(r'^', include('cms.urls')),
)

Then in myproject/subscription/urls.py:

from django.conf.urls import patterns, url

from .views import subscribe

urlpatterns = patterns(
    url(r'^subscribe/', subscribe),
)

If i try to go to http://localhost:8000/lt/sub/subscribe/ it displays 404 page (Page not found). What might be the problem?

EDITED: Project tree:

myproject/
    manage.py
    media/
    static/
    subscription/
        templates/
        __init__.py
        admin.py
        forms.py
        models.py
        urls.py
        views.py
    mysite/
        locale/
        static/
        templates/
        __init__.py
        settings.py
        urls.py
        wsgi.py

Upvotes: 0

Views: 144

Answers (2)

knbk
knbk

Reputation: 53669

You have an error in calling the patterns() function:

urlpatterns = patterns(
    url(r'^subscribe/', subscribe),
)

patterns() accepts a view prefix as the first argument, if you pass an url() instance, this will not be used as a url pattern. If you had any other url patterns, this would give you an error, but in this specific case patterns() will simply return an empty list.

Since patterns() is deprecated, it is better to switch to the new-style url configuration, and use a list:

urlpatterns = [
    url(r'^subscribe/', subscribe),
]

Otherwise you'd have to pass a prefix as the first argument. Since you don't actually use the prefix (you pass a view function, not the import location as a string), this would generally be the empty string ''.

Upvotes: 1

Sayse
Sayse

Reputation: 43300

if i try localhost:8000/sub/subscribe, it appends url with /lt/

Thats because your url pattern requires a trailing slash, which you haven't provided

r'^subscribe/'  # Shouldn't have the slash or should be optional

Normally this wouldn't be a problem since djangos APPEND_SLASH would help you out (if you're using CommonMiddleware) but it would seem that django-cms has a catch-all redirect applied on your application to redirect to a locale.

Upvotes: 0

Related Questions