How to upload files to the server in Django?

You must implement the uploading of files to the server by the user through a form. The Django documentation on this subject somehow not so much information and I did not have to deal with. If you don't mind, I'd be happy ready a simple example, which I would have processed to fit your needs. Thanks in advance
July 9th 19 at 10:55
4 answers
July 9th 19 at 10:57
Solution
From docs:

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

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


Processed 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})
I read it already, but thanks anyway. Judging from the responses, dear to me in docs, I'll figure it out) - Paige.Macejkovic commented on July 9th 19 at 11:00
: You have not shown your code, it's hard to diagnose something - frit commented on July 9th 19 at 11:03
July 9th 19 at 10:59
Solution
Implemented so suddenly someone come in handy

models.py:
from django.db import models

class Article(models.Model):
 file_obj = models.FileField(upload_to='media/')


views.py:
from django.shortcuts import render
from django.contrib.auth import authenticate
from django.http import HttpResponseRedirect

from .forms import ArticleForm


def Upload_file(request):
 if request.method == 'POST':
 form = ArticleForm(request.POST request.FILES)
 if form.is_valid():
form.save()
 return HttpResponseRedirect('/')
else:
 form = ArticleForm()

 return render(request, 'file_upload.html', {'form': form})


forms.py:

from django.forms import ModelForm
from .models import Article

class ArticleForm(ModelForm):
 class Meta:
 model = Article
 fields = ['file_obj']
July 9th 19 at 11:01
It is not clear how to send or receive a file?....Google did not help?
Rather, it is not clear how to take a file to the server. Google didn't help:( for Django the Internet in General is difficult something to find - Paige.Macejkovic commented on July 9th 19 at 11:04
stupidity - frit commented on July 9th 19 at 11:07
July 9th 19 at 11:03
I would first, did not in Django, and then, only in Django.
not in Django it will be harder - Paige.Macejkovic commented on July 9th 19 at 11:06
The problem is that the default on the Internet can't find, say, PHP would be easier. - frit commented on July 9th 19 at 11:09

Find more questions by tags Django