Django: How to get field value dynamically using custom template tags?

duy picture duy · Nov 2, 2010 · Viewed 7.3k times · Source

I'm writing a generic template tag that could return a model's field value dynamically based on user inputs in template files. The idea follows which mentioned in the book "Practical Django Project 2nd Edition", but the book version is getting a list of objects where I want to get only a object's value. I want to get the site settings (Title, Tagline etc.) and pass in the template dynamically so that I don't have to write the code again to get each field's value.

Here is what I have done so far.

Define an admin.py for the model (not included here because it's not important)

Defined a model:

from django.db import models
from django.contrib.sites.models import Site

class Naming(models.Model):
    title = models.CharField(max_length=250)
    site_id = models.ForeignKey(Site)
    tagline = models.CharField(max_length=250)
    description = models.TextField(blank=True)

    def __unicode__(self):
        return self.title

Defined a template tag file, the commented line is where I get stuck

from django.db.models import get_model
from django.contrib.sites.models import Site
from django import template

def do_site_att(parser, token):
    bits = token.split_contents()
    if len(bits) != 5:
        raise template.TemplateSyntaxError("'get_site_att' tag takes exactly four arguments")
    model_args = bits[1].split('.')
    if len(model_args) != 2:
        raise template.TemplateSyntaxError("First argument to 'get_site_att' must be an 'application name'.'model name' string.")
    model = get_model(*model_args)
    if model is None:
        raise template.TemplateSyntaxError("'get_site_att' tag got an invalid model: %s." % bits[1])
    return ContentNode(model, bits[2], bits[4])

class ContentNode(template.Node):
    def __init__(self, model, field, varname):
        self.model = model
        self.field = field
        self.varname = varname

    def render(self, context):
        current_site = Site.objects.get_current()
        try:
            var = self.model.objects.get(site_id=current_site.id)
            context[self.varname] = var.title #I get stuck here because it not accepts input like var.field (I have extract the field value above)
        except:
            context[self.varname] = "Value not found"
        return ''


register = template.Library()
register.tag('get_site_att', do_site_att)

The template query in base.html:

{% load general_tags %}
{% get_site_att general.Naming title as title %}
<h1 id="branding">{{title}}</h1>
{% get_site_att general.Naming tagline as tagline %}
<h2 id="tagline">{{tagline}}</h2>

I have tried all the possible ways I can think of, but just can't get it works. Any help is really appreciated. Thanks.

Answer

duy picture duy · Nov 2, 2010

I found the solution for this:

on the commented lines, use this code:

var = self.model.objects.get(site_id__exact=current_site.id)
context[self.varname] = var.__dict__[self.field]#this will get the field's value dynamically, which is what I was looking for