Django - form action url not redirecting with GET parameters
In my web page I have 3 forms where none, one or all of the form inputs can appear as parameters in the url.
The forms return values for:
- sorting items
- choosing a metric for items
- page number
I use sessions to save all of these parameters, which stops them being overwritten by each other, and then I join them into a url parameter string which is then passed into the templates context.
The problem is when using <form action="{% url watchlist %}{{url_parameter_string}}">
only the parameter from its corresponding form is returned in the url, and does not return all of the parameters stored inside url_parameter_string
.
views.py - watchlist view
if "url_params" in request.session:
for k, v in request.GET.items():
request.session["url_params"][k] = v
else:
request.session["url_params"] = {}
request.session.modified = True
url_param_string = "?" + "".join([f"{k}={v}&"
for k, v in request.session["url_params"].items()])[:-1]
context["url_param_string"] = url_param_string
url_param_string:
?graph-metric=max_price&sort-field=total_quantity-asc&page=1&view=items
html
<form action="{% url view %}{{url_param_string}}" method="GET">
<label for="graph-data-form">Graph Data</label>
<select onchange="this.form.submit()" name="graph-metric">
{% for graph_option in graph_options %}
<option value="{{graph_option.value}}">{{graph_option.text}}</option>
{% endfor %}
</select>
</form>
when the form is submitted
url
.../portfolio/?graph-metric=max_price
All of the other parameters are lost.