Django Template - Stop Processing Template

I am looking for a way to clean a template in django. A simple solution would be to break it into several patterns, but we do not want to do this.

Basically we have the following

{%if data.some_state %} Display some markup {% else %} {%if data.some_state_2 %} State 2 different html view {% else %} {%if data.process_data %} Display some list of data {% else %} No Data to display! {% endif %} <!-- if data.process_data--> {% endif %} <!-- if data.some_state_2 --> {% endif %} <!-- if data.some_state --> 

So it is very difficult and difficult to read. If I could do this in a β€œfunction”, I would use if / else if or return.

Is there a way in the template language to do something like (stop_processing_template will report that we have finished the template ...):

 {%if data.some_state %} Display some markup {% endif %} {% django_stop_processing_template %} {%if data.some_state_2 %} State 2 different view {% endif %} {% django_stop_processing_template %} {%if data.process_data %} Display some list of data {% endif %} {% django_stop_processing_template %} No data provided ! 
+4
source share
4 answers

You can use jinaj2 to template this view (or the whole project), it supports if / elif / else branches:

 {% if data.some_state %} Display some markup {% elif data.some_state_2 %} State 2 different view {% elif data.process_data %} Display some list of data {% endif %} 

There are several different packages that are easy to use jinja2 in the django project, I used coffin and djinja for this.

+3
source

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 %} 
+4
source

Although I think the @burhan approach is better, you can also do what you want with a custom tag that sets the context variable to the boolean and the outer else can also be converted to the if tag

 #Set a context variable nodata to True {% setnodata True %} {%if data.some_state %} Display some markup #Set context variable nodata to False {% setnodata False %} {% endif %} {%if data.some_state_2 %} State 2 different view #Set context variable nodata to False {% setnodata False %} {% endif %} {%if data.process_data %} Display some list of data #Set context variable nodata to False {% setnodata False %} {% endif %} {% if nodata %} No data provided ! { % endif %} 

The custom setnodata tag simply sets the nodata context nodata to True or False, depending on the argument.

+2
source

Today I meet the same question. And I find this tag {% verbatim%} {% endverbatim%} . This works in Django1.5 + Example:

  {% verbatim %} <div class="entry"> <h1>{{ title }}</h1> <div class="body"> {{ body }} </div> </div> {% endverbatim %} 

You can also look below for more details:

+1
source

Source: https://habr.com/ru/post/1385559/


All Articles