, если вы хотите сделать это до того, как пользователь отправит форму, которую вам нужно будет сделать с помощью javascript (jquery, mootools и т. Д. Все предлагают несколько быстрых методов для этого)
на стороне django вы можете сделать это чистым способом в форме. Это должно помочь вам начать, и вам нужно будет отобразить эти ошибки проверки в вашем шаблоне, чтобы пользователь мог их увидеть. Имя метода clean должно совпадать с именем поля формы с добавленным «clean_».
def clean_textBoxFieldName(self):
textInput = self.cleaned_data.get('textBoxFieldName')
fileInput = self.cleaned_data.get('fileFieldName')
if not textInput and not fileInput:
raise ValidationError("You must use the file input box if not entering the full path.")
return textInput
def clean_fileFieldName(self):
fileInput = self.cleaned_data.get('fileFieldName')
textInput = self.cleaned_data.get('textBoxFieldName')
if not fileInput and not textInput:
raise ValidationError("You must provide the file input if not entering the full path")
return fileInput
по шаблону
{% if form.errors %}
{{form.non_field_errors}}
{% if not form.non_field_errors %}
{{form.errors}}
{% endif %}
{% endif %}