S
S
Sergey Eremin2015-05-09 02:08:28
Django
Sergey Eremin, 2015-05-09 02:08:28

Why doesn't django templates have something like a built-in set tag? And what to replace it with?

In order to maximize the separation of views and templates, sometimes you want django templates to have something like declaring an internal (inside the template) variable. For example, if some template block is used repeatedly. For example, in this piece:

<input type="text"
    class="form-control"
    value="{{ APP_INVITATION_STRING_IN_FORM }}"
    id="fio"
    autocomplete="on"
    onfocus="if(this.value=='{{ APP_INVITATION_STRING_IN_FORM }}'){
                                                        this.value='';
                                                        this.style.color='#000';
                                                        }"
    onblur= "if(this.value==''){this.value='{{ APP_INVITATION_STRING_IN_FORM }}';
                                this.style.color='#777';
                                }"
    />

it is very convenient to declare something like this in one place:
{{ set APP_INVITATION_STRING_IN_FORM = "Last name, First name and address of the apartment where the money is" }}
And that's it! Otherwise, you have to write the same thing in a bunch of places ... over and over again ... But if there was a {{ set }} tag, then life would become easier. Before the next input, I made a new assignment {{ set }} and simply copied the code. Of course, you can pass APP_INVITATION_STRING_IN_FORM from a view, but this is not convenient when the same template is used by different views.
How would one organize such a {{ set }} without a tambourine, shamanism and changing the template engine?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vladimir Abramov, 2015-05-09
@Sergei_Erjemin

Docks rule, yes.
https://docs.djangoproject.com/en/1.8/ref/template...

S
sim3x, 2015-05-09
@sim3x

You should not store or set anything in templates - there is the last place for this
. Set the variable better in settings.py

<input type="text"
    class="form-control"
    value="{{ APP_INVITATION_STRING_IN_FORM }}"
    id="fio"
    autocomplete="on"
placeholder="{{ APP_INVITATION_STRING_IN_FORM }}"
    <!--
ВТФ!???
onfocus="if(this.value=='{{ APP_INVITATION_STRING_IN_FORM }}'){
                                                        this.value='';
                                                        this.style.color='#000';
                                                        }"
    onblur= "if(this.value==''){this.value='{{ APP_INVITATION_STRING_IN_FORM }}';
                                this.style.color='#777';
                                }" -->

>

How to style a placeholder
https://css-tricks.com/snippets/css/style-placehol...

B
bromzh, 2015-05-09
@bromzh

In 1.8, the way to replace the template engine seems to be simplified. If I wrote something on this version, I would immediately replace the standard template engine with jinja2. And there is a set , and a lot of other good things.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question