A
A
Alex Xmel2021-03-16 09:03:40
Django
Alex Xmel, 2021-03-16 09:03:40

Why are there so many spaces in django templates?

There is such a view:

class IndexView(View):
    def get(self, request):
        data = {}
        data['mx1'] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
        return render(request, 'index.html', data)

and here is the output template:
<body>
{% for mx in mx1 %}
    {{ mx }}
{% endfor %}
</body>

and when I go to localhost and look at the page code, through ctrl + U I see the following:
60504a02d1ed4474972605.png
Why do these spaces appear and how to get rid of them?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2021-03-16
@Desead

Because in your code, both the instruction {% for mx in mx1 %}and {% endfor %}end with a line break character, which does not disappear after the template is processed. Either write a one-line loop, or use the spaceless tag to remove whitespace characters .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question