In the django template is included a Predefined function named "get_context_data".
How can I pass parameters from the view to the template? should I use "context" variable?
def get_context_data(self, **kwargs):
# Call the base implementation first to get a context
context = super().get_context_data(**kwargs)
"""
# Example to get page name. Refer to dashboards/urls.py file.
url_name = resolve(self.request.path_info).url_name
if url_name == "dashboard-2":
# Example to override settings at the runtime
settings.KT_THEME_DIRECTION = "rtl"
else:
settings.KT_THEME_DIRECTION = "ltr"
"""
# A function to init the global layout. It is defined in _keenthemes/__init__.py file
context = KTLayout.init(context)
context["somevar"] = "sample text"
# Include vendors and javascript files for dashboard widgets
KTTheme.addVendors(["amcharts", "amcharts-maps", "amcharts-stock"])
return context
I found this way. Is this the right pattern to follow in the template?
def get(self, request, **kwargs):
context = super().get_context_data(**kwargs)
context = KTLayout.init(context)
context.update({
"somevar": "sample text from backend",
})
return render(request=request, template_name="pages/dashboards/dashboard-2.html", context=context)
Hi
Yes, please use this method to pass parameters to the template.
context.update({
"somevar": "sample text from backend",
})
{{ somevar }}