I have a form with multiple select
inputs, as below:
<form method="POST" class="row" action="{% url 'solution_product_list' %}">
{% csrf_token %}
{# main select div: usage or model? #}
<div class="col-md-3 mx-md-5">
<h2 class="h5 nm-text-color fw-bold mb-4">انتخاب بر اساس:</h2>
<select required aria-label="Select usage or model" id="usage_model_select" class="form-select" onchange="set_usage_or_model_dic()">
<option selected>----</option>
<option value="usage">کاربرد</option>
<option value="model">مدل</option>
</select>
</div>
{# usage or model select div #}
<div class="col-md-3 mx-md-5">
{# usage select div #}
<div class="usage visually-hidden" id="usage_div">
<h2 class="h5 nm-text-color fw-bold mb-4">انتخاب کاربرد:</h2>
<select required aria-label="Select usage" class="form-select"
name="usage_select" onchange="set_sub_usage_list()" id="usage_select_id">
<option selected>----</option>
{% for usage in usage_queryset %}
<option value="{{ usage.id }}">{{ usage.usage_name_fa }}</option>
{% endfor %}
</select>
</div>
{# model select div #}
<div class="model visually-hidden" id="model_div">
<h2 class="h5 nm-text-color fw-bold mb-4">انتخاب مدل:</h2>
<select required aria-label="Select model" class="form-select"
name="model_select" onchange="set_pump_type_list()" id="model_select_id">
<option selected>----</option>
{% for model in main_model_queryset %}
<option value="{{ model.id }}">{{ model.model_name_fa }}</option>
{% endfor %}
</select>
</div>
</div>
{# select sub_usage or pump_type div #}
<div class="col-md-3 mx-md-5">
{# sub_usage select div #}
<div class="sub_usage visually-hidden" id="sub_usage_div">
<h2 class="h5 nm-text-color fw-bold mb-4">انتخاب کاربرد جزئی:</h2>
<select required aria-label="Select sub_usage" class="form-select" name="sub_usage_select">
<option selected>همهی کابردهای جزئی</option>
{% for sub_usage in sub_usage_queryset %}
<option value="{{ sub_usage.id }}">{{ sub_usage.sub_usage_name_fa }}</option>
{% endfor %}
</select>
</div>
{# model select div #}
<div class="pump-type visually-hidden" id="pump_type_div">
<h2 class="h5 nm-text-color fw-bold mb-4">انتخاب تیپ:</h2>
<select aria-label="Select pump_type" class="form-select" name="pump_type_select">
<option selected>همهی تیپهای این مدل</option>
{% for pump_type in pump_type_queryset %}
<option value="{{ pump_type.id }}">{{ pump_type.type_name }}</option>
{% endfor %}
</select>
</div>
</div>
<div>
<input type="submit" value="مرحله بعدی" id="submit" class="btn btn-primary">
</div>
</form>
As you can see, the action of my form is "{% url 'solution_product_list' %}"
, but I'd like to send the selected value (from any select inputs that has been selected) to this action url, as it would be {% url 'solution_product_list' selected_value %}
. I did some searches and came accross to these questions which suggested using redirect
:
Django form action url dynamic
How to send selected option value in form action in django
Django : HTML form action directing to view (or url?) with 2 arguments
Dynamic URL routing from html form within action in Django
But none the above helped me, or maybe I could not understand the solutions there. Can anyone please help me on this matter?
Also I have a messy view
for this page, as below:
def solution_product_list(request):
product_list_queryset = None
sub_usage_list = []
pump_type_list = []
if request.method == "POST":
if request.POST["usage_select"] != "----":
usage_select = request.POST["usage_select"]
product_list_queryset = Product.objects.filter(usage__id=usage_select)
sub_usage_list = SubUsage.objects.filter(usage__id=usage_select)
if request.POST["sub_usage_select"] != "همهی کابردهای جزئی":
sub_usage_select = request.POST["sub_usage_select"]
product_list_queryset = Product.objects.filter(
sub_usage__id=sub_usage_select
)
elif request.POST["model_select"] != "----":
model_select = request.POST["model_select"]
product_list_queryset = Product.objects.filter(main_model__id=model_select)
pump_type_list = PumpType.objects.filter(
main_model__id=model_select
)
if request.POST["pump_type_select"] != "همهی تیپهای این مدل":
pump_type_select = request.POST["pump_type_select"]
product_list_queryset = Product.objects.filter(
pump_type__id=pump_type_select
)
elif request.POST["head"] and request.POST["flow"]:
flow = float(request.POST["flow"])
head = float(request.POST["head"])
head_flow_list_main = get_head_flow_list(product_list_queryset)
product_list = return_filtered_products(
head_flow_list_main, head, flow
)
product_list_queryset = Product.objects.filter(id__in=product_list)
page = request.GET.get("page", 1)
paginator = Paginator(product_list_queryset, 10)
try:
product_list = paginator.page(page)
except PageNotAnInteger:
product_list = paginator.page(1)
except EmptyPage:
product_list = paginator.page(paginator.num_pages)
queryset_dictionary = get_common_queryset()
page_context = {
"product_list": product_list_queryset,
"sub_usage_queryset": sub_usage_list,
"pump_type_queryset": pump_type_list,
}
print(product_list_queryset)
context = {
**queryset_dictionary,
**page_context,
}
return render(request, "solutions/second_stage_select.html", context)
I'm relatively new to Django, so if you can give me any advice on better code formatting or writing, your notes will be most appreciated.