-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
147 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Generated by Django 4.2.13 on 2024-07-02 16:02 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [ | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='Upload', | ||
fields=[ | ||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('uploaded_at', models.DateTimeField(auto_now_add=True)), | ||
('file', models.FileField(upload_to='')), | ||
], | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
from django.db import models | ||
|
||
# Create your models here. | ||
class Upload(models.Model): | ||
uploaded_at = models.DateTimeField(auto_now_add=True) | ||
file = models.FileField() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
{% load static tailwind_tags %} | ||
|
||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> | ||
<link rel="shortcut icon" type="image/png" href="{% static 'images/favicon.png' %}"> | ||
<title>Image Upload</title> | ||
{% tailwind_css %} | ||
</head> | ||
<body> | ||
<div class="container"> | ||
<div class="section"> | ||
<h1 class="is-size-1">Upload Image</h1> | ||
<hr><br> | ||
<form action="{% url "upload" %}" method="post" enctype="multipart/form-data" class="form"> | ||
{% csrf_token %} | ||
<div class="field"> | ||
<div class="control"> | ||
<div class="file"> | ||
<label class="file-label"> | ||
<input class="file-input" type="file" name="image_file"> | ||
<span class="file-cta"> | ||
<span class="file-label">Choose a file...</span> | ||
</span> | ||
</label> | ||
</div> | ||
</div> | ||
</div> | ||
<div class="field"> | ||
<div class="control"> | ||
<label class="radio"> | ||
<input type="radio" name="image_type" value='public' checked> | ||
Public | ||
</label> | ||
<label class="radio"> | ||
<input type="radio" name="image_type" value='private'> | ||
Private | ||
</label> | ||
</div> | ||
</div> | ||
<br> | ||
<div class="field"> | ||
<div class="control"> | ||
<input type="submit" value="Submit" class="button is-primary" /> | ||
</div> | ||
</div> | ||
</form> | ||
<br><br> | ||
{% if image_url %} | ||
<p>File uploaded at: <a href="{{ image_url }}">{{ image_url }}</a></p> | ||
{% endif %} | ||
</div> | ||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,22 @@ | ||
from django.conf import settings | ||
from django.shortcuts import render | ||
from django.core.files.storage import FileSystemStorage | ||
|
||
# Create your views here. | ||
from .models import Upload | ||
|
||
def image_upload(request): | ||
if request.method == 'POST': | ||
image_file = request.FILES['image_file'] | ||
image_type = request.POST['image_type'] | ||
if settings.USE_S3: | ||
upload = Upload(file=image_file) | ||
upload.save() | ||
image_url = upload.file.url | ||
else: | ||
fs = FileSystemStorage() | ||
filename = fs.save(image_file.name, image_file) | ||
image_url = fs.url(filename) | ||
return render(request, 'upload.html', { | ||
'image_url': image_url | ||
}) | ||
return render(request, 'upload.html') |