2
2
A few bits of helper functions for comment views.
7
from django.core import urlresolvers
7
from urllib.parse import urlencode
8
except ImportError: # Python 2
9
from urllib import urlencode
8
11
from django.http import HttpResponseRedirect
9
from django.shortcuts import render_to_response
12
from django.shortcuts import render_to_response, resolve_url
10
13
from django.template import RequestContext
11
14
from django.core.exceptions import ObjectDoesNotExist
12
15
from django.contrib import comments
13
16
from django.utils.http import is_safe_url
15
def next_redirect(request, default, default_view, **get_kwargs):
18
def next_redirect(request, fallback, **get_kwargs):
17
20
Handle the "where should I go next?" part of comment views.
19
The next value could be a kwarg to the function (``default``), or a
20
``?next=...`` GET arg, or the URL of a given view (``default_view``). See
22
The next value could be a
23
``?next=...`` GET arg or the URL of a given view (``fallback``). See
21
24
the view modules for examples.
23
26
Returns an ``HttpResponseRedirect``.
25
next = request.POST.get('next', default)
28
next = request.POST.get('next')
26
29
if not is_safe_url(url=next, host=request.get_host()):
27
next = urlresolvers.reverse(default_view)
30
next = resolve_url(fallback)
37
40
joiner = ('?' in next) and '&' or '?'
38
next += joiner + urllib.urlencode(get_kwargs) + anchor
41
next += joiner + urlencode(get_kwargs) + anchor
39
42
return HttpResponseRedirect(next)
41
44
def confirmation_view(template, doc="Display a confirmation view."):