E
E
Ed10242016-05-23 18:01:38
Django
Ed1024, 2016-05-23 18:01:38

Does not save the file through the form on the html page?

I'm trying to save a file through a form on the page, but after submitting it, I get "This field is required", as if I didn't upload the file. Everything is saved through the admin. I noticed that my if form.is_valid() condition is not included in
views.py

from django.shortcuts import render
from django.contrib.auth import authenticate
from django.http import HttpResponseRedirect

from .form 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, 'blog/file_upload.html', {'form': form})

models.py
from django.db import models

# Create your models here.
class Article(models.Model):
  title = models.CharField(max_length=100)
  text = models.TextField()
  file_obj = models.FileField(upload_to='blog/static/blog/files/')

form.py
from django.forms import ModelForm
from .models import Article

class ArticleForm(ModelForm):
  class Meta:
    model = Article
    fields = ['title', 'text', 'file_obj']

2bdd057ce11a47d3af35bfa38736197c.png

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question