Super Sale Limited Time 50% OFF for All-Access Plans
Save 50% Now

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
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • 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
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(
Text formatting options
Submit
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(