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 971323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:56:47+00:00 2026-05-16T02:56:47+00:00

I’ve got some templates which I’m building from some data I’ve got stored in

  • 0

I’ve got some templates which I’m building from some data I’ve got stored in my DB:

my_template = Template(string_data)

Now I want that template to extend another one (also stored as text in the DB). How can I do that? Looking for something like:

my_template.base = other_template

Or whatever the syntax is. Can’t find documentation on this.


I see template.nodelist in the source; can I maybe prepend some kind of extends node?


Added a bounty… will grant to first person who can tell me how to do this without hacking the core of django.

  • 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-16T02:56:47+00:00Added an answer on May 16, 2026 at 2:56 am

    No need to hack the core of Django. Creating your own template loader, as Ignacio recommends, is pretty simple. You just need to subclass django.template.loader.BaseLoader, and implement the load_template_source method. This just takes a string for the template name, which you can simply use to look up the item in the database and return the string.

    In your case, since you’ve got two template elements in the the single db row, you might want to do something to specify it in the extends tag:

    {% extends "myinstance.html_version" %}
    

    and simply split that in load_template_source:

    def load_template_source(self, template_name, template_dirs=None):
        name, field = template_name.split('.')
        tpl = TemplateModel.objects.get(name=name)
        return getattr(tpl, field)
    

    Of course you’d want to put some error handling in there, but you get the idea. You just then specify this template loader in your settings TEMPLATE_LOADERS tuple.

    Edit after comment I still don’t really understand why, but if all you want to do is to choose the template to extend from dynamically, why not just insert that in the text template before processing?

        tpl_string = get_template_from_wherever()
        extender = '{% extends "%s" %}' % template_name_to_extend
        tpl = Template(extender + tpl_string)
    

    Still a hack, but probably much less of one than mucking about with the internals of template inheritance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.