Skip to content

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

Add form to create/edit category

Marcin Gębala2cc6acfcfe66
diff --git a/saleor/dashboard/category/forms.py b/saleor/dashboard/category/forms.py
new file mode 100644
index 0000000000..528b79fc4b
--- /dev/null
+++ b/saleor/dashboard/category/forms.py
@@ -0,0 +1,9 @@
+from django import forms
+
+from ...product.models import Category
+
+
+class CategoryForm(forms.ModelForm):
+ class Meta:
+ model = Category
+ exclude = []
\ No newline at end of file
diff --git a/saleor/dashboard/category/views.py b/saleor/dashboard/category/views.py
index e6d4793c68..5e093b441a 100644
--- a/saleor/dashboard/category/views.py
+++ b/saleor/dashboard/category/views.py
@@ -1,8 +1,10 @@
-from django.shortcuts import get_object_or_404
+from django.contrib import messages
+from django.shortcuts import get_object_or_404, redirect
from django.template.response import TemplateResponse
from django.utils.translation import ugettext_lazy as _
from ...product.models import Category
+from .forms import CategoryForm
def category_list(request):
@@ -18,5 +20,17 @@ def category_details(request, pk=None):
else:
category = Category()
title = _('Add new category')
- ctx = {'category': category, 'title': title}
+ form = CategoryForm(request.POST or None, instance=category)
+ if form.is_valid():
+ form.save()
+ if pk:
+ msg = _('Updated category %s') % category
+ else:
+ msg = _('Added category %s') % category
+ messages.success(request, msg)
+ return redirect('dashboard:categories')
+ else:
+ if form.errors:
+ messages.error(request, _('Failed to save category'))
+ ctx = {'category': category, 'form': form, 'title': title}
return TemplateResponse(request, 'dashboard/category/detail.html', ctx)
diff --git a/saleor/dashboard/templates/dashboard/category/detail.html b/saleor/dashboard/templates/dashboard/category/detail.html
index 602cc59cb4..4db29120bf 100644
--- a/saleor/dashboard/templates/dashboard/category/detail.html
+++ b/saleor/dashboard/templates/dashboard/category/detail.html
@@ -1,4 +1,5 @@
{% extends "dashboard/base.html" %}
+{% load bootstrap %}
{% load i18n %}
{% load mptt_tags %}
@@ -10,4 +11,35 @@
<h1 class="page-header">{{ title }}</h1>
</div>
</div>
-{% endblock %}
+
+ <form class="form form-vertical" method="post" id="category-form" enctype="multipart/form-data">
+ <div class="row">
+ <div class="col-lg-8">
+ <div class="form-message alert alert-success" hidden="hidden">
+ <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
+ <span class="glyphicon glyphicon-ok-sign"></span>
+ <span class="message-content">{% trans "Data saved" %}</span>
+ </div>
+ {% csrf_token %}
+ {{ form|as_vertical_form }}
+ </div>
+ </div>
+ <div class="row">
+ <div class="col-lg-8">
+ <p>
+ {% if category.pk %}
+ <span class="pull-right">
+ <a href="" class="btn btn-danger"><i class="glyphicon glyphicon-trash"></i> {% trans "Delete" %}</a>
+ </span>
+ {% endif %}
+ {% if category.pk %}
+ <button type="submit" class="btn btn-primary"><i class="glyphicon glyphicon-refresh"></i> {% trans "Update" %}</button>
+ {% else %}
+ <button type="submit" class="btn btn-primary"><i class="glyphicon glyphicon-upload"></i> {% trans "Create" %}</button>
+ {% endif %}
+ <a href="{% url 'dashboard:categories' %}" class="btn btn-default"><i class="glyphicon glyphicon-remove"></i> {% trans "Cancel" %}</a>
+ </p>
+ </div>
+ </div>
+ </form>
+{% endblock %}
\ No newline at end of file