Project on Product detail (Django)

Product info project(by using modelforms):

Step 1 : Create
           folder name: productfolder
           project name: productspro
           app name: productsapp
Step 2open pycharm
Step 3: Go to models file and write the below code
__________________________
from __future__ import unicode_literals
from django.db import models
import datetime as dt

mdate= dt.datetime.now()
dur=dt.datetime.now()+dt.timedelta(days=730)

class Product(models.Model):
    pid=models.IntegerField(primary_key=True)
    pname=models.CharField(max_length=20)
    pcost=models.IntegerField()
    pcolor=models.CharField(max_length=20,default=’black’)
    pmfd=models.DateField(default=mdate)
    pexfd=models.DateTimeField(default=dur)

——————————————————-
Step 4:Open mysql create database —productdb
Step 5:
configure database
_____________________________________
DATABASES = {
    ‘default’: {
        ‘ENGINE’: ‘django.db.backends.mysql’,
        ‘NAME’: ‘productsdb’,
        ‘USER’:’root’,
        ‘PASSWORD’:’root’,
    }
}
__________________________________
Step 6:run makemigrations, run migrate and runserver
Step 7:forms.py file in app
_________________________________
from django import forms
from .models import Product

class ProductForm(forms.ModelForm):
    class Meta:
        model=Product
        fields=[‘pid’,
                ‘pname’,
                ‘pcost’,
                ‘pcolor’,
                ‘pmfd’,
                ‘pexfd’]
     
class uppdateForm(forms.Form):
    pid=forms.CharField(label=’enter pid’,max_length=20)
    pcost=forms.CharField(label=’enter pcost’,max_length=10)
 
class DeleteForm(forms.Form):
    pid=forms.IntegerField()
________________________________

Step 8open project level urls.py file and write

__________________________________
from django.conf.urls import url
from django.contrib import admin
from django.conf.urls import include

urlpatterns = [
    url(‘admin/’, admin.site.urls),
    url(‘productsapp/’,include(‘productsapp.urls’)),
]
______________
step 9
create url file in app level and write the code

_____________________–

from django.conf.urls import url
from . import views
app_name=”productsapp”

urlpatterns=[
    url(‘^$’,views.home,name=’home’),
    url(‘^insert$’,views.insert,name=’insert’),
    url(‘^display$’,views.display,name=’display’),
    url(‘^update$’,views.update,name=’update’),
    url(‘^delete$’,views.delete,name=’delete’),
]
__________________________________
Step 10:
goto views.py file
_______________________
from django.shortcuts import render,HttpResponse
from .forms import ProductForm,uppdateForm,DeleteForm
from .models import Product
from django.template import loader

def home(request):
    return render(request,’home.html’)
def insert(request):
    if request.method==’POST’:
        form=ProductForm(request.POST)

        if form.is_valid():
            form.save(commit=True)
            data=”<h1>Insertion is successfully done</h1>”
            “<a href=’./’>Goto home</a>”
            return HttpResponse(data)
        else:
            print(form.errors)

    else:
        form=ProductForm()
        return render(request,’insert.html’,{‘form’:form})
 
def display(request):
    det=Product.objects.all()
    if len(det)==0:
        data=”<h1>no data found</h1>”
              “<a href=’./’>Goto home</a>”
        return HttpResponse(data)
    else:
        template=loader.get_template(‘detail.html’)
        context={‘det’:det}
        r=template.render(context,request)
        return HttpResponse(r)
 
def update(request):
    if request.method==’POST’:
        form=uppdateForm(request.POST)
        if form.is_valid():
            id=form.cleaned_data[‘pid’]
            id1=int(id)
            cost=form.cleaned_data[‘pcost’]
            cost1=float(cost)
            dbuser=Product.objects.filter(pid=id1)
            if not dbuser:
                data=”<h1>Invalid product id</h1>”
                     “<a href=’./’>Goto Home</a>”
                return HttpResponse(data)
            else:
                dbuser.update(pcost=cost)
                data=”<h1> product updated successfully</h1>”
                      “<a href=’./’>Goto home</a>”
                return HttpResponse(data)
        else:
            print(form.errors)
    else:
        form=uppdateForm()
        return render(request,’update.html’,{‘form’:form})
 
def delete(request):
    if request.method==’POST’:
        form=DeleteForm(request.POST)
        if form.is_valid():
            pid1=int(form.cleaned_data[‘pid’])
            dbuser=Product.objects.get(pid=pid1)
            if not dbuser:
                return HttpResponse(‘The given id is not available’)
            else:
                dbuser.delete()
                data=”<h1>Product was deleted successfully</h1>”
                    “<a href=’./’>Goto home</a>”
                return HttpResponse(data)
        else:
            print(form.errors)
         
    else:
        form=DeleteForm()
        return render(request,’delete.html’,{‘form’:form})
 
 
 ______________________________________________________
Step 11:
create templates folder and create html file home.html
______________________________________________
<html>
<head>
    <style>
        i{
            color: red;
            font-style: italic;
        }
    </style>
</head>
<body bgcolor=”#7fffd4″,align=”center”>
<MARQUEE BEHAVIOR=”alternate” WIDTH=”100%”><h1>Welcome to <i>Product info</i>Project</h1></MARQUEE>
<center>
    <h1>Please select required operation</h1>
    <h2>
        <a href=”./insert”>Click here to insert the product</a><br>
        <a href=”./display”>click here to display the product</a><br>
        <a href=”./update”>click here to update the product</a><br>
        <a href=”./delete”>click here to delete the product</a><br>
    </h2>
</center>
</body>
   
</html>
_______________________________________________________
       
Step 12:
create html file with name insert.html

_____________________
<html>
<head>
    <style>
        #hid1{
            color:chartreuse;
            font-style: italic;
        }
        i{
            color:red;
            font-style: italic;
        }
    </style>
</head>
<body bgcolor=”#3eff3e”>
<MARQUEE BEHAVIOR=”alternate” width=”100%”><h1>welcome to <i>Product Info</i>Product</h1></MARQUEE>
  <h1 id=”hid1″><center>Please enter your product Detail……</center></h1>
<center>
    <form action=”” method=”post”>
        {%csrf_token%}
        <fieldset style=”width:40%;background: bisque”>
            <body bgcolor=”#ee82ee”, align=”center”>
            <legend align=”center” style=”color:red;”>Product Details:</legend>
            <table border=”0″>
                <b> Product ID:</b>{{form.pid}} <br><br>
                <b> Product Name:</b>{{form.pname}} <br><br>
                <b> Product cost:</b>{{form.pcost}} <br><br>
                <b> Product color:</b>{{form.pcolor}} <br><br>
                <b> Product mfg:</b>{{form.pmfd}} <br><br>
                <b> Product exd:</b>{{form.pexfd}} <br><br>
                <input type=”submit” value=”submit”>
             
            </table>
            </body>
        </fieldset>
    </form>

<h3> <a href=’./’>click here to goto homepage</a></h3><br>
</center>
</body>
</html>
_____________________
Step 12: create detail.html file and write

_____________________
<html>
<head>
    <style>
        #hid1{
            color:red;
            font-style: italic;
        }
        i{
            color: red;
            font-style: italic;
        }
    </style>
</head>
<body bgcolor=”#3eff3e”>
<marquee behavior=”alternate” width=”100%”><h1>welcome to <i>Product info</i>Project</h1></marquee>
    <h1 id=”hid1″><center>Product Details are……</center></h1>
<center>
    <table border=”5″ height=”10%” width=”70%”>
        <tr>
            <th> Product Id</th>
            <th> Product Name</th>
            <th> Product Cost</th>
            <th> Product Color</th>
            <th> Product Mf_date</th>
            <th> Product Ex_date</th>
        </tr>
        {% for prod in det %}
        <tr>
            <td>{{prod.pid}}</td>
            <td>{{prod.pname}}</td>
            <td>{{prod.pcost}}</td>
            <td>{{prod.pcolor}}</td>
            <td>{{prod.pmfd}}</td>
            <td>{{prod.pexfd}}</td>
        </tr>
        {%endfor%}
    </table>
    <h3><a href=”./”>click here to go  home page</a></h3><br>
</center>
</body>
</html>
____________________________
Step 13:
create html file with name update.hmtl

—————
<html>
<head>
    <style>
        #hid1{
            color:red;
            font-style: italic;
        }
        i{
            color: red;
            font-style: italic;
        }
    </style>
</head>
<body bgcolor=”#3eff3e”>
<marquee behavior=”alternate” width=100%><h1>welcome to <i>Product info</i>Project</h1></marquee>
   <h1 id=”hid1″><center>Update product cost here….</center></h1>
<center>
    <form action=”” method=”post”>
        {%csrf_token%}
        <b>Product id</b>{{form.pid}}<br><br>
        <b>Product cost</b>{{form.pcost}}<br><br>
        <input type=”submit” value=”Submit”/>
    </form>
    <h3> <a href=”./”>click here to go to home page</a> </h3><br>
</center>
</body>
</html>
_____________-

Step 14:create html file with name detail.html
_________________
<html>
<head>
    <style>
         #hid1{
            color:chartreuse;
            font-style: italic;
        }
        i{
            color:red;
            font-style: italic;
        }
    </style>
</head>
<body bgcolor=”#3eff3e”>
<marquee behavior=”alternate” width=”100%”><h1>Welcome to <i>Product Info</i>Project</h1></marquee>
  <h1 id=”hid1″><center>Please enter product id…………</center></h1>
<center>
    <form action=”” method=”post”>
        {%csrf_token %}
        {{form}}<br><br>
        <input type=”submit” value=”Delete”>
    </form>
    <h3><a href=”./”>Click here to go home page</a> </h3><br>
</center>
</body>
</html>
____________________
Step 16:
goto settings.py file and set the path of templates and   run the server

6 thoughts on “Project on Product detail (Django)”

  1. Hi there friends, its impressive article concerning teachingand completely defined, keep it up all the time. Mellie Griffin Braden Tamar Pepe Lais

  2. May I simply just say what a relief to discover somebody who genuinely understands what they’re talking about on the internet. You certainly understand how to bring a problem to light and make it important. More and more people have to look at this and understand this side of your story. It’s surprising you’re not more popular given that you most certainly possess the gift.

Leave a Reply

Your email address will not be published. Required fields are marked *