我是django的新手,我正在努力争取更长的时间,表单在提交错误后不会显示所有数据。 在我预订健身课程的应用程序中,有标题显示所选培训的数据,还有输入培训参与者数据的字段,还有一个提交按钮。在form.is_valid()为false时提交后,我想显示相同的页面:选择的培训数据,输入到字段的数据和错误填充的所有字段的验证信息。显示输入到字段和验证信息的数据有效,但显示所选培训的数据不起作用。我附上了版画屏幕以显示这种情况。
截图:
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 %}
forms.py
class BookingForm(forms.ModelForm):
class Meta:
model = Booking
fields = '__all__'
models.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'),
]
答案 0 :(得分:2)
在get
context = { 'training': training, 'form': form }
中post
,{ 'form' : form }
training
if not form.is_valid():
return render(request, self.template_name, { 'training': training, 'form': form })
只有FormView
,如果出现错误(表单无效时),则只返回CreateView
。
您应该以任何方式返回Symbol.scope
个对象,因为它始终显示:
.csv
除此之外,请考虑使用Django的通用编辑视图,例如ifstream read_storage(new_vid_frm_path);
if(!read_storage.is_open()) {
perror("\n\n\n\t\t\t(-)FAIL : Can't Open SavedVideoFrames.txt\n\n\n\t\t\t");
return -1;
}
VideoWriter *vid = new VideoWriter(new_vid_frm_path,CV_FOURCC('P', 'I', 'M', '1'),30,Size(vc.get(CV_CAP_PROP_FRAME_WIDTH),vc.get(CV_CAP_PROP_FRAME_HEIGHT)));
Mat line;
vector<Mat> vid_frms;
while ( getline (read_storage,line) ) {
cout << line << '\n';
}
read_storage.close();
if(vid_frms.size() == 0){
printf("\n\n\n\t\t\t(-)FAIL: Error In Frame\n\n\n\t\t\t");
return -1;
}
for(size_t i = 0; i<vid_frms.size(); i++)
(*vid).write(vid_frms[i]);
printf("\n\n\n\t\t\t(+)SUCCESS: Video Processing Complete \n\n\n\t\t\t ");
或while ( getline (read_storage,line) ) {
cout << line << '\n';
}
(https://docs.djangoproject.com/en/1.9/ref/class-based-views/generic-editing/)。
答案 1 :(得分:1)
您应该使用正确的类来实现您正在实现的功能。您正在数据库中创建实体,因此您应该创建CreateView的子类。完成后,您可以删除大部分逻辑,只需定义get_context_data
即可返回相关的培训对象。