Home > Net >  Pass variable from template to view django
Pass variable from template to view django

Time:07-08

The main goal is to click a link then have a plot be generated based on the link clicked.

I think if I am able to some how pass a variable(string needed for bar plot) from the link clicked on the template, to the views I can create the plot.

In short, I would click the link this passes a variable to the views then the views would create a plot and pass it to a template.

Any help is appreciated

CodePudding user response:

Just use GET parameters to identify which link was clicked.

<a href="/your/url/?parameter_name=value_01">Show with value_01</a>
<a href="/your/url/?parameter_name=value_02">Show with value_02</a>
<a href="/your/url/?parameter_name=value_03">Show with value_03</a>
def your_view(request):
    if request.GET.get('parameter_name') == 'value_01':
        # first link clicked
    elif request.GET.get('parameter_name') == 'value_02':
        # second link clicked
    elif request.GET.get('parameter_name') == 'value_03':
        # third link clicked
    # ... your code
  • Related