Answer the question
In order to leave comments, you need to log in
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})
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/')
from django.forms import ModelForm
from .models import Article
class ArticleForm(ModelForm):
class Meta:
model = Article
fields = ['title', 'text', 'file_obj']
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question