0

hi i am trying to build flat pages in django here i would like to override the

base html

<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<div id="main">    
    {% block container %}
    {% block content %}{% endblock content %}
    {% endblock container %}
</div>
</body>
</html>

here i would like to override the block content, so instead of inheriting the base template then overriding blockcontent and use data from context like

newstatic.html

{% extends "base.html" %}
{% block content %}
   {{body}}
{% endblock %} 

is there any way to override the block directly from view itself ??

Amandeep Singh
  • 305
  • 2
  • 11
  • 1
    You can not override a template block from views. If you want to override a block from its parent you need to inherit from that parent and add your data in that block. Which you are doing ! Why do you want such a behavior ? – kapilsdv Jul 14 '16 at 08:23
  • currently I am inheriting base.html into newstatic.html and overriding block content. I just had this query that if i could override directly from view then I wont need newstatic.html and i could directly use base.html – Amandeep Singh Jul 14 '16 at 08:36
  • You can take if around block like `{% block content %}{% if context %}{{body}}{% elif %} {% endif %} {% endblock %}`. Pass a context with response to base.html from your view. If the context is present your {{body}} part will be shown otherwise default. But its not a good practice. There is nothing wrong currently in the way you are doing and its modular. – kapilsdv Jul 14 '16 at 08:52

1 Answers1

0

I'm not entirely sure what you are doing. But if you put {{ body }} into the content block inside base.html, then you would be able to render that template directly without needing newstatic.html at all.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895