Added Form for Image Upload to Place Create View

This commit is contained in:
Leonhard Strohmidel 2020-07-30 13:09:00 +02:00
parent 73a6a4f8d8
commit c8380a0073
5 changed files with 52 additions and 12 deletions

View File

@ -1,6 +1,6 @@
from django import forms from django import forms
from django.contrib.auth.forms import UserCreationForm, UserChangeForm from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from .models import Explorer, Place from .models import Explorer, Place, PlaceImage
class ExplorerCreationForm(UserCreationForm): class ExplorerCreationForm(UserCreationForm):
class Meta: class Meta:
@ -18,3 +18,12 @@ class PlaceForm(forms.ModelForm):
model = Place model = Place
fields = '__all__' fields = '__all__'
exclude = ['submitted_by'] exclude = ['submitted_by']
class PlaceImageCreateForm(forms.ModelForm):
class Meta:
model = PlaceImage
fields = '__all__'
exclude = ['submitted_by', 'place', 'description']
widgets = {
'filename': forms.ClearableFileInput(attrs={'multiple': True})
}

View File

@ -0,0 +1,20 @@
# Generated by Django 3.0.8 on 2020-07-30 09:18
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('lostplaces_app', '0003_auto_20200729_1822'),
]
operations = [
migrations.AddField(
model_name='placeimage',
name='submitted_by',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='images', to=settings.AUTH_USER_MODEL),
),
]

View File

@ -38,6 +38,12 @@ class Place (models.Model):
return self.name return self.name
def generate_image_upload_path(instance, filename):
"""
Callback for generating path for uploaded images
"""
return 'places/' + str(uuid.uuid4())+'.'+filename.split('.')[-1]
class PlaceImage (models.Model): class PlaceImage (models.Model):
SIZES=( SIZES=(
@ -46,17 +52,24 @@ class PlaceImage (models.Model):
{'code': 'large', 'wxh': '1920x1920'} {'code': 'large', 'wxh': '1920x1920'}
) )
description = models.TextField(blank=True)
filename = ImageThumbsField( filename = ImageThumbsField(
upload_to=generate_image_upload_path, upload_to=generate_image_upload_path,
max_length=50, max_length=50,
sizes=self.SIZES sizes=SIZES
) )
place = models.ForeignKey( place = models.ForeignKey(
Place, Place,
on_delete=models.CASCADE, on_delete=models.CASCADE,
related_name='images' related_name='images'
) )
description = models.TextField(blank=True) submitted_by = models.ForeignKey(
Explorer,
on_delete=models.SET_NULL,
null=True,
blank=True,
related_name='images'
)
def __str__(self): def __str__(self):
""" """
@ -65,11 +78,6 @@ class PlaceImage (models.Model):
""" """
return ' '.join([self.place.name, str(self.pk)]) return ' '.join([self.place.name, str(self.pk)])
def generate_image_upload_path(self, filename):
"""
Callback for generating path for uploaded images
"""
return 'places/' + str(uuid.uuid4())+'.'+filename.split('.')[-1]
# These two auto-delete files from filesystem when they are unneeded: # These two auto-delete files from filesystem when they are unneeded:

View File

@ -8,7 +8,8 @@
<h2>Place erstellen</h2> <h2>Place erstellen</h2>
<form method="post"> <form method="post">
{% csrf_token %} {% csrf_token %}
{{ form.as_p }} {{ place_form.as_p }}
{{ place_image_form.as_p }}
<button type="submit">Abschicken</button> <button type="submit">Abschicken</button>
</form> </form>

View File

@ -3,7 +3,7 @@ from django.urls import reverse_lazy
from django.views.generic.edit import CreateView from django.views.generic.edit import CreateView
from django.views import View from django.views import View
from .forms import ExplorerCreationForm, PlaceForm from .forms import ExplorerCreationForm, PlaceForm, PlaceImageCreateForm
from .models import Place from .models import Place
# Create your views here. # Create your views here.
@ -25,13 +25,15 @@ def hello_world(request):
class PlaceEditView(View): class PlaceEditView(View):
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
place_image_form = PlaceImageCreateForm()
if 'pk' in kwargs: if 'pk' in kwargs:
place = get_object_or_404(Place,pk=kwargs['pk']) place = get_object_or_404(Place,pk=kwargs['pk'])
place_form = PlaceForm(instance=place) place_form = PlaceForm(instance=place)
else: else:
place_form = PlaceForm() place_form = PlaceForm()
context = { context = {
'form': place_form 'place_form': place_form,
'place_image_form': place_image_form
} }
return render(request, 'create_place.html', context) return render(request, 'create_place.html', context)