trying to figure out how to serve user-uploaded files
This commit is contained in:
Binary file not shown.
Binary file not shown.
BIN
website/upload/__pycache__/forms.cpython-36.pyc
Normal file
BIN
website/upload/__pycache__/forms.cpython-36.pyc
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -6,4 +6,4 @@ class ImageForm(forms.ModelForm):
|
||||
"""Form for the image model"""
|
||||
class Meta:
|
||||
model = Image
|
||||
fields = ('title', 'image')
|
||||
fields = ('image',)
|
||||
|
21
website/upload/migrations/0001_initial.py
Normal file
21
website/upload/migrations/0001_initial.py
Normal 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/')),
|
||||
],
|
||||
),
|
||||
]
|
18
website/upload/migrations/0002_auto_20210209_1803.py
Normal file
18
website/upload/migrations/0002_auto_20210209_1803.py
Normal 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/'),
|
||||
),
|
||||
]
|
18
website/upload/migrations/0003_auto_20210209_1806.py
Normal file
18
website/upload/migrations/0003_auto_20210209_1806.py
Normal 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/'),
|
||||
),
|
||||
]
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -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/')
|
||||
|
@ -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