Allow any view (probably a generic view) to accept captured URL variables into extra_context.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import inspect

# changes a view such that it captures specified variables from the URL and sends it to the context
def view_url_vars_to_context(view):
    def view_with_uv2c(request, url_vars_to_context = {}, extra_context = {}, *args, **kwargs):

        basicargs = inspect.getargspec(view).args

        # This is going to prevent some sort of confusion at some point.
        if set(url_vars_to_context.keys()) & set(basicargs):
            raise ValueError("Context variable names from URL can't be arguments expected by generic view.")

        for varname in url_vars_to_context.keys():
            if varname not in kwargs.keys():
                raise ValueError("URL variable %s does not exist" % varname)

        for k in kwargs.keys():
            if k in url_vars_to_context:
                context_var_name = url_vars_to_context[k]
                extra_context[context_var_name] = kwargs[k]
                del kwargs[k]
        
        return view(request, extra_context=extra_context, *args, **kwargs)
    return view_with_uv2c

More like this

  1. Allow any view (probably a generic view) to accept POST variables into extra_context by orblivion 3 years, 5 months ago
  2. A action decorator for URLs by Batiste 6 years ago
  3. direct to template from a subdir by Scanner 5 years, 6 months ago
  4. Quickly check templates while sketching them out by Amr Mostafa 6 years, 12 months ago
  5. Cheap direct_to_tempalte patterns by jjdelc 5 years, 5 months ago

Comments

(Forgotten your password?)