Поля формы, возвращающие None
У меня есть форма, из которой поля travel_date и max_amt возвращают none после отправки формы из кода шаблона для ref:
forms.py
class GetQuery(forms.Form):
origin = forms.CharField(max_length=30, widget=forms.TextInput(
attrs={'type': 'text', 'id': 'org', 'name': 'origin', 'placeholder': 'origin code'}))
destination = forms.CharField(max_length=30, widget=forms.TextInput(
attrs={'type': 'text', 'id': 'dst', 'name': 'destination', 'placeholder': 'destination code'}))
travel_date = forms.DateField(widget=forms.DateInput(attrs={'type': 'date', 'name': 'date'}))
max_amt = forms.CharField(max_length=6, widget=forms.NumberInput(
attrs={'type': 'number', 'id': 'amt', 'name': 'amount', 'placeholder': 'Enter Price Range'}))
Views.py
def home(request):
if request.method == "POST":
origin, destination, date, amount = request.POST.get('origin'), request.POST.get('destination'), request.POST.get('date'), request.POST.get('amount')
print(origin, destination, date, amount)
IC = AC.objects.all().order_by('-pk')
form = GetQuery
return render(request, 'index.html', context={'IC':IC, 'form': form})
шаблон
<form name="TravelQuery" method="post" novalidate="novalidate">
{% csrf_token %}
<div class="row">
<div class="col-6">
{{ form.origin|as_crispy_field }}
</div>
<div class="col-6">
{{ form.destination|as_crispy_field }}
</div>
<div class="col-6">
{{ form.max_amt|as_crispy_field }}
</div>
</div>
{{ form.travel_date|as_crispy_field }}
<div class="col-md-12 text-right">
<button type="submit" value="submit" class="btn btn-primary primary-btn text-uppercase">Send Message</button>
</div>
</form>
получение значений для пунктов отправления и назначения, но не для travel_date и max_amt.
Измените post на POST в html-форме, и это может быть написано чисто так
def home(request):
if request.method == "POST":
form = GetQuery(request.POST or None):
if form.is_valid():
origin = form.cleaned_data['origin']
destination = form.cleaned_data['destination']
date = form.cleaned_data['date']
amount = form.cleaned_data['amount']
print(origin, destination, date, amount)
IC = AC.objects.all().order_by('-pk')
form = GetQuery
return render(request, 'index.html', context={'IC':IC, 'form': form})
вместо
def home(request):
if request.method == "POST":
origin, destination, date, amount = request.POST.get('origin'), request.POST.get('destination'), request.POST.get('date'), request.POST.get('amount')
print(origin, destination, date, amount)
IC = AC.objects.all().order_by('-pk')
form = GetQuery
return render(request, 'index.html', context={'IC':IC, 'form': form})