from django.http import HttpResponseRedirect from django.shortcuts import render from django.views.generic import FormView, UpdateView from django.urls import reverse from idescriptor.forms import ImageForm from idescriptor.models import Image from django.db.models import Q def get_image_for_descriptor(): image = Image.objects.filter(Q(alt_text=None) | Q(title=None)).first() return image def list_images(request): images = Image.objects.all() context = {"images": images} return render(request, 'idescriptor/list_images.html', context=context) class ImageFormView(UpdateView): model = Image form_class = ImageForm template_name = "idescriptor/image-form.html" success_url = "." def form_valid(self, form): return super().form_valid(form) def index(request): """View function for home page of site.""" image_to_describe = get_image_for_descriptor() if request.method == "POST": action = request.POST.get("action") if action == "consume": Image.consume() if action == "add_image_details": form = ImageForm(request.POST, instance=image_to_describe) if form.is_valid(): instance = form.save() print("Didi") return HttpResponseRedirect(reverse("image-update", args=(instance.id,))) else: print("Dodo") else: form = ImageForm(instance=image_to_describe) else: form = ImageForm(instance=image_to_describe) context = {"image_to_describe": image_to_describe, "form": form} return render(request, 'idescriptor/index.html', context=context)