Answer the question
In order to leave comments, you need to log in
How can I disable the creation of empty posts in Django?
Greetings!
There is a form on Django, it has 10 fields, of which 5 are required, and if they are not filled in, then pressing the submit button will report that the required fields are not filled, while there is a "back" button next to it, and if you leave the form empty and "return", then an empty record appears in the database, where all required fields are empty.
Why is this happening?
This does not happen with other similar models
Django 2.0.7
Answer the question
In order to leave comments, you need to log in
Make these 5 required fields mandatory at the base level
. And now, without the view code, it is not clear what is there
At the model level, remove all default, blank=True, null=True from the fields.
Then the database itself will not allow you to create a record
class UNew(CreateView):
model = U
fields = '__all__'
def get_form(self, form_class=None):
form = super().get_form(form_class)
form.helper = FormHelper()
form.helper.add_input(Submit('submit', 'Create', css_class='btn-primary'))
form.fields['ZAY_NUM'].label = "Номер заявления"
form.fields['ZAY_TYPE'].label = "Тип заявления"
#- далее описание кучи аналогичных полей
#------ Причина записи в БД
obj = form.save(commit=False)
obj.author += self.request.user.username
obj.save()
return form
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question