def post(self, request, *args, **kwargs): if request.user.is_authenticated: form = ReviewForm(loads(request.body.decode('utf-8'))) if form.is_valid(): review = form.save() review.author = request.user.name review.save() return JsonResponse({'result': True}) else: return JsonResponse({'result': False, 'errors': form.errors}) else: return JsonResponse({'result': False, 'authenticate': True})
def add_review(request, id): if not request.user.is_authenticated: messages.info(request, "You need to be logged in in order to give a review") return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path)) if request.method == "POST": form = ReviewForm(request.POST) if form.is_valid(): review = form.save(commit=False) review.product = Product.objects.get(id=id) review.user = request.user review.save() messages.success(request, 'Review saved') return redirect('shop:products_detail', id) else: return redirect('shop:products_detail', id)
def post(self, request, product_id): product = Product.objects.get(pk=product_id) form = ReviewForm(request.POST) if form.is_valid(): review = form.save(commit=False) review.product = product review.user = request.user review.save() return redirect('/') context = { 'form': form, 'product': product } return render(request, 'product.html', context)
def detail(request, slug): product = Product.objects.get(active=True, slug=slug) if request.method == "POST": form = ReviewForm(request.POST) if form.is_valid(): review = form.save(commit=False) review.product = product review.user = request.user review.save() messages.success(request, "Review saved") else: messages.error(request, "Invalid form") else: form = ReviewForm() categories = Category.objects.filter(active=True) context = {"product": product, "categories": categories, "form": form} return render(request, "shop/detail.html", context)
def add_review(request, id): cart_product_form = CartAddProductForm() target_product = get_object_or_404(Product, id=id, available=True) reviews = Review.objects.all().filter(product=target_product) if request.method == "POST": form = ReviewForm(request.POST) if form.is_valid(): review = form.save(commit=False) review.product = target_product review.author = request.user review.published_date = timezone.now() review.save() return render( request, 'shop/product/detail.html', { 'product': target_product, 'cart_product_form': cart_product_form, 'reviews': reviews }) else: form = ReviewForm() return render(request, 'shop/product/add_review.html', {'form': form})
def product_detail(request, id, slug, category_slug=None): category = None categories = Category.objects.all() if category_slug: category = get_object_or_404(Category, slug=category_slug) product = get_object_or_404(Product, id=id, slug=slug, available=True) cart_product_form = CartAddProductForm() # отзыв на продукт reviews = Review.objects.filter(product=id) reviews_count = Review.objects.all() if request.method == "POST": #после отправки данных формы на сервер методом POST form = ReviewForm(request.POST) if form.is_valid(): review_f = form.save(commit=False) review_f.author = request.user review_f.date = datetime.now() review_f.product = Product.objects.get(id=id) review_f.save() return redirect( 'shop:product_detail', id=product.id, slug=product.slug, ) #переадресация на ту же стр else: form = ReviewForm() #создание формы для ввода комментария assert isinstance(request, HttpRequest) return render( request, 'shop/product/detail.html', { 'product': product, 'form': form, 'reviews': reviews, 'reviews_count': reviews_count, 'cart_product_form': cart_product_form, 'category': category, 'categories': categories })
def detail(request, slug): product = Product.objects.get(active=True, slug=slug) if request.method == "POST": form = ReviewForm(request.POST) if form.is_valid(): review = form.save(commit=False) review.product = product review.user = request.user review.save() messages.success(request, "Review saved") else: messages.error(request, "Invalid form") else: form = ReviewForm() categories = Category.objects.filter(active=True) products = Product.objects.filter(active=True) shops = Main_Category.objects.all() sess = request.session.get("data", {"items": []}) cart_products = Product.objects.filter(active=True, slug__in=sess["items"]) cart_products_sum = Product.objects.filter( active=True, slug__in=sess["items"]).aggregate(Sum('price'))['price__sum'] site_data = site_settings.objects.first() context = { "product": product, "categories": categories, "slug": slug, "products": products, "shops": shops, "cart_products_sum": cart_products_sum, "cart_products": cart_products, "site_data": site_data } return render(request, "shop/detail.html", context)