Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3316728
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:30:13+00:00 2026-05-17T22:30:13+00:00

I’m writing a generic template tag that could return a model’s field value dynamically

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T22:30:14+00:00Added an answer on May 17, 2026 at 10:30 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.