Как отображать сообщения об ошибках в формах django как «Это поле обязательно», если форма отправлена пустой.
Это мой models.py
class Student(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
image = models.ImageField(upload_to='student/images')
classes = models.CharField(max_length=10, choices=classes)
roll = models.CharField(max_length=20)
gender = models.CharField(max_length=10, choices=gender)
dob = models.DateField()
blood_group = models.CharField(max_length=5, choices=blood_group, blank=True)
phone = models.CharField(max_length=15)
address = models.CharField(max_length=25)
status = models.BooleanField(default=True)
Это мой forms.py
class StudentForm(forms.ModelForm):
class Meta:
model = User
fields = ['first_name', 'last_name', 'username', 'email', 'password']
def __init__(self, *args, **kwargs):
super(StudentForm, self).__init__(*args, **kwargs)
for visible in self.visible_fields():
visible.field.widget.attrs['class'] = 'form-control'
class StudentExtraForm(forms.ModelForm):
class Meta:
model = models.Student
fields = ['classes', 'roll', 'blood_group', 'phone', 'dob', 'gender', 'address', 'status', 'image']
def __init__(self, *args, **kwargs):
super(StudentExtraForm, self).__init__(*args, **kwargs)
for visible in self.visible_fields():
visible.field.widget.attrs['class'] = 'form-control'
Это форма. html
<form class="forms-sample" action="{% url 'student:add_student' %}" method="POST"
enctype="multipart/form-data">
{% csrf_token %}
{{ form1.non_field_errors }}
{{ form2.non_field_errors }}
<div class="form-group">
{{ form1.first_name.errors }}
<label for="{{ form1.first_name.id_for_label }}">First Name:</label>
{{ form1.first_name }}
</div>
<div class="form-group">
{{ form1.last_name.errors }}
<label for="{{ form1.last_name.id_for_label }}">Last Name:</label>
{{ form1.last_name }}
</div>
<div class="form-group">
{{ form1.username.errors }}
<label for="{{ form1.username.id_for_label }}">Username:</label>
{{ form1.username }}
</div>
<div class="form-group">
{{ form1.email.errors }}
<label for="{{ form1.email.id_for_label }}">Email:</label>
{{ form1.email }}
</div>
<div class="form-group">
{{ form1.password.errors }}
<label for="{{ form1.password.id_for_label }}">Password:</label>
{{ form1.password }}
</div>
<div class="form-group">
{{ form2.classes.errors }}
<label for="{{ form2.classes.id_for_label }}">Select Class:</label>
{{ form2.classes }}
</div>
<div class="form-group">
{{ form2.roll.errors }}
<label for="{{ form2.roll.id_for_label }}">Roll No:</label>
{{ form2.roll }}
</div>
<div class="form-group">
{{ form2.blood_group.errors }}
<label for="{{ form2.blood_group.id_for_label }}">Select Blood Group:</label>
{{ form2.blood_group }}
</div>
<div class="form-group">
{{ form2.dob.errors }}
<label for="{{ form2.dob.id_for_label }}">Date of birth:</label>
{{ form2.dob }}
</div>
<div class="form-group">
{{ form2.image.errors }}
<label for="{{ form2.image.id_for_label }}">Upload Image:</label>
{{ form2.image }}
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
А это мой views.py
def add_student(request):
form1 = forms.StudentForm()
form2 = forms.StudentExtraForm()
context = {
'form1': form1,
'form2': form2
}
if request.method == 'POST':
form1 = forms.StudentForm(request.POST)
form2 = forms.StudentExtraForm(request.POST, request.FILES)
if form1.is_valid() and form2.is_valid():
user = form1.save()
user.set_password(user.password)
user.save()
f2 = form2.save(commit=False)
f2.user = user
f2.status = True
user2 = f2.save()
student_user_group = Group.objects.get_or_create(name='Student')
student_user_group[0].user_set.add(user)
return redirect('student:student_index')
return render(request, 'Student/add_student.html', context=context)
Когда я отправляю форму без ее заполнения, она не показывает сообщение об ошибке и перенаправляет на страницу индекса. Я хочу отобразить сообщение, если какое-либо поле ввода не заполнено.