bharal
bharal

Reputation: 16154

django trailing slash not being added

so i have a django app, and i visit this url:

http://127.0.0.1:8000/stories

and i get this:

Request Method:     GET

Request URL:    http://127.0.0.1:8000/stories

"stories" does not exist

and then i check out the urls.py and i see:

#stories
url(r'^stories/$',
    StoryShowView.as_view(
        context_object_name='story_list',
        template_name='accounts/viewAndAddStory.html')
),

and finally, i look at my settins.py and i see:

#appends a slash if nothing is found without a slash.
APPEND_SLASH = True

shouldn't, with the APPEND_SLASH set as above, the url without the slash be 301 redirected to the url with the slash, and then the webpage load?

if i do manually add the slash to the url, then the page loads as expected and everybody has some tea and knocks off early.

UPDATE:

i also have this entry in my settings.py:

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
)

UPDATE:

from the error message on the page when i try to access the url:

Django Version: 1.3.1

SOLVED: so okm was bang on the money, honey. The problem was my urls - right at the bottom, i had this:

if settings.DEBUG:
    urlpatterns += patterns('',
        url(r'^%s(?P<path>.*)$' % settings.MEDIA_URL[1:],
        'django.views.static.serve',
        {'document_root': settings.MEDIA_ROOT, 'show_indexes': True})
    )

What i hadn't, however, done was that the MEDIA_URL and the MEDIA_ROOT weren't entered in my settings.py - they were both just empty strings ('')

so the url finding thing was finding all the urls i'd entered, thinking they were css entries. I entered the values for the media_root (folder where my css etc files are) and media_url (the url i was using to indicate to get static files) and all was good.

Upvotes: 5

Views: 2598

Answers (1)

okm
okm

Reputation: 23871

The "does not exist" should be something like "Page not found". Thus, I suspect you're not facing a normal 404 but a 404 raised by some mis-matched view in mis-configured urlconf. For example, I found that django.views.static.serve would raise Http404('some_path does not exist'), can you check urls.py to ensure views such as static.serve does not match path such as /stories?

If there is a matching, Django will not append suffix slash and redirect automatically.

You could check by

from django.core.urlresolvers import resolve
resolve('/stories')

to know which view actually gets matched.

Upvotes: 9

Related Questions