trying to figure out how to serve user-uploaded files

This commit is contained in:
Yamozha
2021-02-09 18:19:42 +02:00
parent d1280445f4
commit dfda536aab
39 changed files with 93 additions and 22 deletions

Binary file not shown.

View File

@ -6,4 +6,4 @@ class ImageForm(forms.ModelForm):
"""Form for the image model"""
class Meta:
model = Image
fields = ('title', 'image')
fields = ('image',)

View File

@ -0,0 +1,21 @@
# Generated by Django 3.1.6 on 2021-02-09 15:56
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Image',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('image', models.ImageField(blank=True, upload_to='static/%Y/%m/%d/')),
],
),
]

View File

@ -0,0 +1,18 @@
# Generated by Django 3.1.6 on 2021-02-09 16:03
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('upload', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='image',
name='image',
field=models.FileField(upload_to='../static/%Y/%m/%d/'),
),
]

View File

@ -0,0 +1,18 @@
# Generated by Django 3.1.6 on 2021-02-09 16:06
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('upload', '0002_auto_20210209_1803'),
]
operations = [
migrations.AlterField(
model_name='image',
name='image',
field=models.FileField(upload_to='static/'),
),
]

View File

@ -1,4 +1,4 @@
from django.db import models
class Image(models.Model):
image = models.ImageField(upload_to='static/%Y/%m/%d/', blank=True)
image = models.FileField(upload_to='static/')

View File

@ -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()