Django

Code

Ticket #6213 (reopened)

Opened 1 year ago

Last modified 3 months ago

PREPEND_WWW and APPEND_SLASH settings don't work with flatpages middleware

Reported by: esaj Assigned to: nobody
Milestone: Component: Core framework
Version: SVN Keywords: APPEND_SLASH PREPEND_WWW flatpages easy-pickings
Cc: semente@taurinus.org, xarquonster@gmail.com, jm.bugtracking@gmail.com, che@inkvizice.org Triage Stage: Accepted
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

The PREPEND_WWW and APPEND_SLASH settings don't force a redirect when FlatpageFallbackMiddleware is being used.

This is because a redirect only happens if the target URL can be resolved using urlresolvers.resolve() in the code.

This behaviour has occurred since the change in [6852].

If the target URL cannot be resolved immediately using urlresolvers.resolve(), perhaps we should check in process_response() to make absolutely sure the response status code will be 404? If some intermediate middleware, e.g. FlatpageFallbackMiddleware has returned a successful response code, we can then issue a redirect as per the old behaviour.

Attachments

common_middleware.py.diff (5.0 kB) - added by esaj on 12/29/07 16:50:54.
Patch for CommonMiddleware? to fix handling of APPEND_SLASH and PREPEND_WWW
common_middleware.2.py.diff (8.9 kB) - added by esaj on 06/20/08 19:17:24.
Updated patch with unit tests.
common_middleware.3.py.diff (9.1 kB) - added by esaj on 06/20/08 19:23:46.
Updated patch with slightly improved unit tests (more descriptive docstring comments).
common_middleware.4.py.diff (9.1 kB) - added by esaj on 08/13/08 04:41:51.
Updated patch

Change History

12/16/07 01:37:27 changed by SmileyChris

  • needs_better_patch changed.
  • stage changed from Unreviewed to Accepted.
  • summary changed from PREPEND_WWW and APPEND_SLASH settings don't work with flatpages middleware to PREPEND_WWW setting doesn't work with flatpages middleware.
  • needs_tests changed.
  • needs_docs changed.

#6167 handles APPEND_SLASH (someone didn't search before adding a ticket :P). Rewording summary to just cover PREPEND_WWW

12/18/07 09:27:13 changed by esaj

Ah, I didn't see #6167, but I don't think it's exactly the same: this ticket concerns the use of FlatpageFallbackMiddleware whereas #6167 seems to concern the use of the flatpage view (at least from looking at the patch).

It's actually a more general problem, in that if a middleware returns a successful response code (even when no view is mapped to the current URL), then ideally we should honour the APPEND_SLASH and PREPEND_WWW settings and issue a redirect.

12/18/07 16:13:23 changed by SmileyChris

Correct, they are different issues (hence why I didn't close).

To make APPEND_SLASH always append a slash (on a successful response code) goes against the new behaviour. It should only try the slash if it 404s. Sure, fix PREPEND_WWW like that though (even though I think it's a stupid setting that should belong to a web server, not the Django framework).

12/29/07 16:20:51 changed by esaj

Ah yes, what I should have said is that in CommonMiddleware's process_response() we should append a slash if it 404s and APPEND_SLASH is set.

The problem is that if you use FlatpageFallbackMiddleware, redirects do not happen even if they did before the change in [6852], as the change relies on being able to resolve the URL using the URLconf.

For example, if the URL "/foo" isn't found by FlatpageFallbackMiddleware, we should still redirect if APPEND_SLASH is set.

12/29/07 16:50:54 changed by esaj

  • attachment common_middleware.py.diff added.

Patch for CommonMiddleware? to fix handling of APPEND_SLASH and PREPEND_WWW

12/29/07 16:51:56 changed by esaj

  • summary changed from PREPEND_WWW setting doesn't work with flatpages middleware to PREPEND_WWW and APPEND_SLASH settings don't work with flatpages middleware.

12/29/07 16:52:58 changed by esaj

Changed the summary back since this ticket still covers the APPEND_SLASH problem.

12/29/07 17:05:26 changed by esaj

This patch also fixes #6167.

12/30/07 02:34:41 changed by SmileyChris

  • has_patch set to 1.
  • needs_tests set to 1.

From a brief overview, I'm not 100% sure this fixes #6167 (it will always resolve the non-slash if you're using flatpages as the fallback in urlconf rather than using the middleware).

Go ahead and steal the tests in the other ticket, make sure this ticket fixes every case and then close #6167 as superceeded by this one. (You'll probably need some more tests for the things you're fixing)

02/21/08 15:51:09 changed by SmileyChris

  • keywords changed from APPEND_SLASH PREPEND_WWW flatpages to APPEND_SLASH PREPEND_WWW flatpages easy-pickings.

04/07/08 09:15:24 changed by Guilherme M. Gondim <semente@taurinus.org>

  • cc set to semente@taurinus.org.

06/20/08 19:17:24 changed by esaj

  • attachment common_middleware.2.py.diff added.

Updated patch with unit tests.

06/20/08 19:23:46 changed by esaj

  • attachment common_middleware.3.py.diff added.

Updated patch with slightly improved unit tests (more descriptive docstring comments).

06/20/08 19:27:27 changed by esaj

  • needs_tests deleted.

The unit tests catch the problem reported by this ticket, as well as checking some cases in #6167, which this ticket supercedes.

(follow-up: ↓ 14 ) 07/25/08 07:48:44 changed by lukeplant

Jason: In your patch, file django/middleware/common.py, line 108,

if resolve and new_url[0] == new_url[0] and ...

?

(follow-up: ↓ 19 ) 08/05/08 12:45:31 changed by jacob

  • status changed from new to closed.
  • resolution set to fixed.

(In [8218]) Fixed #6213: flatpage view now correctly redirects if settings.APPEND_SLASH. Thanks, crankycoder.

(in reply to: ↑ 12 ) 08/13/08 04:32:18 changed by esaj

Replying to lukeplant:

Jason: In your patch, file django/middleware/common.py, line 108, {{{ #!python if resolve and new_url[0] == new_url[0] and ... }}}

Oops, that should be:

if resolve and new_url[0] == old_url[0] and ...

08/13/08 04:39:34 changed by esaj

  • status changed from closed to reopened.
  • resolution deleted.

This issue has still not been fixed: if settings.PREPEND_WWW is True and I have a flatpage mapped to '/', http://jasondavies.com/ doesn't redirect to http://www.jasondavies.com/

This is because in the code at the moment, a redirect only happens if the target URL can be resolved using urlresolvers.resolve() , which obviously doesn't work when FlatpageFallbackMiddleware is used. My patch fixes this problem by checking for a successful response code from any preceding middleware, and issuing a redirect if necessary as per the old behaviour.

08/13/08 04:41:51 changed by esaj

  • attachment common_middleware.4.py.diff added.

Updated patch

08/13/08 05:12:30 changed by esaj

  • cc changed from semente@taurinus.org to semente@taurinus.org, xarquonster@gmail.com.

08/22/08 14:02:26 changed by jonas

The current solution also leads to an infinite "302 Found" redirect if APPEND_SLASH is True and a flatpage exists at "/".

Should I file a new bug for this?

08/22/08 14:03:41 changed by jonas

  • cc changed from semente@taurinus.org, xarquonster@gmail.com to semente@taurinus.org, xarquonster@gmail.com, jm.bugtracking@gmail.com.

(in reply to: ↑ 13 ) 09/17/08 19:54:41 changed by che@inkvizice.org

Replying to jacob:

(In [8218]) Fixed #6213: flatpage view now correctly redirects if settings.APPEND_SLASH. Thanks, crankycoder.

Changeset [8218] also causes flatpages to trigger redirect on all URLs that don't end with a slash and return 404 response, regardless whether any page exists.

For example, if you have flatpage '/about/', flagpages are going to redirect '/about' -> '/about/', but also '/robots.txt' to '/robots.txt/' (if you don't have the file handled in urlconf), etc.

09/17/08 20:01:53 changed by che@inkvizice.org

  • cc changed from semente@taurinus.org, xarquonster@gmail.com, jm.bugtracking@gmail.com to semente@taurinus.org, xarquonster@gmail.com, jm.bugtracking@gmail.com, che@inkvizice.org.

Add/Change #6213 (PREPEND_WWW and APPEND_SLASH settings don't work with flatpages middleware)




Change Properties
Action