imageBotDjango/idescriptor/views.py

51 lines
1.5 KiB
Python
Raw Permalink Normal View History

2024-10-21 18:32:47 +00:00
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
2024-10-21 18:43:30 +00:00
def list_images(request):
images = Image.objects.all()
context = {"images": images}
return render(request, 'idescriptor/list_images.html', context=context)
2024-10-21 18:32:47 +00:00
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."""
2024-10-21 21:02:13 +00:00
image_to_describe = Image.get_image_for_descriptor()
2024-10-21 18:32:47 +00:00
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)