Extended pages - wait and render with parent template

30 views
Skip to first unread message

Nkansah Rexford

unread,
Jul 12, 2015, 12:02:21 AM7/12/15
to django...@googlegroups.com
If template B extends template A, template B renders/parses its content independently before being added to the parent A template.

Is there a way to make template B wait and render only after when added to the parent A template?

Independent rendering of extended pages ( same thing happens with the {% include %} tag too [ see the Note section there] ) sounds great if thats what one expects. 

In my case, I wish I could let B (along with any includes made) wait , conjoin with the parent template, A, then all in unison, renders/parses the content together. I tried to do below, but it failed, for obvious reasons ( $ function isn't defined. Template_B doesn't reference jquery resource )

template_B.html: 

{% extends 'template_A.html' %}
{# usual html stuffs #}

{% block content %}
  {# page content stuffs here #}
  <script type="text/javascript">
    // A function targeting a specific area of, only
    // and Only the template_B.html page, e.g
    $( ".class" ).addClass( "new-class" );
  </script>
{% endblock %}

What I want to have is:

template_A.html

<html>
<head><!-- Head things -->
<script src="jquery-resource"></script>
</head>
<body>
<!-- below snippet coming from template_B.html -->
{% block content %}
  {# page content stuffs here #}
  <script type="text/javascript">
    // A function targeting a specific area of, only
    // and Only the template_B.html page, e.g
    $( ".class" ).addClass( "new-class" );
  </script>
{% endblock %}
</body>
</html>

Then when everything of template B has arrived, django now renders the page, in which case, 

Is such approach a bad thing? Performance-wise? Perhaps security-wise? Please enlighten me. Such a feature wouldn't be default, but a switch in cases like my own, one can turn on.

James Schneider

unread,
Jul 12, 2015, 3:10:37 AM7/12/15
to django...@googlegroups.com

The browser/client does not get two 'chunks' of HTML, Django renders the entire page first and sends the entire rendered piece at once after rendering both templates.

If you need your JS to be interpreted earlier, add an extra {% block %} to template_A up in the <head> section, and override that block rather than (or in addition to) overriding your content block so that your JS is interpreted before the rest of the body.

-James

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c4106146-0892-4513-bab5-f6c7d18c64a3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Peith Vergil

unread,
Jul 12, 2015, 3:31:50 AM7/12/15
to django...@googlegroups.com

I think the issue here is in how you're loading jQuery. It doesn't seem like jQuery has been loaded successfully. Make sure the path to your jQuery file is correct. This seems more like a client side issue rather than a server side (Django) issue.

When Django sends the HTML response to your browser, it's already in its final form. Meaning both templates A and B has already been rendered and assembled together. All this is done on the server side. The loading and execution of JS files happens on the client side (browser). Since
Django is a server side framework, it isn't concerned about how JS files are loaded on the browser.

--
Reply all
Reply to author
Forward
0 new messages