Giter Site home page Giter Site logo

django-smart-basket's Introduction

forthebadge forthebadge forthebadge

django-smart-basket

Simple E - Commerce Website made using Django and machine learning(frequency pattern) which can suggest items as per user history


hope you have installed django or else you can follow the command


python -m pip install Django

Create the project

django-admin startproject flipkart

change the directory to the new project folder created

Then create the app for the project

python manage.py startapp mainApp

For quick overview of the project you can copy and overwrite the files and folder from the repo to the respective files of django that you have created

How to run the django server

python manage.py runserver

Important Code Snippets

models.py

from django.db import models
from django.conf import settings
from django.shortcuts import reverse

class Item(models.Model):
    title = models.CharField(max_length = 100)
    price = models.FloatField()
    link = models.URLField()
    slug = models.SlugField()
    
    def __str__(self):
        return self.title
    
    def get_absolute_url(self):
        return reverse("mainApp:cart", kwargs={"slug": self.slug})
    
class Cart(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,blank = True,null = True)
    item = models.ForeignKey(Item,on_delete=models.CASCADE)
    quantity = models.IntegerField(default=1)
    
    def __str__(self):
        return f"{self.quantity} of {self.item.title}"
    
    def get_absolute_user(self):
        return reverse("mainApp:buy", kwargs={"user": self.user})

class Order(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE)
    items = models.ManyToManyField(Cart)
    # ordered = models.BooleanField(default=False)
    
    def __str__(self):
        return self.user.username
        

3 set of models of database made

  • Item
  • Cart
  • Order

Item

This model of database will store all the items along with it's picture , description and price

Cart

This database will store the user which is logged in,and the items added to cart by user

Order

This database will confirm the purchase and clear the Cart database and store the data as transaction history of the user which will be later trained my ML to suggest items to the same user from the Order database

views.py

from django.shortcuts import render , get_object_or_404,redirect
from django.views.generic import ListView,DetailView
from .models import Item , Cart , Order

class homeView(ListView):
    model = Item
    template_name = "frontend/index.html"

def add_to_cart(request,slug):
    item = get_object_or_404(Item,slug = slug)
    item_check = Cart.objects.filter(item = item)
    if item_check.exists():
        order = item_check[0]
        order.quantity += 1
        order.save()
    else:
        order_item = Cart.objects.create(item = item)
    return redirect("mainApp:home")

class cartView(ListView):
    model = Cart
    template_name = "frontend/cart.html"

def buyView(request,user):
    try:
        user_cart = Cart.objects.filter(user = request.user)
        cart_order = Order.objects.filter(user = request.user)
        print(cart_order)
        if cart_order.exists():
            old_user = Order.objects.filter(user = request.user)
            print(old_user)
            old_user.items.add(user_cart)
            print('exists')
        else:
            new_user = Order.objects.create(user = request.user)
            print(new_user)
            new_user.items.add(user_cart)
        return redirect("mainApp:home")
    except:
        # Cart.objects.all().delete()
        return redirect('mainApp:home')

Currently working on ML to integrate it with the frontend and django database

Model to be used is : Frequency Pattern Tree

Contributions to this project is open

Snapshot of the website and databae

Frontend items list

Cart frontpage

Item Database structure

Each Item store in database

Order databae

django-smart-basket's People

Contributors

geek-ninja avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.