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">
|
<ul class="navbar">
|
||||||
<li class="navbar" ><a class="navbar" href="/">Home</a></li>
|
<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" 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>
|
</ul>
|
||||||
|
|
||||||
<meta name="description" content="Start throwing out trash in an eco-friendly way!">
|
<meta name="description" content="Start throwing out trash in an eco-friendly way!">
|
||||||
|
@ -5,12 +5,17 @@
|
|||||||
{% block content %}
|
{% block content %}
|
||||||
{% if user.is_authenticated %}
|
{% if user.is_authenticated %}
|
||||||
Hi {{ user.username }}!<br>
|
Hi {{ user.username }}!<br>
|
||||||
<form action="" method="POST">{% csrf_token %}
|
<form method="post" enctype="multipart/form-data">
|
||||||
<input type="file" name="image" id="image">
|
{% csrf_token %}
|
||||||
|
{{ form.as_p }}
|
||||||
|
<button type="submit">Upload</button>
|
||||||
|
</form>
|
||||||
|
|
||||||
<button type="submit"> Submit </button>
|
{% if file_url %}
|
||||||
|
<h3>Succesfully uploaded</h3>
|
||||||
{% else %}
|
<img src="{{ file_url }}" alt="connect" style="max-height:300px">
|
||||||
|
{% endif %}
|
||||||
|
{% else %}
|
||||||
<p>You are not logged in</p>
|
<p>You are not logged in</p>
|
||||||
<a href="{% url 'login' %}">Log In</a>
|
<a href="{% url 'login' %}">Log In</a>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
BIN
website/upload/__pycache__/forms.cpython-36.pyc
Normal file
@ -6,4 +6,4 @@ class ImageForm(forms.ModelForm):
|
|||||||
"""Form for the image model"""
|
"""Form for the image model"""
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Image
|
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
|
from django.db import models
|
||||||
|
|
||||||
class Image(models.Model):
|
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
|
from django.shortcuts import render, redirect
|
||||||
import os.path
|
import os.path
|
||||||
|
from .forms import ImageForm
|
||||||
|
from django.core.files.storage import FileSystemStorage
|
||||||
|
|
||||||
def uploadContent(request):
|
def uploadContent(request):
|
||||||
if request.method == "GET":
|
if request.method == "GET":
|
||||||
@ -15,16 +17,22 @@ def uploadContent(request):
|
|||||||
else:
|
else:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# Please fix the fact that this shit gets uploaded literally everywhere
|
||||||
|
|
||||||
def imageUpload(request):
|
def imageUpload(request):
|
||||||
if request.method == "GET":
|
"""Process images uploaded by users"""
|
||||||
return render(request, "uploadImage.html")
|
if request.method == 'POST':
|
||||||
elif request.method == "POST":
|
form = ImageForm(request.POST, request.FILES)
|
||||||
image = request.POST["image"]
|
if form.is_valid():
|
||||||
# add the id of the user, after you add in users
|
myfile = request.FILES['image']
|
||||||
savingImage = open(f"static/{image}","wb")
|
fs = FileSystemStorage(location="./static/") #defaults to MEDIA_ROOT
|
||||||
for i in image:
|
filename = fs.save(myfile.name, myfile)
|
||||||
savingImage.write(i)
|
file_url = fs.url(filename)
|
||||||
savingImage.close()
|
|
||||||
return redirect(f"/static/{image}")
|
return render(request, 'uploadImage.html', {
|
||||||
|
'file_url': file_url,
|
||||||
|
'file_name': myfile.name,
|
||||||
|
})
|
||||||
|
|
||||||
else:
|
else:
|
||||||
return
|
form = ImageForm()
|
||||||
|
@ -124,8 +124,6 @@ USE_TZ = True
|
|||||||
STATIC_URL = '/static/'
|
STATIC_URL = '/static/'
|
||||||
STATIC_ROOT= str(BASE_DIR.joinpath('static'))
|
STATIC_ROOT= str(BASE_DIR.joinpath('static'))
|
||||||
|
|
||||||
MEDIA_URL = '/media/'
|
|
||||||
MEDIA_ROOT = str(BASE_DIR.joinpath('media'))
|
|
||||||
LOGIN_REDIRECT_URL = "/"
|
LOGIN_REDIRECT_URL = "/"
|
||||||
LOGOUT_REDIRECT_URL = "/"
|
LOGOUT_REDIRECT_URL = "/"
|
||||||
|
|
||||||
|
@ -29,4 +29,7 @@ urlpatterns = [
|
|||||||
path('users/', include('users.urls')),
|
path('users/', include('users.urls')),
|
||||||
path("upload/", uploadContent, name="Upload"),
|
path("upload/", uploadContent, name="Upload"),
|
||||||
path("upload_image/", imageUpload, name="Image")
|
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)
|
||||||
|