Skip to content

Commit edd39e1

Browse files
committed
Added Categories and Sub Categories pages
1 parent 86f2aa7 commit edd39e1

31 files changed

+441
-14
lines changed
418 Bytes
Binary file not shown.

DjangoEcommerce/urls.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,17 @@
2929
path('admin_logout_process',views.adminLogoutProcess,name="admin_logout_process"),
3030

3131
# PAGE FOR ADMIN
32-
path('admin_home',AdminViews.admin_home,name="admin_home")
32+
path('admin_home',AdminViews.admin_home,name="admin_home"),
33+
34+
#CATEGORIES
35+
path('category_list',AdminViews.CategoriesListView.as_view(),name="category_list"),
36+
path('category_create',AdminViews.CategoriesCreate.as_view(),name="category_create"),
37+
path('category_update/<slug:pk>',AdminViews.CategoriesUpdate.as_view(),name="category_update"),
38+
39+
#SUBCATEGORIES
40+
41+
path('sub_category_list',AdminViews.SubCategoriesListView.as_view(),name="sub_category_list"),
42+
path('sub_category_create',AdminViews.SubCategoriesCreate.as_view(),name="sub_category_create"),
43+
path('sub_category_update/<slug:pk>',AdminViews.SubCategoriesUpdate.as_view(),name="sub_category_update"),
44+
3345
]+static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)+static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)

DjangoEcommerceApp/AdminViews.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,43 @@
11
from django.shortcuts import render
22
from django.contrib.auth.decorators import login_required
3+
from django.views.generic import ListView,CreateView,UpdateView,DetailView
4+
from DjangoEcommerceApp.models import Categories,SubCategories
5+
from django.contrib.messages.views import SuccessMessageMixin
36

47
@login_required(login_url="/admin/")
58
def admin_home(request):
6-
return render(request,"admin_templates/home.html")
9+
return render(request,"admin_templates/home.html")
10+
11+
class CategoriesListView(ListView):
12+
model=Categories
13+
template_name="admin_templates/category_list.html"
14+
15+
class CategoriesCreate(SuccessMessageMixin,CreateView):
16+
model=Categories
17+
success_message="Category Added!"
18+
fields="__all__"
19+
template_name="admin_templates/category_create.html"
20+
21+
class CategoriesUpdate(SuccessMessageMixin,UpdateView):
22+
model=Categories
23+
success_message="Category Updated!"
24+
fields="__all__"
25+
template_name="admin_templates/category_update.html"
26+
27+
28+
class SubCategoriesListView(ListView):
29+
model=SubCategories
30+
template_name="admin_templates/sub_category_list.html"
31+
32+
class SubCategoriesCreate(SuccessMessageMixin,CreateView):
33+
model=SubCategories
34+
success_message="Sub Category Added!"
35+
fields="__all__"
36+
template_name="admin_templates/sub_category_create.html"
37+
38+
class SubCategoriesUpdate(SuccessMessageMixin,UpdateView):
39+
model=SubCategories
40+
success_message="Sub Category Updated!"
41+
fields="__all__"
42+
template_name="admin_templates/sub_category_update.html"
43+
Binary file not shown.
Binary file not shown.

DjangoEcommerceApp/models.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from django.contrib.auth.models import AbstractUser
33
from django.dispatch import receiver
44
from django.db.models.signals import post_save
5+
from django.urls import reverse
56

67
# Create your models here.
78
class CustomUser(AbstractUser):
@@ -43,6 +44,14 @@ class Categories(models.Model):
4344
created_at=models.DateTimeField(auto_now_add=True)
4445
is_active=models.IntegerField(default=1)
4546

47+
def get_absolute_url(self):
48+
return reverse("category_list")
49+
50+
def __str__(self):
51+
return self.title
52+
53+
54+
4655

4756
class SubCategories(models.Model):
4857
id=models.AutoField(primary_key=True)
@@ -54,6 +63,10 @@ class SubCategories(models.Model):
5463
created_at=models.DateTimeField(auto_now_add=True)
5564
is_active=models.IntegerField(default=1)
5665

66+
def get_absolute_url(self):
67+
return reverse("sub_category_list")
68+
69+
5770

5871
class Products(models.Model):
5972
id=models.AutoField(primary_key=True)

DjangoEcommerceApp/templates/admin_templates/base_template.html

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,18 @@
2626
{% block custom_css %}
2727
{% endblock custom_css %}
2828

29+
<style>
30+
ul.errorlist{
31+
margin:0px;
32+
}
33+
</style>
2934
<!-- /END GA --></head>
3035

3136
<body>
3237
<div id="app">
3338
<div class="main-wrapper main-wrapper-1">
3439
{% include 'admin_templates/navbar.html' %}
35-
{% include 'admin_templates/sidebar.html' %}
40+
{% include 'admin_templates/sidebar.html' with id=form.instance.id %}
3641

3742

3843
<!-- Main Content -->
@@ -41,6 +46,20 @@
4146
<div class="section-header">
4247
<h1>{% block page_title %}{% endblock page_title %}</h1>
4348
</div>
49+
{% if messages %}
50+
{% for message in messages %}
51+
{% if message.tags == "success" %}
52+
<div class="alert alert-success">
53+
{{ message }}
54+
</div>
55+
{% endif %}
56+
{% if message.tags == "error" %}
57+
<div class="alert alert-danger">
58+
{{ message }}
59+
</div>
60+
{% endif %}
61+
{% endfor %}
62+
{% endif %}
4463
{% block page_content %}
4564
{% endblock page_content %}
4665
</div>
@@ -80,5 +99,10 @@ <h1>{% block page_title %}{% endblock page_title %}</h1>
8099
<script src="{% static '/js/custom.js' %}"></script>
81100
{% block custom_js %}
82101
{% endblock custom_js %}
102+
<script>
103+
document.getElementById("title").onkeyup=function(){
104+
document.getElementById("url_slug").value=document.getElementById("title").value.toLowerCase().replaceAll(" ","-");
105+
}
106+
</script>
83107
</body>
84108
</html>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{% extends 'admin_templates/base_template.html' %}
2+
{% block title %}
3+
Category Create
4+
{% endblock title %}
5+
6+
7+
{% block custom_css %}
8+
{% endblock custom_css %}
9+
10+
{% block page_title %}
11+
Category Create
12+
{% endblock page_title %}
13+
14+
{% block page_content %}
15+
16+
<div class="row">
17+
<div class="col-12 col-md-12 col-lg-12">
18+
<div class="card">
19+
<div class="card-header">
20+
<h4>Categories</h4>
21+
</div>
22+
<div class="card-body">
23+
<form method="post" enctype="multipart/form-data">
24+
{% csrf_token %}
25+
{% for field in form %}
26+
<div class="form-group">
27+
<label>{{ field.label }}</label>
28+
<input type="{{ field.field.widget.input_type }}" name={{ field.name }} id="{{ field.name }}" class="form-control">
29+
{% if field.errors %}
30+
<div class="alert alert-danger" style="margin:10px">
31+
{{ field.errors }}
32+
</div>
33+
{% endif %}
34+
</div>
35+
{% endfor %}
36+
<button type="submit" class="btn btn-primary btn-block">SAVE CATEGORY</button>
37+
</form>
38+
</div>
39+
</div>
40+
</div>
41+
{% endblock page_content %}
42+
43+
44+
{% block custom_js %}
45+
{% endblock custom_js %}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{% extends 'admin_templates/base_template.html' %}
2+
{% block title %}
3+
Category List
4+
{% endblock title %}
5+
6+
7+
{% block custom_css %}
8+
{% endblock custom_css %}
9+
10+
{% block page_title %}
11+
Category List
12+
{% endblock page_title %}
13+
14+
{% block page_content %}
15+
<div class="row">
16+
{% for category in categories_list %}
17+
<div class="col-12 col-sm-6 col-md-6 col-lg-3">
18+
<article class="article article-style-b">
19+
<div class="article-header">
20+
<div class="article-image" data-background="/media/{{ category.thumbnail }}" style="background-image: url(&quot;assets/img/news/img13.jpg&quot;);">
21+
</div>
22+
<div class="article-badge">
23+
<div class="article-badge-item bg-danger"><i class="fas fa-fire"></i>{{ category.title }}</div>
24+
</div>
25+
</div>
26+
<div class="article-details">
27+
<p>{{ category.description }}</p>
28+
<p>Url Slug : {{ category.url_slug }}</p>
29+
<div class="article-cta">
30+
<label class="custom-switch mt-2" style="float:left">
31+
<input type="checkbox" name="custom-switch-checkbox" class="custom-switch-input" {% if category.is_active == 1 %}checked{% endif %}>
32+
<span class="custom-switch-indicator"></span>
33+
<span class="custom-switch-description">ACTIVE</span>
34+
</label>
35+
<a href="{% url 'category_update' category.id %}" class="btn btn-warning">EDIT <i class="fas fa-chevron-right"></i></a>
36+
</div>
37+
</div>
38+
</article>
39+
</div>
40+
{% endfor %}
41+
</div>
42+
{% endblock page_content %}
43+
44+
45+
{% block custom_js %}
46+
{% endblock custom_js %}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{% extends 'admin_templates/base_template.html' %}
2+
{% block title %}
3+
Category Update
4+
{% endblock title %}
5+
6+
7+
{% block custom_css %}
8+
{% endblock custom_css %}
9+
10+
{% block page_title %}
11+
Category Update
12+
{% endblock page_title %}
13+
14+
{% block page_content %}
15+
16+
<div class="row">
17+
<div class="col-12 col-md-12 col-lg-12">
18+
<div class="card">
19+
<div class="card-header">
20+
<h4>Categories</h4>
21+
</div>
22+
<div class="card-body">
23+
<form method="post" enctype="multipart/form-data">
24+
{% csrf_token %}
25+
{% for field in form %}
26+
<div class="form-group">
27+
<label>{{ field.label }}</label>
28+
<input type="{{ field.field.widget.input_type }}" name={{ field.name }} id="{{ field.name }}" class="form-control" value={{field.value}}>
29+
{% if field.errors %}
30+
<div class="alert alert-danger" style="margin:10px">
31+
{{ field.errors }}
32+
</div>
33+
{% endif %}
34+
</div>
35+
{% if field.field.widget.input_type == "file" %}
36+
<div class="form-group">
37+
<label>Current {{ field.label }}</label><br>
38+
<div class="card">
39+
<div class="card-body">
40+
<img src="/media/{{ field.value }}" style="height:250px"/>
41+
</div>
42+
</div>
43+
</div>
44+
{% endif %}
45+
46+
{% endfor %}
47+
<button type="submit" class="btn btn-primary btn-block">SAVE CATEGORY</button>
48+
</form>
49+
</div>
50+
</div>
51+
</div>
52+
{% endblock page_content %}
53+
54+
55+
{% block custom_js %}
56+
{% endblock custom_js %}

0 commit comments

Comments
 (0)