Login

Default to a static template.

Author:
arthurfurlan
Posted:
May 3, 2009
Language:
Python
Version:
1.0
Tags:
template static default
Score:
1 (after 1 ratings)

Default to a static template.

Example:

    urlpatterns = patterns('',
        ...

        # this rule SHOULD BE the last one
        (r'^(?P<template>[a-z-_/]+/?)?$',
            'myproj.apps.myapp.views.static_template'),
    )
 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
# -*- coding: utf-8 -*-
#
# Copyright (c) 2009 Arthur Furlan <arthur.furlan@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# On Debian systems, you can find the full text of the license in
# /usr/share/common-licenses/GPL-2

from django.shortcuts import render_to_response
form django.template import RequestContext
from django.http import Http404

STATIC_TEMPLATE_BASEPATH = 'static'

def static_template(request, template):
    '''
    Default to a static template.

    Example:

        urlpatterns = patterns('',
            ...

            # this rule SHOULD BE the last one
            (r'^(?P<template>[a-z-_/]+/?)?$',
                'myproj.apps.myapp.views.static_template'),
        )
    '''

    # setting up response vars
    context_instance = RequestContext(request)
    template = re.sub('/+$', '', template)

    try:
        # looking for a template file
        template_name = '%s/%s.html' % (STATIC_TEMPLATE_BASEPATH, template)
        return render_to_response(template_name,
            context_instance=context_instance)
    except:
        try:
            # looking for a template directory
            template = '%s/%s/index.html' % (STATIC_TEMPLATE_BASEPATH, template
            return render_to_response(template_name,
                context_instance=context_instance)
        except:
            raise Http404()

More like this

  1. Automatic urls for static pages by jibbolo 4 years, 7 months ago
  2. Template tag to sort a list of links by pytechd 7 years, 10 months ago
  3. Search results pagination by polarbear 8 years, 3 months ago
  4. Creating custom time entries in Django Date widget by adiq 1 year, 1 month ago
  5. Support for {% macro %} tags in templates, version 2 by mludvig 7 years, 10 months ago

Comments

Please login first before commenting.