from django.shortcuts import render,get_object_or_404,redirect from .models import Cocktail,Ingredienti,Strumenti,Bicchieri from .forms import CocktailForm import random # Create your views here. def homepage(request): lista_cocktail = Cocktail.objects.all() context = {"lista_cocktail":lista_cocktail} return render(request,"homepage.html",context) def cocktail(request,nome_cocktail): cocktail = get_object_or_404(Cocktail,nome_cocktail=nome_cocktail) context = {"cocktail":cocktail} return render(request,"cocktail.html",context) def random_cocktail(request): random_cocktail = Cocktail.objects.all() number_of_cocktail = Cocktail.objects.all().count() random_cocktail_list = random.randint(1,number_of_cocktail) rand_cocktail = random.sample(list(random_cocktail),random_cocktail_list) context = {"random_cocktail":rand_cocktail} return render(request,"random_cocktail.html",context) def strumenti_cocktail(request): lista_strumenti = Strumenti.objects.all() context = {"lista_strumenti":lista_strumenti} return render(request,"strumenti.html",context) # def strumento_views(request,nome_strumento): # strumento = get_object_or_404(Strumenti,nome_strumento=nome_strumento) # context = {"strumento":strumento} # return render(request,"strumento_detail.html",context) def bicchieri_cocktail(request): lista_bicchieri = Bicchieri.objects.all() context = {"lista_bicchieri":lista_bicchieri} return render(request,"bicchieri.html",context) # def bicchiere_views(request,nome_bicchiere): # bicchiere = get_object_or_404(Strumenti,nome_bicchiere=nome_bicchiere) # context = {"bicchiere":bicchiere} # return render(request,"bicchiere_detail.html",context) # def cerca(request): # if "q" in request.GET: # querystring = request.GET.get("q") # if len(querystring) == 0: # return redirect("cerca") # ingrediente = Cocktail.objects.filter(ingredienti__ingrediente__icontains=querystring).order_by("-nome_cocktail") # cocktail = Cocktail.objects.filter(nome_cocktail__icontains=querystring).order_by("-nome_cocktail") # context = {"querystring":querystring,"cocktail":cocktail,"ingrediente":ingrediente} # return render(request,"cerca.html",context) # else: # return render(request,"cerca.html") def cerca(request): if "q" in request.GET: querystring = request.GET.get("q") if len(querystring) < 3: return redirect("cerca") all_ingredients = Cocktail.objects.all() searching_ingred = Cocktail.objects.none() for one_ingredient in querystring.split(","): searching_ingred = searching_ingred.union(searching_ingred, all_ingredients.filter(ingredienti__ingrediente__icontains=one_ingredient)) # ingrediente = Cocktail.objects.filter(ingredienti__ingrediente__icontains=querystring).order_by("-nome_cocktail") cocktail = Cocktail.objects.filter(nome_cocktail__icontains=querystring).order_by("-nome_cocktail") context = {"querystring":querystring,"cocktail":cocktail,"ingrediente":searching_ingred} return render(request,"cerca.html",context) else: return render(request,"cerca.html") def createCocktail(request): if request.method == "POST": form = CocktailForm(request.POST,request.FILES) if form.is_valid(): cocktail = form.save() return redirect("cocktail", nome_cocktail=cocktail.nome_cocktail) else: form = CocktailForm() context = {"form":form} return render(request,"crea_cocktail.html",context)