Refresh forms. FileField for django forms

I have a model with a file field in it:

class DocumentUpload(models.Model):
    document_name = models.CharField(max_length=100, blank=True)
    document_path = models.FileField(upload_to='uploads')

and form using this model

class DocumentUploadForm(forms.ModelForm):
    class Meta:
        model = DocumentUpload

When I use the form to create a new upload, everything works fine.

    if request.method == 'POST':
        form = DocumentUploadForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()

However, when I try to update / modify a record, it updates all fields except the loaded document. This remains the same as the original download.

d = get_object_or_404(DocumentUpload, pk=id)

if request.method == 'POST':
    form = DocumentUploadForm(data=request.POST, files=request.FILES, instance=d)
    if form.is_valid():
        u = form.save()

How do I change the boot file when editing an instance?

thank

+5
source share
2 answers

Since this was my idea, I will send it as an answer (just to stroke my own ego and / or rating) ...

Add the following to the form template:

enctype="multipart/form-data"

feel free to check this as an answer ...

:)

+5
source

:

enctype="multipart/form-data" 

.

0

All Articles