5👍
Interrobang’s answer is correct — variables returned by the controller will be available even in included (as well as extended) views. So, you can do:
In mycontroller.py:
def myfunc():
return dict(caption='Me', source='http://example.com/img.png')
and then in /views/mycontroller/myfunc.html:
{{include 'image.html'}}
In that case, caption
and source
will be available in the image.html view. Instead of returning caption
and source
from the controller, another option is just to define them in the view before the include
directive:
{{caption = 'Me'
source = 'http://example.com/img.png'}}
{{include 'image.html'}}
2👍
From the book:
It is also worth pointing out that the variables returned by the
controller function are available not only in the function’s main
view, but in all of its extended and included views as well.
Unless I’m misunderstanding your question, you don’t have to specifically pass the variables– instead, just use them as normal.
- [Django]-Django ModelForm CheckBox Widget
- [Django]-Django: Direct assignment to the forward side of a many-to-many set is prohibited. Use user.set() instead
0👍
To elaborate on Anthony’s answer,
If you need additional variables passed to the view, just include them in the return dict.
In my current project I pass a whole bunch of variables to be used in the view.
return dict(maxsize=5, message='hello world', fadetimeout=10, warning=0)
Also if you need to access certain values in multiple views in your web, you could store them in the session.
session.some_var_i_need_in_multiple_views = ['one', 'two', 'three']
Then access it in the view:
{{=H3(session.some_var_i_need_in_multiple_views[0])}}