i want to use the dropdown menu in the request POST query, but I cannot define the name for the dropdown menu options, how can I do this? Is there a chance to do if else according to value with POST in this query?
my ex. view;
def list(request):
if 'Storefront' in request.POST:
filtre=Alarmlar.objects.filter( Ekip__contains="Storefront" )
elif 'CAC' in request.POST:
filtre=Alarmlar.objects.filter ( Ekip__contains="Cac")
else:
filtre=Alarmlar.objects.all()
members_list=filtre
paginator = Paginator(members_list, 100)
page = request.GET.get('page')
try:
members = paginator.page(page)
except PageNotAnInteger:
members = paginator.page(1)
except EmptyPage:
members = paginator.page(paginator.num_pages)
return render(request, 'list.html', {'members': members})
html;
<select class="form-control" id="adsoyad" name="adsoyad" style="text-align:center" required>
<option value="" disabled selected >Choose...</option>
<option value="all">ALL</option>
<option value="Cac">Cac</option>
<option value="Storefront">Storefront</option>
</select>
CodePudding user response:
The name is adsoyad
, since you specified <select name="adsoyad" …>
. You thus filter with:
def list(request):
data = request.POST.get('adsoyad')
if data == 'Storefront':
filtre=Alarmlar.objects.filter( Ekip__contains="Storefront" )
elif data == 'Cac':
filtre=Alarmlar.objects.filter ( Ekip__contains="Cac")
else:
filtre=Alarmlar.objects.all()
members_list=filtre
paginator = Paginator(members_list, 100)
page = request.GET.get('page')
try:
members = paginator.page(page)
except PageNotAnInteger:
members = paginator.page(1)
except EmptyPage:
members = paginator.page(paginator.num_pages)
return render(request, 'list.html', {'members': members})