Y7da
Y7da

Reputation: 433

How do I use a Django view to remove logic from templates?

I have read that don't use logic in your Django templates. Use a view. Yes, I can figure out why that is. But I don't really know how to implement it to change for example with this:

{% if '/admin/' in request.path or '/users/' in request.path %}
<div class="col-md-full">
{% else %}
<div class="col-md-8">
{% endif %}

How would a view do the above?

Upvotes: 0

Views: 107

Answers (1)

Sayse
Sayse

Reputation: 43320

Its bad to include business logic in your templates, that is to say logic that involves how your models work or behave and any other logic that affects the purpose of the web site/app.

The logic you've shown is to differentiate ui elements, also known as presentation logic, and that is ok because it directly modifies what should be shown to the user and has no effect whatsoever on the backend.

Upvotes: 3

Related Questions