Django context processor being called twice per request

Django context processor being called twice per request



I've found only one topic like this and not a single answer in there seems to work.
I have two context processors:


def cart_view(request):
try:
cart_id = request.session['cart_id']
cart = Cart.objects.get(id=cart_id)
request.session['total'] = cart.items.count()
print('OLD CART USED')
except:
cart = Cart()
cart.save()
cart_id = cart.id
request.session['cart_id'] = cart_id
cart = Cart.objects.get(id=cart_id)
print('NEW CART CREATED')
return 'cart':cart

# dropdown menu categories to every page
def categories(request):
print('CATEGORIES CONTEXT PROCCESOR')
categories = Category.objects.all()
return 'dropdown_categories':categories



Settings:


'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'django.template.context_processors.media',
'shopping.views.cart_view',
'shopping.views.categories',
]



Via those print statements I'm able to see that each one of those CP being executed twice per request, although I'm rendering just base.html. What may be the problem?


base.html



import traceback; traceback.print_stack() gives this two times:


import traceback; traceback.print_stack()



P.S. I know that I'm querying the DB every time I use CP, I'll add caching later.



Cosole log(that's one page load):


OLD CART USED
CATEGORIES CONTEXT PROCCESOR
[30/Aug/2018 18:56:13] "GET / HTTP/1.1" 200 2651
OLD CART USED
CATEGORIES CONTEXT PROCCESOR
[30/Aug/2018 18:56:13] "GET / HTTP/1.1" 200 2651



View:


class HomePageView(TemplateView):
template_name = 'base.html'



Project URLs:


urlpatterns = [re_path(r'^',include('shopping.urls',namespace='shop'))]



App's URLs:


urlpatterns = [re_path(r'^$',views.HomePageView.as_view(),name='home')]





Did you perhaps added the context processor twice in the settings?
– Willem Van Onsem
Aug 30 at 14:51





Added settings to the question.
– bloodwithmilk
Aug 30 at 14:54





do you have any inclusion_tag or anything else rendering a template (in addition to the one rendered by the view) ?
– bruno desthuilliers
Aug 30 at 15:08





The django debug toolbar and ` 'django.template.context_processors.debug'` are two different things. If you show the full console output that show the context processors being called twice per request, it might indicate what is going on.
– Alasdair
Aug 30 at 15:19





Adding import traceback; traceback.print_stack() to the context processor might also help you debug the issue.
– Alasdair
Aug 30 at 15:27



import traceback; traceback.print_stack()




1 Answer
1



Well, I don't what magic is this but the issue of getting two requests per page load had something to do with this line of code in my base.html:


base.html


<img src="#" width="30" height="30" class="d-inline-block align-top" alt="">



As soon as I deleted it, everything started to work normaly...





The browser exapands the relative url # to http://example.org/#, so the browser makes a request to / to try load the image (the # and anything after it isn't sent to the server).
– Alasdair
Aug 31 at 9:20



#


http://example.org/#


/


#





I see. Thank you for your help:)
– bloodwithmilk
Aug 31 at 11:45



Required, but never shown



Required, but never shown






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ