Login

Cheap direct_to_tempalte patterns

Author:
jjdelc
Posted:
November 14, 2008
Language:
Python
Version:
1.0
Score:
-1 (after 1 ratings)

Django cheap-pages

Methods to use when you just want to use the Django dispatcher and there will be no extra business logic in your pages.

In some cases flatpages is too flat, and store templates in DB is too much hassle

    >>> url(^name/$,
    ...    direct_to_template,
    ...    {'template': 'name.html'},
    ...    name='name')

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

    urlpatterns = patterns('', ...)
    urlpatterns += build('Regexp', ['page1', 'page2', ...])
 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. Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Please login first before commenting.