Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Add AddToCartForm in the product view.

Michal Ociepka46519b9eda6a
diff --git a/product/forms.py b/product/forms.py
new file mode 100644
index 0000000000..c8e0012aef
--- /dev/null
+++ b/product/forms.py
@@ -0,0 +1,5 @@
+from cart.forms import AddToCartForm
+
+class ProductForm(AddToCartForm):
+
+ pass
diff --git a/product/templates/product/details.html b/product/templates/product/details.html
index f6a60ccf2d..832827cce9 100644
--- a/product/templates/product/details.html
+++ b/product/templates/product/details.html
@@ -1,5 +1,30 @@
{% extends "base.html" %}
+{% load i18n %}
+{% load gross from prices %}
{% block content %}
-asd
+<h1>{{ product }}</h1>
+<p>Price: {% gross product.price %}</p>
+<form class="form-horizontal" method="post" action=".">
+ {% csrf_token %}
+ <div class="control-group {{ form.quantity.html_name }}{{ form.quantity.html_name }}{{ form.quantity.html_name }}{% if form.quantity.errors %} error{% endif %}{% if form.quantity.field.required %} required{% endif %}">
+ <label class="control-label" for="inputEmail">{{ form.quantity.label }}</label>
+ <div class="controls">
+ {{ form.quantity }}
+ {% if form.quantity.errors %}
+ {% for error in form.quantity.errors %}
+ <span class="help-inline error">{{ error }}</span>
+ {% endfor %}
+ {% endif %}
+ </div>
+ </div>
+
+ <div class="control-group">
+ <div class="controls">
+ <button class="btn">
+ {% trans "Add to cart" %}
+ </button>
+ </div>
+ </div>
+</form>
{% endblock content %}
diff --git a/product/views.py b/product/views.py
index 40694b98aa..f398f0f3c5 100644
--- a/product/views.py
+++ b/product/views.py
@@ -1,4 +1,6 @@
from .models import Product
+from .forms import ProductForm
+from cart.models import Cart, CartItem
from django.http import HttpResponsePermanentRedirect, Http404
from django.template.response import TemplateResponse
@@ -17,9 +19,17 @@ def details(request, slug, product_id):
except Product.DoesNotExist:
return Http404()
+ cart = Cart.objects.get(id=1)
+
+ form = ProductForm(cart, product, request.POST or None)
+
+ if form.is_valid():
+ form.save()
+
if product.get_slug() != slug:
return HttpResponsePermanentRedirect(product.get_absolute_url())
return TemplateResponse(request, 'product/details.html', {
- 'product': product
+ 'product': product,
+ 'form': form
})
diff --git a/saleor/settings.py b/saleor/settings.py
index 23b899245a..6d2ad75c1e 100644
--- a/saleor/settings.py
+++ b/saleor/settings.py
@@ -82,10 +82,11 @@ INSTALLED_APPS = (
# External apps
'south',
- 'mptt',
-
+ 'django_prices',
+ 'mptt',
# Local apps
'product',
+ 'cart',
)
LOGGING = {