Cheap direct_to_tempalte patterns

 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from os.path import join

from django.conf.urls.defaults import url
from django.views.generic.simple import direct_to_template

def page(destination, prefix='',
        template_dir='', 
        name_prefix='',
        default_extension='.html'):
    """
    Returns an url() object for the given name using direc_to_template.
    For "name":
        >>> url(^name/$,
        ...    direct_to_template,
        ...    {'template': 'name.html'},
        ...    name='name')

        can be expressed as:
        >>> page('name')

    Usage:
        urlpatterns = patterns('views',
            url(r'Regexp/$', view, name='str'),
            url(r'Regexp2/$', view, name='str'),
            page('page_name'),
        )
    """
    return url(r'^%s/$' % join(prefix, destination),
                direct_to_template,
                {'template': '%s%s' % (join(template_dir, destination),
                    default_extension)}, 
                name='%s%s' % (name_prefix, destination))

def build(prefix, destination_list, 
        template_dir='', # Where to look for the templates
        name_prefix='', # Prefix to use to name URLs, cheap_page (the 'cheap_')
        default_extension='.html' # are the pages .html, .csv, .whatever?
        ):
    """
    Builds a patterns object to render all the pages
    in destination_list.

    Usage:
        urlpatterns = patterns('', ...)
        urlpatterns += build('Regexp', ['page1', 'page2', ...])
    """
    urlpatterns = []
    for destination in destination_list:
        urlpatterns.append(page(
            destination, prefix, template_dir, name_prefix,
            default_extension))
    return urlpatterns

More like this

  1. direct to template from a subdir by Scanner 5 years, 6 months ago
  2. Pagination Alphabetically compatible with paginator_class by vascop 1 year, 12 months ago
  3. Decorator to ease dynamic redirections in Django by david_bgk 5 years, 2 months ago
  4. Quickly check templates while sketching them out by Amr Mostafa 6 years, 12 months ago
  5. Automatic urls for static pages by jibbolo 3 years, 5 months ago

Comments

(Forgotten your password?)