shopingwebsite
Shopingapp --> Models.py
go to shopingapp --> admin.py
go to templates --> base.html
go to templates --> header.html
Go to Templates --> navbar.html
Go to Templates --> footer.html
go to Shopingapp --> views.py
import below
get_object_or_404
from . models import Category, Product
Go to shopingpro TEMPLATES add links:
This link showing on navbar
from django.db import models from django.urls import reverse # Create your models here. class Category(models.Model): objects = None name = models.CharField(max_length=250, unique=True) slug = models.SlugField(max_length=250, unique=True) description = models.TextField(blank=True) image = models.ImageField(upload_to='category', blank=True) class Meta: ordering = ('name',) verbose_name = 'category' verbose_name_plural = 'categories' def __str__(self): return '{}'.format(self.name) class Product(models.Model): objects = None name = models.CharField(max_length=250, unique=True) slug = models.SlugField(max_length=250, unique=True) description = models.TextField(blank=True) price = models.DecimalField(max_digits=10, decimal_places=2) category = models.ForeignKey(Category, on_delete=models.CASCADE) image = models.ImageField(upload_to='product', blank=True) stock= models.IntegerField() available = models.BooleanField(default=True) created = models.DateTimeField(auto_now=True) update = models.DateTimeField(auto_now=True) # product details get urls def get_url(self): return reverse('shop:prodCatdetail', args=[self.category.slug, self.slug]) class Meta: ordering = ('name',) verbose_name = 'product' verbose_name_plural = 'products' def __str__(self): return '{}'.format(self.name)
go to shopingapp --> admin.py
from django.contrib import admin # Register your models here. from . models import Category, Product class CategoryAdmin(admin.ModelAdmin): list_display = ['name','slug'] prepopulated_fields = {'slug':('name',)} admin.site.register(Category,CategoryAdmin) class ProductAdmin(admin.ModelAdmin): list_display = ['name','price','stock','available','created','update'] list_editable = ['price','stock','available'] prepopulated_fields = {'slug':('name',)} list_per_page = 20 admin.site.register(Product, ProductAdmin)
go to templates --> base.html
{% load static %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="description" content="{% block metadescription %}{% endblock %}"> <title>{% block title %}{% endblock %}</title> </head> <body> <div> {% include 'header.html' %} {% include 'navbar.html' %} {% block content %} {% endblock %} </div> {% include 'footer.html' %} </body> </html>
{% load static %} <header> <center> <a href="{% url 'shop:allProdCat' %}"><img src="{% static 'img/logo.png' %}" alt="LogImage"></a> </center> </header>
Go to Templates --> navbar.html
<nav class="navbar navbar-expand-lg navbar-light bg-light"> <div class="container-fluid"> <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarSupportedContent"> <ul class="navbar-nav me-auto mb-2 mb-lg-0"> <li class="nav-item"> <a class="nav-link active" aria-current="page" href="#">Home</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Shop</a> </li> <li class="nav-item dropdown {% if shop in request.path %} active {% endif %}"> <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false"> Dropdown </a> <ul class="dropdown-menu" aria-labelledby="navbarDropdown"> <li><a class="dropdown-item" href="{% url 'shop:allProdCat' %}">All Product</a></li> {% for cat in links %} <li><a class="dropdown-item" href="{{cat.get_urls}}">{{cat.name}}</a></li> {% endfor %} </ul> </li> <li class="nav-item"> <a class="nav-link">Your Cart()</a> </li> </ul> <form class="d-flex"> <input class="form-control me-2" type="search" placeholder="Search" aria-label="Search"> <button class="btn btn-outline-success" type="submit">Search</button> </form> </div> </div> </nav>
Go to Templates --> footer.html
<div> <p>Copy; Broadway Kochi Pvt Ltd. All right Reserved</p> </div>
go to Shopingapp --> views.py
from django.http import HttpResponse from django.shortcuts import render, get_object_or_404 from . models import Category, Product # Vertual Env: shop # super user, shop sh1234 # All product category. def allProdCat(request, c_slug=None): c_page=None products=None if c_slug!=None: c_page=get_object_or_404(Category, slug=c_slug) products=Product.objects.all().filter(category=c_page, available=True) else: products=Product.objects.all().filter(available=True) return render(request, "category.html", {'category': c_page, 'products': products}) # To display the product def proDetail(request, c_slug, product_slug): try: product=Product.objects.get(category__slug=c_slug, slug=product_slug) except Exception as e: raise e return render(request, 'product.html', {'product': product})
get_object_or_404
from . models import Category, Product
Create category page on templates, category.html
templates --> category.html{% extends 'base.html' %} {% load static %} <!---metadescription start---> {% block metadescription %} {% if category %} {{ category.description|truncatewords:155 }} {% else %} Welcome to BroadWay online store for any time any were. {% endif %} {% endblock %} <!---metadescription start---> <!--Header/title start---> {% block title %} {% if category %} {{ category.name }} - BroadWay online Store {% else %} See our new Collections - ABC Store . {% endif %} {% endblock %} <!--Header/title end---> <!--block content from base start---> {% block content %} {% if category %} <div> <div> <p>> <a href="{% url 'shop:allProdCat' %}">Our Product Collections</a></p> </div> </div> {% endif %} <div> {% if category %} <img src="{{category.image.url}}" alt="{{category.name}}"> </div> <br> <div> <h1>{{category.name}}</h1> <p>{{category.description}}</p> </div> {% else %} <div> <img src="{% static 'img/banner.png' %}" alt="our products"> </div> <br> <div> <h1>Our product Collections</h1> <p>Our product CollectionsOur product Collections,t CollectionsOur product Collections t CollectionsOur product Collectionst CollectionsOur product Collectionst CollectionsOur product Collectionst CollectionsOur product Collectionst CollectionsOur product Collectionst CollectionsOur product Collectionst CollectionsOur product Collectionst CollectionsOur product Collectionst CollectionsOur product Collectionst CollectionsOur product Collectionst CollectionsOur product Collectionst CollectionsOur product Collectionst CollectionsOur product Collections </p> </div> {% endif %} <div> <div> {% for product in products %} <div> <div> <a href="{{product.get_url}}"><img src="{{product.image.url}}" alt="{{product.name}}"></a> <div> <h3>{{product.name}}</h3> <h3>{{product.price}}</h3> </div> </div> </div> {% endfor %} </div> </div> {% endblock %} <!--block content from base end--->
Create python file on shopingapp.
Go to shopingapp --> create python file name as ‘context_processorse.py’from . models import Category def menu_links(request): links=Category.objects.all() return dict(links=links)
Go to shopingpro TEMPLATES add links:
TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, 'template')], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', 'shopingapp.context_processors.menu_links', ], }, }, ]
This link showing on navbar
<nav class="navbar navbar-expand-lg navbar-light bg-light"> <div class="container-fluid"> <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarSupportedContent"> <ul class="navbar-nav me-auto mb-2 mb-lg-0"> <li class="nav-item"> <a class="nav-link active" aria-current="page" href="#">Home</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Shop</a> </li> <li class="nav-item dropdown {% if shop in request.path %} active {% endif %}"> <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false"> Dropdown </a> <ul class="dropdown-menu" aria-labelledby="navbarDropdown"> <li><a class="dropdown-item" href="{% url 'shop:allProdCat' %}">All Product</a></li> {% for cat in links %} <li><a class="dropdown-item" href="{{cat.get_urls}}">{{cat.name}}</a></li> {% endfor %} </ul> </li> <li class="nav-item"> <a class="nav-link">Your Cart()</a> </li> </ul> <form class="d-flex"> <input class="form-control me-2" type="search" placeholder="Search" aria-label="Search"> <button class="btn btn-outline-success" type="submit">Search</button> </form> </div> </div> </nav>
No comments