Get 2024 Templates Mega Bundle!$1000 worth of 19 Bootstrap HTML, Vue & React Templates + 3 Vector Sets for just $99
Get for 99$

Django - Metronic - How to pass parameters using the default classview?


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


Please provide me a sample code or some documentation what is the pattern to use accordingly to the standards of the template you provided us for Django.


Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(

Replies (2)


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",
})



In the template file dashboard-2.html, you can call it using the variable "somevar". For example;

{{ somevar }}


Thanks


Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(
Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(