from django.contrib.auth.models import User from django.http import JsonResponse from django.urls import reverse_lazy from .models import Product, Manufacturer from django.views import generic from django.shortcuts import render from django.contrib.auth.views import LogoutView from django.views.generic import CreateView from .forms import RegisterUserForm class ProductDetailView(generic.DetailView): model = Product class ProductListView(generic.ListView): model = Product def index(request): num_product=Product.objects.all().count() num_manufacturer=Manufacturer.objects.count() return render( request, 'index.html', context={'num_product':num_product, 'num_manufacturer':num_manufacturer}, ) def login(request): return render(request, 'registration/login.html') def registration(request): return render(request, 'registration.html') def logout(request): return render(request, 'logout.html') class RegisterView(CreateView): template_name = 'registration.html' form_class = RegisterUserForm success_url = reverse_lazy('login') def validate_username(request): username = request.GET.get('username', None) response = { 'is_taken': User.objects.filter(username__iexact=username).exists() } return JsonResponse(response) class BBLogoutView(LogoutView): template_name = 'logout.html' success_url = reverse_lazy('index')