Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic template "Includes" with django

Tags:

python

django

I am building a Django website and my side bar can have different elements for different users. So my main sidebar template has a div for every plugin to be included and the specific HTML for every one of these plugins is included in their own template file.

example:

  <div id="plugins">
    <div id="plugin1">
      {% include 'plugin1.html' %}
    </div>
    <div id="plugin2">
      {% include 'plugin2.html' %}  
    </div>
  </div>

Now I want to build this list dynamically how could I do it? As the template is only parsed once so I could not send it a '{% include 'plugin1.html'}' string in the context

Any ideas?

like image 527
Alex Angelini Avatar asked Feb 21 '11 04:02

Alex Angelini


People also ask

What does Django template include?

A Django template is a text document or a Python string marked-up using the Django template language. Some constructs are recognized and interpreted by the template engine. The main ones are variables and tags. A template is rendered with a context.

What {% include %} does?

{% include %} Processes a partial template. Any variables in the parent template will be available in the partial template. Variables set from the partial template using the set or assign tags will be available in the parent template.

Can Django make dynamic websites?

As with every web framework, Django has a templating engine for creating dynamic HTML. The information that the user wants to access is created by this engine and presented through views.

How you can pass dynamic data in Django?

Using Views and Django Template we can display data in the template. There are lots of Django Template Tags and Filter for displaying data dynamically. We can pass the Model data to view for rendering it in the template.


1 Answers

You can use a variable inside the include tag:

{% include my_user_html %}
like image 164
Daniel Roseman Avatar answered Sep 23 '22 04:09

Daniel Roseman