#20500: Flatpages catchall URLconf example that works with APPEND_SLASH
-------------------------------------+-------------------------------------
     Reporter:  josh.23.french@…     |                    Owner:  ByteMonk
         Type:                       |                   Status:  assigned
  Cleanup/optimization               |                  Version:  1.4
    Component:  Documentation        |               Resolution:
     Severity:  Normal               |             Triage Stage:
     Keywords:  flatpages            |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  1                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by josh.23.french@…):

 If we skip right to the redirect, we'd end up redirecting pages to 404s,
 which seems like something we want to keep.
 
([https://github.com/django/django/commit/196ac8f8b31302d026af6d79f764c8f146a8c78e
 196ac8f8b3])
 (If it's not, ignore everything that follows.)
 [[BR]]
 [[BR]]
 ''Keep in mind I'm completely new to Django's codebase(4 months), so the
 following code might be inappropriate.''

 This logic might be too heavy, but if we want to keep the no-redirect in
 cases where it'll end up a 404, it's something to think about:

 {{{
 #!python
 from django.core import urlresolvers

 if not url.endswith('/') and settings.APPEND_SLASH:
     url += '/'

     # Check if new url will resolve, and get the view info if it does.
     try:
         found_url = urlresolvers.resolve(url)
     except urlresolvers.Resolver404:
         raise

     # Check if the new url is a flatpage. If it is, skip the second half
 of the OR and redirect.
     if (FlatPage.objects.filter(url__exact=url,
 sites__id__exact=site_id).exists()

        # Check if the new url_name would bring us back here. (If it won't,
 redirect.
        # If it will, both sides of the OR are False and we raise the 404.)
        or (found_url.url_name !=
 "django.contrib.flatpages.views.flatpage")):

         # Stole this from CommonMiddleware. We want the same warning...
 and CommonMiddleware's warning will never get raise'd.
         if settings.DEBUG and request.method == 'POST':
             raise RuntimeError((""
             "You called this URL via POST, but the URL doesn't end "
             "in a slash and you have APPEND_SLASH set. Django can't "
             "redirect to the slash URL while maintaining POST data. "
             "Change your form to point to %s (note the trailing "
             "slash), or set APPEND_SLASH=False in your Django "
             "settings.") % url)
         return HttpResponsePermanentRedirect(url)
     else:
         # No flatpage exists
         raise
 else:
     raise
 }}}

 This is basically the APPEND_SLASH logic with a check for FlatPages
 specifically.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/20500#comment:5>
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 django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/082.4556c2fe9947d1e118f4edb80163e0ce%40djangoproject.com?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to