A
A
Artyom Innokentiev2015-11-08 11:31:09
Django
Artyom Innokentiev, 2015-11-08 11:31:09

How to write controller code shorter in such a case?

When passing the context to the templates, I always pass the Basket - so that the header contains the number of items in the basket.
Controllers code:

def view1(request):
      return render(request, 'template1.html',  {'cart': cart})

def view2(request):
      return render(request, 'template2.html',  {'cart': cart})

def view3(request):
      return render(request, 'template3.html',  {'cart': cart})

Header code: I'm sure that this can be written shorter, and not always pass the Cart object. What solutions exist?
<p>Корзина <span>{{ cart.get_count }}</span></p>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladisus, 2015-11-08
@artinnok

look at context processors .

U
un1t, 2015-11-08
@un1t

make your own template tag https://docs.djangoproject.com/en/1.8/howto/custom...
and then in the template
{% get_cart as cart %}
{{ cart.get_count }}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question