trying to figure out how to serve user-uploaded files
BIN
website/static/2021/02/09/Screenshot_at_2021-01-19_03-15-00.png
Normal file
After Width: | Height: | Size: 240 KiB |
BIN
website/static/Screenshot at 2021-01-19 03-15-00_5jYNLfq.png
Normal file
After Width: | Height: | Size: 240 KiB |
BIN
website/static/Screenshot at 2021-01-19 03-15-00_HzSfOyh.png
Normal file
After Width: | Height: | Size: 240 KiB |
BIN
website/static/Screenshot at 2021-01-19 03-15-00_Wukng9o.png
Normal file
After Width: | Height: | Size: 240 KiB |
BIN
website/static/Screenshot at 2021-01-19 03-15-00_p1gRbJ7.png
Normal file
After Width: | Height: | Size: 240 KiB |
BIN
website/static/Screenshot_at_2021-01-19_03-15-00.png
Normal file
After Width: | Height: | Size: 240 KiB |
BIN
website/static/Screenshot_at_2021-01-19_03-15-00_nSTujQE.png
Normal file
After Width: | Height: | Size: 240 KiB |
BIN
website/static/Screenshot_at_2021-01-19_03-15-00_nnpFyZr.png
Normal file
After Width: | Height: | Size: 240 KiB |
@ -9,7 +9,7 @@
|
||||
<ul class="navbar">
|
||||
<li class="navbar" ><a class="navbar" href="/">Home</a></li>
|
||||
<li class="navbar"><a class="navbar" href="/users/signup">Sign up</a></li>
|
||||
<li class="navbar"><a class="navbar" id="login" href="/users/login"style="float:right">Login</a></li>
|
||||
<li class="navbar"><a class="navbar" id="login" href="/users/login">Login</a></li>
|
||||
</ul>
|
||||
|
||||
<meta name="description" content="Start throwing out trash in an eco-friendly way!">
|
||||
|
@ -5,11 +5,16 @@
|
||||
{% block content %}
|
||||
{% if user.is_authenticated %}
|
||||
Hi {{ user.username }}!<br>
|
||||
<form action="" method="POST">{% csrf_token %}
|
||||
<input type="file" name="image" id="image">
|
||||
|
||||
<button type="submit"> Submit </button>
|
||||
<form method="post" enctype="multipart/form-data">
|
||||
{% csrf_token %}
|
||||
{{ form.as_p }}
|
||||
<button type="submit">Upload</button>
|
||||
</form>
|
||||
|
||||
{% if file_url %}
|
||||
<h3>Succesfully uploaded</h3>
|
||||
<img src="{{ file_url }}" alt="connect" style="max-height:300px">
|
||||
{% endif %}
|
||||
{% else %}
|
||||
<p>You are not logged in</p>
|
||||
<a href="{% url 'login' %}">Log In</a>
|
||||
|
BIN
website/upload/__pycache__/forms.cpython-36.pyc
Normal file
@ -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
@ -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
@ -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
@ -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/'),
|
||||
),
|
||||
]
|
@ -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()
|
||||
|
@ -124,8 +124,6 @@ USE_TZ = True
|
||||
STATIC_URL = '/static/'
|
||||
STATIC_ROOT= str(BASE_DIR.joinpath('static'))
|
||||
|
||||
MEDIA_URL = '/media/'
|
||||
MEDIA_ROOT = str(BASE_DIR.joinpath('media'))
|
||||
LOGIN_REDIRECT_URL = "/"
|
||||
LOGOUT_REDIRECT_URL = "/"
|
||||
|
||||
|
@ -29,4 +29,7 @@ urlpatterns = [
|
||||
path('users/', include('users.urls')),
|
||||
path("upload/", uploadContent, name="Upload"),
|
||||
path("upload_image/", imageUpload, name="Image")
|
||||
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
|
||||
]
|
||||
if settings.DEBUG:
|
||||
urlpatterns += static(settings.STATIC_URL,
|
||||
document_root=settings.STATIC_ROOT)
|
||||
|