You are not logged in Log in Join
You are here: Home » Members » Stefan's Home » Zope3 Page Templates for Django » 1.0.4 » Django Page Templates » View Document

Log in
Name

Password

 

Django Page Templates

The django.contrib.pagetemplate package makes Zope3 Page Templates available to Django sites. You must have Zope3 installed and on your PYTHONPATH.

Untar djangopagetemplates-1.0.4.tar.gz into the django/contrib directory of your Django sandbox. You can now use Page Templates in your Django views.

Simply put a Page Template file (extension .pt not .html) into a directory on Django's template search path. You can then access it from your views much like you would a standard Django template.

Example Code

A "detail" view may look like:

    from django.contrib.pagetemplate import pagetemplate
    from django.core.extensions import DjangoContext as Context
    from django.core.exceptions import Http404
    from django.utils.httpwrappers import HttpResponse
    from django.models.myapp import articles

    def detail(request, article_id):
        try:
            a = articles.get_object(pk=article_id)
        except articles.ArticleDoesNotExist:
            raise Http404

        t = pagetemplate.get_template('myapp/detail')
        c = Context(request, {
            'title': a.title,
            'article': a,
        })
        return HttpResponse(t.render(c))

What's Different?

If you know Page Templates, you most likely know them from working with Zope. DjangoPageTemplate is a subclass of PageTemplateFile with the following differences:

  • You cannot pass a directory prefix to the constructor. Templates will be looked up along Django's TEMPLATE_DIRS path. The filename may contain a path portion however, like with Django's template loader.
  • You may omit the .pt file extension when specifying a template file.
  • The namespace does not contain certain bindings you may know from Zope. There is no context, no container, and no root, as they have no meaning in Django. There is also no request, unless you explicitly add it to the DjangoContext passed to render.
  • The top-level namespace of a DjangoPageTemplate contains:
    • template which refers to the current Page Template (self).
    • templates which is a namespace used to load METAL macros from other templates.
    • All values from the DjangoContext passed to render. This includes at least user, perms, and messages.
    • All keyword arguments passed to render.
    • A few more names (args, options, usage, nothing) which are PT specific and can be ignored.
  • Metal:use-macro expressions cannot rely on the Zope context to locate macros in other templates. The DPT namespace provides the templates object (a macro loader in disguise) for this.

    To access the macro master in template myapp/main use:

            <html metal:use-macro="templates/myapp/main/macros/master">
            </html>
    

    To refer to the macro props in the current template use:

            <div metal:use-macro="template/macros/props">
            </div>
    

Unicode

Page templates use Unicode throughout. This means that all values passed to a template must be either unicode strings, or be encoded in the Python interpreter's default encoding (normally ASCII, may be changed in sitecustomize.py).

Also, rendering a Page Template results in a unicode string. This is fine when you serve utf-8 (Django takes care of the conversion). In case you want your site to serve, say, latin-1, you have to encode the unicode string returned by the render method:

      return HttpResponse(t.render(c).encode('latin-1'),
                          mimetype='text/html;charset=latin-1')

Page Template Resources

Contact

DjangoPageTemplates 1.0.4
(c) 2005-2006, Stefan H. Holek, [email protected]
License: ZPL