validation - Django: fetching data from form fields that takes multiple values -
currently,i building django powered web app displaying filtered data form master table.i have created form user specify his/her choice , depending upon form values data( in tabular form) displayed. problem not able fetch data form fields takes multiple values.i using "request.post.get('fieldname','') fetch data form, multiple values fields , it's displaying last value.
example : if user selecting first, second , fourth option in formfield "modules",
request.post.get('modules','')
is giving me fourth option. hope making myself clear.
views.py
def consisreportgen(request): if request.method == 'post': form = consistencyform(request.post or none) if form.is_valid(): form_user_data = consistencyform(request.post) customer=form.cleaned_data['customer'] components=form.cleaned_data['components'] modules=form.cleaned_data['modules'] customername=request.post.get('customer','') modules_choosen=request.post.get('modules','') bpc_choosen=request.post.get('components','') severity=request.post.get('severity_level','') print(modules_choosen) print(customername) print(severity) print(bpc_choosen) return httpresponseredirect('/consistency/results/') else: form = consistencyform return render(request,'consistency/consistency.html', {'form': form})
forms.py
class consistencyform(forms.form): customer=forms.modelchoicefield( label="customer name", widget=forms.select, queryset=customer.objects.all(), empty_label=none, ) severity_level=forms.modelchoicefield( label="severity level", widget=forms.select, queryset=criticality.objects.all(), empty_label=none, ) query_status=forms.modelchoicefield( widget=forms.select, label="query status", queryset=status.objects.all(), empty_label=none, help_text="</br>" ) modules=forms.modelmultiplechoicefield( label="business module", widget=forms.checkboxselectmultiple, queryset=modules.objects.all(), ) components=forms.modelmultiplechoicefield( widget=forms.checkboxselectmultiple, queryset=component.objects.all(), )
template file
{% extends 'home/base.html' %} {% block title %}consistency report {% endblock %} {% block content %} <form action=" " method="post"> {% csrf_token %} {{ form }} <br /> <input type="submit" value="generate report" /> </form> {% endblock %}
you have use "getlist" method:
request.post.getlist('modules')
see:
Comments
Post a Comment