I have such a question, i have googled a lot, read about decorators and middleware, but i didnt catch how to solve my question better.
i have base template base.html and templates template1.html and template2.html which are extended of base.html.
base.html has some general block, which is needed in template1.html and template2.html.
this block is with dinamic data, so i have to get data for this block in each view and then render template.
for example i have 2 views :
@render_to("template1.html")
def fun_1(request):
data = getGeneralData()
#...getting other data
return {
"data" : data,
"other" : other_data,
}
@render_to("template2.html")
def fun_2(request):
data = getGeneralData()
#...getting other data
return {
"data" : data,
"other2" : other_data2,
}
So general i need this getGeneralData in all of my views, have i to call getGeneralData() function in every my view or can i make any function which will get general_data and render it to template before any view gets its own data ?
Can you please provide me an example of code or give me a good link how to do it better?
Will recommend you to write your own context processor and return required context data from there.
Example :
custom_context_processor.py
In settings file, update
TEMPLATE_CONTEXT_PROCESSORSto have'custom_context_processor.ctx_getGeneralData'