I'm not sure what your logic will stop processing; but a cleaner way to make your logic would be to write your own tag that takes your arguments and then only returns the HTML related to your variables. This way you remove the if/else
loops and instead replace it all with a simple {% do_stuff %}
tag.
Edit
This is a very simple implementation to give you an idea of ββhow logic will go.
First you create patterns for each variation and save them somewhere django can find them.
Then a simple tag that displays the exact template you want (this is not verified, psuedo):
from django import template from django.db.models import get_model register = template.Library() class ProcessData(template.Node): def __init__(self, var_name): self.obj = get_model(*var_name.split('.')) def render(self, context): if self.obj.some_state: t = template.loader.get_template('some_markup_template.html') result = 'something' else: if self.obj.some_state_2: t = template.loader.get_template('some_different_html_view.html') result = 'something' else: if self.obj.process_data: t = template.loader.get_template('some_list_data.html') result = 'something' else: t = template.loader.get_template('no_data.html') result = 'something' return t.render(Context({'result': result}, autoescape=context.autoescape)) @register.tag def process_data(parser, token): try: tag_name, arg = token.contents.split(None, 1) except ValueError: raise template.TemplateSyntaxError("%r tag requires arguments" % token.contents.split()[0]) return ProcessData(arg)
Finally, in your template:
{% load my_tags %} {% process_data data.mymodel %}
source share