Django 为什么form.is\u valid返回false。请指教
我有一个项目,其中一个在线预订房间,然后被引导到结帐页面,在那里你输入一个电话,这是后端所需的。代码如下: forms.pyDjango 为什么form.is\u valid返回false。请指教,django,django-views,django-forms,django-templates,http-post,Django,Django Views,Django Forms,Django Templates,Http Post,我有一个项目,其中一个在线预订房间,然后被引导到结帐页面,在那里你输入一个电话,这是后端所需的。代码如下: forms.py from django import forms class AvailabilityForm(forms.Form): check_in = forms.DateTimeField(input_formats=['%Y-%m-%dT%H:%M'], required=True) check_out = forms.DateTimeField(inpu
from django import forms
class AvailabilityForm(forms.Form):
check_in = forms.DateTimeField(input_formats=['%Y-%m-%dT%H:%M'], required=True)
check_out = forms.DateTimeField(input_formats=['%Y-%m-%dT%H:%M'], required=True)
class PhoneNoForm(forms.Form):
phone_no = forms.IntegerField(required=True)
views.py
class RoomDetailView(View):
def get(self, request, *args, **kwargs):
category = self.kwargs.get('category', None)
got_category = get_room_category(category)
print(category)
print(got_category)
form = AvailabilityForm()
if got_category is not None:
context = {
'category':got_category,
'form':form
}
return render(request, 'detail.html', context)
else:
return HttpResponse("Category Nyet!")
def post(self, request, *args, **kwargs):
category = self.kwargs.get('category', None)
form = AvailabilityForm(request.POST)
if form.is_valid():
data = form.cleaned_data
available_rooms = get_available_rooms(category, data['check_in'], data['check_out'] )
if available_rooms is not None:
room = available_rooms[0]
context = {
'room':room
}
return render(request, 'booking/checkout.html', context)
else:
return HttpResponse("We're out of those rooms" )
else:
return HttpResponse('Form invlid')
class CheckoutView(TemplateView):
template_name = 'booking/checkout.html'
def get_phone(request):
if request.POST:
phone_no = request.POST.get('PhoneNo', None)
print(phone_no)
cl = MpesaClient()
# Use a Safaricom phone number that you have access to, for you to be able to view the prompt
phone_number = phone_no
amount = 1
account_reference = 'reference'
transaction_desc = 'Description'
callback_url = 'https://darajambili.herokuapp.com/express-payment'
# callback_url = request.build_absolute_uri(reverse('booking:mpesa_stk_push_callback'))
response = cl.stk_push(phone_number, amount, account_reference, transaction_desc, callback_url)
return HttpResponse(response.text)
else:
return HttpResponse('This is else')
checkout.html
<div>
<form action="" method="POST">
{% csrf_token %}
<label for="Input No.">Input Phone No. :</label>
<input type="number" name="id_PhoneNo" id="PhoneNo">
<input type="submit" value="Proceed">
</form>
</div>
{%csrf_令牌%}
输入电话号码:
detail.html
<form id="booking-form" action="" method="POST">
{% csrf_token %}
<div class="input-div">
<label for="id_check_in">Check In : </label>
<input type="datetime-local" id="id_check_in" name="check_in">
</div>
<div class="input-div">
<label for="id_check_out">Check Out : </label>
<input type="datetime-local" id="id_check_out" name="check_out">
</div>
<div class="input-div">
<button type="submit">Book the Room</button>
</div>
</form>
{%csrf_令牌%}
登记入住:
退房:
预订房间
form.is\u有效
返回False。我尝试过使用GET方法,但我需要更多处理的电话号码,因此我坚持使用POST。如果可能的话,快帮我。问题在哪里?如果代码太多,很抱歉。您需要将错误返回到HTML页面,以查看错误发生的原因。因此,如果表单无效,请通过以下方式将其发送到HTML页面:
def post(self, request, *args, **kwargs):
category = self.kwargs.get('category', None)
form = AvailabilityForm(request.POST)
if form.is_valid():
...
else:
category = self.kwargs.get('category', None)
got_category = get_room_category(category)
context = {
'category':got_category,
'form':form
}
return render(request, 'detail.html', context)
并按照以下方式呈现错误:
您需要将错误返回到HTML页面,以查看错误发生的原因。因此,如果表单无效,请通过以下方式将其发送到HTML页面:
def post(self, request, *args, **kwargs):
category = self.kwargs.get('category', None)
form = AvailabilityForm(request.POST)
if form.is_valid():
...
else:
category = self.kwargs.get('category', None)
got_category = get_room_category(category)
context = {
'category':got_category,
'form':form
}
return render(request, 'detail.html', context)
并按照以下方式呈现错误:
嘿,谢谢。我按照步骤呈现错误,发现输入格式有问题。除此之外,一旦我在checkout.html中输入了电话号码,在提交时,我将被重定向回detail.html而不是响应页面。可能是什么问题?您没有在CheckOutView中处理post方法。这就是为什么它会重定向到“/“路径可能是他们,谢谢。我按照步骤呈现错误,发现我的输入格式有问题。除此之外,一旦我在checkout.html中输入了电话号码,在提交时,我将被重定向回detail.html而不是响应页面。可能是什么问题?您没有在CheckOutView中处理post方法。这就是为什么会出现这种情况。”是否可能重定向到“/”路径
class RoomDetailView(FormView):
form_class = AvailabilityForm
def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
category = self.kwargs.get('category', None)
got_category = get_room_category(category)
if got_category is not None:
context.update({
'category':got_category,
})
return context
else:
raise Http404("Category does not exist")
def form_valid(self, form):
data = form.cleaned_data
available_rooms = get_available_rooms(category, data['check_in'], data['check_out'] )
if available_rooms is not None: # this part should be in a separate view
room = available_rooms[0]
context = {
'room':room
}
return render(request, 'booking/checkout.html', context)
else:
return HttpResponse("We're out of those rooms" )