Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from django.views import generic
- from django.shortcuts import render, redirect
- from django.contrib.auth import authenticate, login
- from django.views.generic import View
- from .models import Jela, Sastojci
- from .forms import UserForm, DodajRecept
- from django.contrib import messages
- class IndexView(generic.ListView): # pocetna
- template_name = 'recepti/index.html'
- model = Jela
- def param(self):
- tags = self.request.GET.get('tags')
- return tags
- class ReceptiView(generic.ListView):
- template_name = 'recepti/recepti.html'
- context_object_name = 'recepti'
- # because of context_object_name above and how generic.ListView works,
- # the result of this is available in your template as {{ recepti }}
- def get_queryset(self):
- query = Jela.objects \
- .annotate(
- contains_other=Exclude(
- Sastojci.objects.filter(Jaja=Outerref('pk')).exclude(
- Ime__in=('jaja', 'butter')
- )
- )
- ) \
- .filter(contains__other=False)
- return query
- # the : str and -> Jela are type hints, they show what kind of data type the input/output have
- def get_random(self, category: str) -> Jela:
- # we can re-use the base queryset to further filter, and for that we can just call existing methods again
- queryset = self.get_queryset()
- return queryset.filter(kategorija__ime=category).order_by('?').first()
- # this provides all the extra context you want in your template that
- # is not 'automagically' generated by django's generic views
- def get_context_data(self, **kwargs):
- # you need to call the parent method to get all the 'default' stuff from django's generic view
- context = super().get_context_data(**kwargs)
- context.update({
- 'random_dorucak': self.get_random('Doručak'),
- 'random_rucak': self.get_random('Ručak'),
- 'random_vecera': self.get_random('Večera'),
- 'random_uzina': self.get_random('Užina'),
- })
- return context
- class DetailView(generic.DetailView): # detalji recepta
- context_object_name = 'jelo'
- model = Jela
- template_name = 'recepti/detail.html'
- class UserFormView(View):
- form_class = UserForm
- template_name = 'recepti/registration_form.html'
- def get(self, request): # display blank form
- form = self.form_class(None)
- return render(request, self.template_name, {'form': form})
- def post(self, request): # process form data
- form = self.form_class(request.POST)
- if form.is_valid():
- user = form.save(commit=False)
- # cleaned (normalized) data
- username = form.cleaned_data['username']
- password = form.cleaned_data['password']
- user.set_password(password)
- user.save()
- # return User objects if credentials are correct
- user = authenticate(username=username, password=password)
- if user is not None:
- if user.is_active:
- login(request, user)
- return redirect('index')
- return render(request, self.template_name, {'form': form})
- class DodajView(View):
- template_name = 'recepti/dodajrecept.html'
- def get(self, request):
- return render(request, self.template_name, {'form': DodajRecept()})
- def post(self, request):
- if request.method == 'POST':
- form = DodajRecept(request.POST, request.FILES)
- if form.is_valid():
- form.save(commit=True)
- return redirect('index')
- else:
- messages.error(request, form)
- return render(request, self.template_name, {'form': DodajRecept()})
Add Comment
Please, Sign In to add comment