gpt4 book ai didi

python - django - 当 form.is_valid() 为 false 时如何返回包含所有数据的同一页面

转载 作者:行者123 更新时间:2023-11-28 16:23:28 25 4
gpt4 key购买 nike

我是 django 的新手,我在提交错误后不显示所有数据的表单上苦苦挣扎了很长时间。在我预订健身类(class)的申请中,有一个标题,显示所选训练的数据,还有输入训练参与者数据的字段,还有一个提交按钮。当 form.is_valid() 为 false 时提交后,我想显示同一页面:所选训练的数据、输入到字段的数据以及所有错误填写的字段的验证信息。显示输入到字段和验证信息的数据有效,但显示所选培训的数据无效。我附上打印屏幕以显示这种情况。

截图:

enter image description here

views.py

class BookView(View):
"""
Booking page - getting data of trainings and
posting data of booking to db
"""
template_name = 'club/book.html'
form_class = BookingForm

def post(self, request, training_id):
form = self.form_class(request.POST)
if form.is_valid():
form.save()
messages.success(request, 'Well done! Fitness Class has been booked for you. If you would like to cancel booked fitness class, please contact our office.')
return HttpResponseRedirect('/book/' + training_id)
else:
messages.error(request, 'Oh snap! Fill/correct all fields and try submitting again.')
return render(request, self.template_name, {'form': form})

def get(self, request, training_id):
training = get_object_or_404(Training,pk = training_id)
form = self.form_class(instance=training)
context = {'training': training,
'form': form}
return render(request, self.template_name, context)

book.html

{% extends 'club/base.html' %}

{% block content %}
{% load bootstrap %}
<div>
<h4>Please input your details to book the following Fitness Class</h4>
<br></br>
<div class="training-context">
<label>{{training.name}} {{training.training_date}} {{training.start_time}}</label>
</div>
</div>
<br></br>
<form action=" " method="POST">{% csrf_token %}
<div class="input-group">
{{form.first_name|bootstrap}}
<p></p>
{{form.last_name|bootstrap}}
<p></p>
{{form.email|bootstrap}}
<p></p>
{{form.phone|bootstrap}}
</div>
<input type="hidden" name="training" value={{training.id}}>
<br></br>
<a href=""><button type="submit" class="btn btn-primary">Submit</button></a>
</form>
</div>

{% endblock %}

表单.py

class BookingForm(forms.ModelForm):
class Meta:
model = Booking
fields = '__all__'

模型.py

class Training(models.Model):
"""Class for plan training"""
STATE = (
('A', 'Active'),
('I', 'Inactive'),
)
name = models.ForeignKey('TrnDesc')
instructor = models.ForeignKey('Instructor')
start_time = models.TimeField(blank=True)
end_time = models.TimeField(default='00:00:00')
availability = models.IntegerField(default=15)
state = models.CharField(max_length=1, choices=STATE, default='A')
training_date = models.DateField(default=date.today)
def __str__(self):
return self.name.name

class Booking(models.Model):
"""Data of people which book fitness classes"""
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
email = models.CharField(max_length=50)
phone = models.CharField(max_length=10)
training = models.ForeignKey('Training')
def __str__(self):
return self.training.name.name

urls.py

urlpatterns = [
url(r'^$', HomePageView.as_view(), name='homepage'),
url(r'^book/(?P<training_id>\d+)/$', BookView.as_view(), name='book'),
]

最佳答案

在您的 get 中,您在 post 中使用 context = { 'training': training, 'form': form } 进行渲染>,你只返回 { 'form' : form } 以防出错(当表单无效时)。

您应该以任何一种方式返回 training 对象,因为它始终显示:

if not form.is_valid():
return render(request, self.template_name, { 'training': training, 'form': form })

除此之外,考虑使用 Django 的通用编辑 View ,例如 FormViewCreateView ( https://docs.djangoproject.com/en/1.9/ref/class-based-views/generic-editing/ )。

关于python - django - 当 form.is_valid() 为 false 时如何返回包含所有数据的同一页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38461434/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com