I
I
Ilya Pochepko2016-05-27 16:10:58
Django
Ilya Pochepko, 2016-05-27 16:10:58

How to implement file upload to server in Django?

It is necessary to implement uploading files to the server by the user through a form. For some reason, there is not much information in the Django documentation on this topic and it was not enough for me to figure it out. If it doesn’t make it difficult, then I will be happy with a ready-made simple example, which I would already have reworked to fit my needs. Thanks in advance

Answer the question

In order to leave comments, you need to log in

3 answer(s)
Z
zigen, 2016-05-27
@Chepapka

From docs :

# In forms.py...
from django import forms

class UploadFileForm(forms.Form):
    title = forms.CharField(max_length=50)
    file = forms.FileField()

Processing the form:
from django.http import HttpResponseRedirect
from django.shortcuts import render
from .forms import UploadFileForm

def upload_file(request):
    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/success/url/')
       return render(request, 'upload.html', {'form': form})
    else:
        form = UploadFileForm()
    return render(request, 'upload.html', {'form': form})

#
#algooptimize #bottize, 2016-05-27
@user004

It's not clear how to send or receive a file? .... Google did not help?

R
Rou1997, 2016-05-27
@Rou1997

I would first do it not in Django, and then the same way, only in Django.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question