trying to figure out how to serve user-uploaded files
This commit is contained in:
@ -1,5 +1,7 @@
|
||||
from django.shortcuts import render, redirect
|
||||
import os.path
|
||||
from .forms import ImageForm
|
||||
from django.core.files.storage import FileSystemStorage
|
||||
|
||||
def uploadContent(request):
|
||||
if request.method == "GET":
|
||||
@ -15,16 +17,22 @@ def uploadContent(request):
|
||||
else:
|
||||
return
|
||||
|
||||
# Please fix the fact that this shit gets uploaded literally everywhere
|
||||
|
||||
def imageUpload(request):
|
||||
if request.method == "GET":
|
||||
return render(request, "uploadImage.html")
|
||||
elif request.method == "POST":
|
||||
image = request.POST["image"]
|
||||
# add the id of the user, after you add in users
|
||||
savingImage = open(f"static/{image}","wb")
|
||||
for i in image:
|
||||
savingImage.write(i)
|
||||
savingImage.close()
|
||||
return redirect(f"/static/{image}")
|
||||
"""Process images uploaded by users"""
|
||||
if request.method == 'POST':
|
||||
form = ImageForm(request.POST, request.FILES)
|
||||
if form.is_valid():
|
||||
myfile = request.FILES['image']
|
||||
fs = FileSystemStorage(location="./static/") #defaults to MEDIA_ROOT
|
||||
filename = fs.save(myfile.name, myfile)
|
||||
file_url = fs.url(filename)
|
||||
|
||||
return render(request, 'uploadImage.html', {
|
||||
'file_url': file_url,
|
||||
'file_name': myfile.name,
|
||||
})
|
||||
|
||||
else:
|
||||
return
|
||||
form = ImageForm()
|
||||
|
Reference in New Issue
Block a user