feat: Add basic image upload (WIP)

This commit is contained in:
moanos [he/him] 2024-03-20 11:38:30 +01:00
parent 68e6d3e299
commit 240ced0374
7 changed files with 33 additions and 5 deletions

View File

@ -2,11 +2,12 @@ from django.contrib import admin
from django.contrib import admin from django.contrib import admin
from .models import Animal, Species, RescueOrganization, AdoptionNotice, Location, Rule from .models import Animal, Species, RescueOrganization, AdoptionNotice, Location, Rule, Image
admin.site.register(Animal) admin.site.register(Animal)
admin.site.register(Species) admin.site.register(Species)
admin.site.register(RescueOrganization) admin.site.register(RescueOrganization)
admin.site.register(Location) admin.site.register(Location)
admin.site.register(AdoptionNotice) admin.site.register(AdoptionNotice)
admin.site.register(Rule) admin.site.register(Rule)
admin.site.register(Image)

View File

@ -17,7 +17,8 @@ class AdoptionNoticeForm(forms.ModelForm):
class AnimalForm(forms.ModelForm): class AnimalForm(forms.ModelForm):
class Meta: class Meta:
model = Animal model = Animal
fields = ['name', "species", "sex", "date_of_birth", "description", "photos"] picture = forms.ImageField(label='Image', required=False)
fields = ['name', "species", "sex", "date_of_birth", "description"]
widgets = { widgets = {
'date_of_birth': DateInput(), 'date_of_birth': DateInput(),
} }

View File

@ -0,0 +1,17 @@
# Generated by Django 5.0.3 on 2024-03-20 10:35
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('fellchensammlung', '0002_rule'),
]
operations = [
migrations.RemoveField(
model_name='image',
name='uploaded_by',
),
]

View File

@ -11,7 +11,6 @@ class Image(models.Model):
title = models.CharField(max_length=200) title = models.CharField(max_length=200)
image = models.ImageField(upload_to='images') image = models.ImageField(upload_to='images')
alt_text = models.TextField(max_length=2000) alt_text = models.TextField(max_length=2000)
uploaded_by = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self): def __str__(self):
return self.title return self.title

View File

@ -7,6 +7,7 @@
<form method = "post" enctype="multipart/form-data"> <form method = "post" enctype="multipart/form-data">
{% csrf_token %} {% csrf_token %}
{{ form.as_p }} {{ form.as_p }}
<input type="file" name="picture">
<button name="button_add_another_animal" type="submit">Speichern und nächstes Tier hinzufügen</button> <button name="button_add_another_animal" type="submit">Speichern und nächstes Tier hinzufügen</button>
<button name="button_save_and_continue" type="submit">Speichern und weiter</button> <button name="button_save_and_continue" type="submit">Speichern und weiter</button>
</form> </form>

View File

@ -3,7 +3,7 @@ from django.http import HttpResponse
from django.urls import reverse from django.urls import reverse
import markdown import markdown
from fellchensammlung.models import AdoptionNotice, MarkdownContent, Animal, Rule from fellchensammlung.models import AdoptionNotice, MarkdownContent, Animal, Rule, Image
from .forms import AdoptionNoticeForm, AnimalForm from .forms import AdoptionNoticeForm, AnimalForm
@ -53,6 +53,12 @@ def add_animal_to_adoption(request, adoption_notice_id):
form.cleaned_data["adoption_notice_id"] = adoption_notice_id form.cleaned_data["adoption_notice_id"] = adoption_notice_id
instance = form.save(commit=False) instance = form.save(commit=False)
instance.adoption_notice_id = adoption_notice_id instance.adoption_notice_id = adoption_notice_id
if 'image' in request.FILES:
image_instance = Image(image=request.FILES['image'])
image_instance.save()
instance.photos.add(image_instance)
instance.save() instance.save()
if "button_add_another_animal" in request.POST: if "button_add_another_animal" in request.POST:
return redirect(reverse("add-animal-to-adoption", args=[str(adoption_notice_id)])) return redirect(reverse("add-animal-to-adoption", args=[str(adoption_notice_id)]))

View File

@ -105,6 +105,9 @@ print(f"Allowed hosts: {ALLOWED_HOSTS}")
# This is adjusted based on this guide https://testdriven.io/blog/django-docker-traefik/ # This is adjusted based on this guide https://testdriven.io/blog/django-docker-traefik/
# compression and caching support (see https://whitenoise.readthedocs.io/en/latest/#quickstart-for-django-apps) # compression and caching support (see https://whitenoise.readthedocs.io/en/latest/#quickstart-for-django-apps)
STORAGES = { STORAGES = {
"default": {
"BACKEND": "django.core.files.storage.FileSystemStorage",
},
"staticfiles": { "staticfiles": {
"BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage", "BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage",
}, },