39 lines
1.4 KiB
Python
39 lines
1.4 KiB
Python
from django.shortcuts import render,get_object_or_404,redirect
|
|
from .models import Cocktail,Ingredienti
|
|
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 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")
|