lostplaces-backend/lostplaces/lostplaces_app/views.py

68 lines
2.2 KiB
Python
Raw Normal View History

2020-07-29 21:28:12 +02:00
from django.shortcuts import render, redirect, get_object_or_404
from django.urls import reverse_lazy
from django.views.generic.edit import CreateView
from django.views import View
from .forms import ExplorerCreationForm, PlaceForm, PlaceImageCreateForm
from .models import Place, PlaceImage
2020-07-26 22:19:37 +02:00
# Create your views here.
class SignUpView(CreateView):
form_class = ExplorerCreationForm
success_url = reverse_lazy('login')
template_name = 'signup.html'
2020-07-26 23:25:07 +02:00
def place_list_view(request,):
2020-07-26 23:34:03 +02:00
return render(request, 'placeList.html', {'place_list':Place.objects.all()})
2020-07-26 23:25:07 +02:00
2020-07-26 22:19:37 +02:00
def place_detail_view(request, pk):
2020-07-26 23:25:07 +02:00
return render(request, 'placeOverview.html', {'place':Place.objects.get(pk=pk)})
2020-07-26 22:19:37 +02:00
def hello_world(request):
return render(request, 'hello_world.html', {'text':'Hello World!'})
class PlaceEditView(View):
2020-07-30 10:51:05 +02:00
def get(self, request, *args, **kwargs):
place_image_form = PlaceImageCreateForm()
2020-07-29 21:28:12 +02:00
if 'pk' in kwargs:
place = get_object_or_404(Place,pk=kwargs['pk'])
place_form = PlaceForm(instance=place)
else:
place_form = PlaceForm()
2020-07-30 10:51:05 +02:00
context = {
'place_form': place_form,
'place_image_form': place_image_form
2020-07-30 10:51:05 +02:00
}
return render(request, 'create_place.html', context)
def post(self, request, *args, **kwargs):
place_form = PlaceForm(request.POST)
if place_form.is_valid():
submitter = request.user
instance = place_form.save(commit=False)
# Save logged in user as "submitted_by"
instance.submitted_by = submitter
instance.save()
if request.FILES:
for image in request.FILES.getlist('filename'):
place_image = PlaceImage.objects.create(
filename=image,
place=instance,
submitted_by=submitter
)
place_image.save()
2020-07-30 10:51:05 +02:00
kwargs_to_pass = {
'pk': instance.pk
}
return redirect(reverse_lazy('place_detail', kwargs=kwargs_to_pass))
else:
2020-07-30 10:51:05 +02:00
context = {
'form': form_place
}
return render(request, 'create_place.html', context)