lostplaces-backend/lostplaces/lostplaces_app/views.py
2020-07-30 13:09:00 +02:00

57 lines
1.9 KiB
Python

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
# Create your views here.
class SignUpView(CreateView):
form_class = ExplorerCreationForm
success_url = reverse_lazy('login')
template_name = 'signup.html'
def place_list_view(request,):
return render(request, 'placeList.html', {'place_list':Place.objects.all()})
def place_detail_view(request, pk):
return render(request, 'placeOverview.html', {'place':Place.objects.get(pk=pk)})
def hello_world(request):
return render(request, 'hello_world.html', {'text':'Hello World!'})
class PlaceEditView(View):
def get(self, request, *args, **kwargs):
place_image_form = PlaceImageCreateForm()
if 'pk' in kwargs:
place = get_object_or_404(Place,pk=kwargs['pk'])
place_form = PlaceForm(instance=place)
else:
place_form = PlaceForm()
context = {
'place_form': place_form,
'place_image_form': place_image_form
}
return render(request, 'create_place.html', context)
def post(self, request, *args, **kwargs):
place_form = PlaceForm(request.POST)
if place_form.is_valid() == True:
instance = place_form.save(commit=False)
# Save logged in user as "submitted_by"
instance.submitted_by = request.user
instance.save()
kwargs_to_pass = {
'pk': instance.pk
}
return redirect(reverse_lazy('place_detail', kwargs=kwargs_to_pass))
else:
context = {
'form': form_place
}
return render(request, 'create_place.html', context)