Answer the question
In order to leave comments, you need to log in
SuspiciousFileOperation when uploading a file in Django, how to fix?
Traceback:
Exception Type: SuspiciousFileOperation at /upload/
Exception Value: The joined path (/media/code.cpp) is located outside of the base path component (/home/macheta/PycharmProjects/myproject/media)
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
from django.db import models
class Article(models.Model):
file_obj = models.FileField(upload_to='/media/')
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})
from django.forms import ModelForm
from .models import Article
class ArticleForm(ModelForm):
class Meta:
model = Article
fields = ['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