direct to template from a subdir

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
def direct_to_template_subdir(request, template, subdir = None, **kwargs):
    """
    A wrapper around django's direct_to_template view. It prepend a
    given file path on to the template we are given. This lets us
    render templates in a sub-dir of the url pattern that matches this
    template.

    What do I mean?

    Take for example:

        url(r'^foo/(?P<template>.*\.html)$', direct_to_template,
            {'subdir' : 'subdir/'}),


    Which will template any url that matches <parent url>/foo/bar.html for any
    'bar'. The problem is if this is a sub-url pattern match this is going to
    look for the template "bar.html" when we may actually want it to get the
    template "<parent url>/foo/bar.html"

    Arguments:
    - `request`: django httprequest object... 
    - `template`: The template to render.
    - `subdir`: The subdir to prepend to the template.
    - `**kwargs`: kwargs to pass in to 
    """
    
    if subdir is not None:
        template = os.path.join(subdir,template)
    return direct_to_template(request, template, **kwargs)

More like this

  1. Automatically create urls for templates in a directory by blackrobot 1 year, 6 months ago
  2. Quickly check templates while sketching them out by Amr Mostafa 6 years, 1 month ago
  3. Cheap direct_to_tempalte patterns by jjdelc 4 years, 6 months ago
  4. Allow any view (probably a generic view) to accept captured URL variables into extra_context. by orblivion 2 years, 6 months ago
  5. Url filter middleware by limodou 6 years, 2 months ago

Comments

(Forgotten your password?)