如何在django中使FileField可选?

时间:2022-04-24 19:15:41

I have form with a textbox and filefield in django. It should let the use either paste the text into that box or upload a file. If the user has pasted the text into the box, I needn't check the fileField.

我在django中有一个带有文本框和文件字段的表单。它应该让使用将文本粘贴到该框或上传文件。如果用户已将文本粘贴到框中,我无需检查fileField。

How do I make the forms.FileField() optional?

如何制作forms.FileField()可选?

2 个解决方案

#1


38  

If you're using a forms.FileField() in a forms.Form derived class, you can set:

如果您在forms.Form派生类中使用forms.FileField(),则可以设置:

class form(forms.Form):
    file = forms.FileField(required=False)

If you're using a models.FileField() and have a forms.ModelForm assigned to that model, you can use

如果您正在使用models.FileField()并将forms.ModelForm分配给该模型,则可以使用

class amodel(models.Model):
    file = models.FileField(blank=True, null=True)

Which you use depends on how you are deriving the form and if you are using the underlying ORM (i.e. a model).

您使用哪种方法取决于您如何派生表格以及您是否使用基础ORM(即模型)。

#2


0  

if you want to do this before the user submits the form you will need to do so using javascript(jquery, mootools, etc all offer some quick methods for that)

如果你想在用户提交表单之前这样做,你需要使用javascript(jquery,mootools等都提供一些快速的方法)

on the django side you could do so in a clean method in the form. This should get you started and you will need to display those validation errors on your template for the user to see them. The name of the clean method must match the form field name with "clean_" prepended.

在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

on the template

在模板上

{% if form.errors %}
  {{form.non_field_errors}}
  {% if not form.non_field_errors %}
    {{form.errors}}
  {% endif %}
{% endif %}

#1


38  

If you're using a forms.FileField() in a forms.Form derived class, you can set:

如果您在forms.Form派生类中使用forms.FileField(),则可以设置:

class form(forms.Form):
    file = forms.FileField(required=False)

If you're using a models.FileField() and have a forms.ModelForm assigned to that model, you can use

如果您正在使用models.FileField()并将forms.ModelForm分配给该模型,则可以使用

class amodel(models.Model):
    file = models.FileField(blank=True, null=True)

Which you use depends on how you are deriving the form and if you are using the underlying ORM (i.e. a model).

您使用哪种方法取决于您如何派生表格以及您是否使用基础ORM(即模型)。

#2


0  

if you want to do this before the user submits the form you will need to do so using javascript(jquery, mootools, etc all offer some quick methods for that)

如果你想在用户提交表单之前这样做,你需要使用javascript(jquery,mootools等都提供一些快速的方法)

on the django side you could do so in a clean method in the form. This should get you started and you will need to display those validation errors on your template for the user to see them. The name of the clean method must match the form field name with "clean_" prepended.

在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

on the template

在模板上

{% if form.errors %}
  {{form.non_field_errors}}
  {% if not form.non_field_errors %}
    {{form.errors}}
  {% endif %}
{% endif %}