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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:13:04+00:00 2026-06-11T12:13:04+00:00

I need to add to the output of the TemplateView html {%extends some_base.html%} in

  • 0

I need to add to the output of the TemplateView html {%extends some_base.html%} in the views.py.
I can’t work with html directly, cause template_name will be always different and i don’t want to add {%extends..%} to each template.html file.
i want to do something like this:

class PageView(TemplateView):

def get_context_data(self, **kwargs):
    object = PageModel.objects.get(view_base__slug=kwargs.get('slug'))
    self.template_name = object.template_name
    self.base='base.html'
    from django.template.loader import render_to_string
    #just example, it's not working
    rendered = render_to_string(self.template_name) 
    rendered= '{% extends' + self.base + '%} '+ rendered
    ###
    return locals()

But it does not work. Even more – i want to save all variables, which are being passed to the template.

  • 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-06-11T12:13:06+00:00Added an answer on June 11, 2026 at 12:13 pm

    I’m not sure why are you trying but you cannot put {%extends ...%} in HTML (unless you want to render it again using django templates. Adding that string to template after rendering will add unwanted {%extends ...%} string in the template.

    But if you want you can create a template dynamically and render it. The new template can extend existing template.
    For example:

    >>> from django.template import Template, Context
    >>> #creates a template from string, "base.html" can be self.base in your case
    >>> t = Template('{%extends "' + "base.html" + '"%} ...') 
    >>> c = Context({'your_var1': 'var1_value'})            #get context for template
    >>> t.render(c)                                         #render the created template 
    u'\n<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n
     <html xmlns="http://www.w3.org/1999/xhtml">
    ....
    

    More reference at: Template Compiling a string

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

Sidebar

Related Questions

I need to add an xmlns to the root element in the output of
I can't access a element with its href. Like this example, i need add
I have a need to add custom HTML attributes, specifically classes or styles to
i need to add a new item. this new item will be added as
I need to add CSS classes to each item output by an XSLT template.
I need to add some random html content with text before and after my
I need to add currency output to a jQuery function. So that a number
I need to add links to my resteasy api xml and json output. So
I need add Auto complete on apex Tabular Form. there is a column as
I need add a new user group for mediawiki. The new group has more

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.