Allow any view (probably a generic view) to accept POST variables into extra_context

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import string

# changes a view such that it sends specified POST vars to the context
def view_post_vars_to_context(view):
    def view_with_pv2c(request, post_vars_to_context = {}, extra_context={}, *args, **kwargs):
        if request.method == "POST":
            for k, v in request.POST.items():
                if k in post_vars_to_context.keys():
                    if set(v) <= set(string.letters + string.digits + "-_"): # restrict to alphanumeric, plus _ and -, since POST isn't otherwised cleansed
                        context_var_name = post_vars_to_context[k]
                        extra_context[context_var_name] = v

        return view(request, extra_context=extra_context, *args, **kwargs)
    return view_with_pv2c

More like this

  1. Allow any view (probably a generic view) to accept captured URL variables into extra_context. by orblivion 2 years, 6 months ago
  2. Using Django Generics with Jinja2 by rmt 4 years, 5 months ago
  3. Paginator Tag by insin 6 years, 2 months ago
  4. Variable._resolve_lookup monkeypatch by showell 3 years, 6 months ago
  5. Arbitrary length formset by Rupe 3 years, 8 months ago

Comments

(Forgotten your password?)