Question

I have a problem using tags in django templates. Here is part of my code in HTML file:

{% for habitacion in habitaciones %}
    {% with template_name=habitacion.url|add:".html" habitación_name=habitacion.name %}
        {% include "pacientes/habitaciones/"|add:template_name with habitacionNum=""|add:habitacion_name %}
        {% if forloop.counter|divisibleby:6 %}<hr>{% endif %}
    {% endwith %}
{% endfor %}

And this is my views.py code:

def AdministrativoView( request ):
    pacientes = Pacientes.objects.all()
    habitaciones = [{'url':'habitacionAdmLlena', 'name':'Habitación 1'},{'url':'habitacionAdmLlena', 'name':'Habitación 2'},{'url':'habitacionAdmVacia', 'name':'Habitación 3'},
    template_name = "pacientes/pacientes_administrativo.html"
    return render_to_response( template_name, { 'pacientes': pacientes, 'habitaciones': habitaciones } )

I want create dynamically the content of include tag, is posible to do this? I know that my code have many errors, or one at least, but I can't find it.

Thanks!

Was it helpful?

Solution 2

In your Place I would do the logic in the view, generate an array of relative URLs (what you now do in the template) and then just iterate the list in the template and include each in turn.

That way your template code gets cleaner. Additionally debugging code in a view is much easier than template code, because template tags lacks good Error Codes.

Trying to Use the least possible amount of template tags is a good idea. The add filter seems to me the Bad boy, which is exactly what I d do in the view.

You could also generate an array of dicts, if you need more variables to be attached to your include statements.

Hope his helps - que te vaya bien.

OTHER TIPS

I meet the same problem before, I want to include different base.html according the views function in one html template. Unfortunately, I failed... and I try another way.

  1. {% include base.html %} in every template html
  2. make some change to the base.html

    {% if case1 %}
        ...
    {% elif case2 %}
        ...
    {% endif %}
    

I know it is a rough way, but can work

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top