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.
Historically, it's common for URLs with a trailing slash to indicate a directory, and those without a trailing slash to denote a file: http://example.com/foo/ (with trailing slash, conventionally a directory) http://example.com/foo (without trailing slash, conventionally a file)
Among Django's many built-in features is APPEND_SLASH, which by default is set to True and automatically appends a slash / to URLs that would otherwise 404. Note: Web browsers aggressively cache past URLs and will often automatically add a trailing slash / as a result.
If you're creting a RESTful API using Django, this can be a good solution when developers POST data directly to endpoint URL. When using APPEND_SLASH , if they accidently sent it without trailing slash, and your urlconf is WITH a trailing slash they would get an exception about data lose when redirecting POST requests.
To do so, use re_path() instead of path() . In Python regular expressions, the syntax for named regular expression groups is (? P<name>pattern) , where name is the name of the group and pattern is some pattern to match.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With