[Fixed]-How should I allow a FileField not need to upload a new file when the existed in Django?

1👍

As following the previous comments, maybe like this;

1. forms.py

class AssignmentForm(forms.ModelForm):
    # as following @Rohan, to make it optional.
    content = forms.FileField(required=False)

    class Meta:
        model = Assignment
        fields = ['content', 'other_val']

2. yourtemplate.html

<form method="post" enctype="multipart/form-data" action=".">
  {% csrf_token %}
  {{ form.as_p }}
  <button type="submit">Save</button>
</form>

<script> 
{% if not form.content.value %}
  $('#id_content').attr({'required': 'required'});
{% endif %}
</script>

The field of content is under required only if havn’t value before…

👤binpy

Leave a comment