def product(request, pk): obj = get_object_or_404(Product, pk=pk) template_name = 'products/product.html' context = {'obj': obj, 'menu': get_menu()} return render(request, template_name, context)
def product_create(request): title = 'Добавить товар' template_name = 'products/create.html' success_url = reverse_lazy('products:catalog') form = ProductModelForm(request.POST) if request.method == 'POST': if form.is_valid(): form.save() return redirect(success_url) context = {'form': form, 'title': title, 'menu': get_menu()} return render(request, template_name, context)
def catalog(request): title = 'Каталог' template_name = 'products/catalog.html' product_query = get_list_or_404(Product) shuffle(product_query) paginator = Paginator(product_query, 4) page = request.GET.get('page') product_items = paginator.get_page(page) context = { 'product_items': product_items, 'title': title, 'menu': get_menu() } return render(request, template_name, context)
def product_delete(request, pk): title = 'Удалить товар' template_name = 'products/delete.html' success_url = reverse_lazy('products:catalog') obj = get_object_or_404(Product, pk=pk) form = ProductModelForm(instance=obj) if request.method == 'POST': form = ProductModelForm(request.POST, instance=obj) if form.is_valid(): obj.delete() return redirect(success_url) context = {'form': form, 'title': title, 'menu': get_menu()} return render(request, template_name, context)
def category_update(request, pk): title = 'Редактировать категорию' template_name = 'products/create.html' success_url = reverse_lazy('categories:catalog') obj = get_object_or_404(Category, pk=pk) form = CategoryModelForm(instance=obj) if request.method == 'POST': form = CategoryModelForm(request.POST, instance=obj) if form.is_valid(): form.save() return redirect(success_url) context = {'form': form, 'title': title, 'menu': get_menu()} return render(request, template_name, context)
def login_view(request): title = 'Вход' if request.method == 'POST': username = request.POST.get('username') password = request.POST.get('password') user = authenticate(username=username, password=password) if user and user.is_active: login(request, user) else: pass return render(request, 'accounts/login.html', context={ 'menu': get_menu(), 'title': title })