#25878: Django 1.9 works well in Debug=True, but doesn't redirect unslash urls
into
slashed one in Debug=False
-------------------------------+--------------------------------------
Reporter: kuna | Owner: nobody
Type: Bug | Status: new
Component: HTTP handling | Version: 1.9
Severity: Normal | Resolution:
Keywords: slash,debug | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Description changed by kuna:
Old description:
> I first uploaded it to google groups but I don't find where's my article
> is (I'm not get used to it) so I decided to make a ticket here.
>
> as I mentioned in title, django 1.9's url slash redirection doesn't
> working well in debug=False. APPEND_SLASH was no effect. but in
> debug=True, it does well, and only these 'cached(loaded in debug mode)'
> url redirects well even in debug=False. (I don't know it's browser
> redirection or django's work ...)
>
> anyway, I fixed django/middleware/common.py like this and works well.
> original code was rather a little strange, as I thought.
>
> {{{
> def process_request(self, request):
> """
> Check for denied User-Agents and rewrite the URL based on
> settings.APPEND_SLASH and settings.PREPEND_WWW
> """
>
> # Check for denied User-Agents
> if 'HTTP_USER_AGENT' in request.META:
> for user_agent_regex in settings.DISALLOWED_USER_AGENTS:
> if
> user_agent_regex.search(request.META['HTTP_USER_AGENT']):
> raise PermissionDenied('Forbidden user agent')
>
> # Check for a redirect based on settings.PREPEND_WWW
> host = request.get_host()
>
> if settings.PREPEND_WWW and host and not host.startswith('www.'):
> host = 'www.' + host
>
> # Check if we also need to append a slash so we can do it all
> # with a single redirect.
> if self.should_redirect_with_slash(request):
> path = self.get_full_path_with_slash(request)
> return self.response_redirect_class('%s://%s%s' %
> (request.scheme, host, path))
> }}}
>
>
> Hope this issue can be meaningful.
>
> thanks for reading
New description:
I first uploaded it to google groups but I don't find where's my article
is (I'm not get used to it) so I decided to make a ticket here.
as I mentioned in title, django 1.9's url slash redirection doesn't
working well in debug=False. APPEND_SLASH was no effect. but in
debug=True, it does well, and only these 'cached(loaded in debug mode)'
url redirects well even in debug=False. (I don't know it's browser
redirection or django's work ...)
anyway, I fixed django/middleware/common.py like this and works well.
original code was rather a little strange, as I thought.
{{{
def process_request(self, request):
"""
Check for denied User-Agents and rewrite the URL based on
settings.APPEND_SLASH and settings.PREPEND_WWW
"""
# Check for denied User-Agents
if 'HTTP_USER_AGENT' in request.META:
for user_agent_regex in settings.DISALLOWED_USER_AGENTS:
if
user_agent_regex.search(request.META['HTTP_USER_AGENT']):
raise PermissionDenied('Forbidden user agent')
# Check for a redirect based on settings.PREPEND_WWW
host = request.get_host()
redirect = False
if settings.PREPEND_WWW and host and not host.startswith('www.'):
host = 'www.' + host
redirect = True
# Check if we also need to append a slash so we can do it all
# with a single redirect.
if self.should_redirect_with_slash(request):
path = self.get_full_path_with_slash(request)
redirect = True
else:
path = request.get_full_path()
if (redirect):
return self.response_redirect_class('%s://%s%s' %
(request.scheme, host, path))
}}}
Hope this issue can be meaningful.
thanks for reading
PS changed a little - previous code won't work with prepending WWW
--
--
Ticket URL: <https://code.djangoproject.com/ticket/25878#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/062.56eeaca5e82e952d886a5b5a06980d9a%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.