Django-Project models tutorial part-3

Python-Django Framework

Posted by indranil on March 21, 2021, 4:04 p.m.

Here i am showing you how to create models for our django project. In this previous post i show you how to use templates and static for django-project.

If you already learn templates and static folder then lets visit this post here i show  how to use modles folder on our django-project.

What Is Models?

A model in Django is a special kind of object – it is saved in the database. A database is a collection of data. This is a place in which you will store information about users, your blog posts, etc. We will be using a SQLite database to store our data. This is the default Django database adapter – it'll be enough for us right now. Every App has separed models file for create database objects.

Location(your app folder):

django-project/home/

Open models.py

from django.db import models

# Create your models here For Tutorial-3.

class StudentList(models.Model):

    name = models.CharField(max_length=100)

    class_name = models.CharField(max_length=100)

    roll_no = models.IntegerField(default=0)

    pass

Open admin.py

from django.contrib import admin

from home.models import StudentList

# Register your models here.

admin.site.register(StudentList)

Open Your Terminal

python manage.py makemigrations

python manage.py migrate

python manage.py runserver

Go to the Admin Panel: http://127.0.0.1:8000/admin

Django-Project Save Objects By Post Method

Location(your templates folder):

django-project/templates/home/

Open index.html

{% extends 'base.html' %}

{% load static %}

{% block title %} home {% endblock title %}

{% block body %}

<center>

    <div class="container">

        <h3>Add Your Student</h3>

        <br>

        <form action="/" method ='post'>

            {% csrf_token %}

        <input type="text" name="name" id="" placeholder="Enter Student Name"><br><br>

        <input type="text" name="classname" id="" placeholder="Enter Class Name"><br><br>

        <input type="text" name="roll" id="" placeholder="Enter Roll Number"><br><br>

        <button>Submit</button>

    </form>

    

    </div>

</center>

{% endblock %}

Location(your app folder):

django-project/home/

Open views.py

from django.shortcuts import render, redirect, HttpResponse

import datetime

# Create your views here.

from home.models import StudentList

def homeIndex(request):

    if request.method == 'POST':

        name = request.POST.get('name', '')

        class_name = request.POST.get('classname', '')

        roll = request.POST.get('roll', '')

        print(name, class_name, roll)

        saveme = StudentList(name=name, class_name=class_name, roll_no=roll)

        saveme.save()

        return redirect('/')

        #print("This method is post")

    #return HttpResponse(f'The sum of a+b is: {a+b}')

    return render(request, 'home/index.html')



def testIndex(request):


   return render(request, 'home/test.html')

def test1Index(request):

    return render(request, 'home/test1.html')

def test2Index(request):

   return render(request, 'home/test2.html')

Django-Project Fetch Objects

Location(your templates folder):

django-project/templates/home/

Open text.html

{% extends 'base.html' %}

{% load static %}

{% block title %} test1 {% endblock title %}

{% block body %}

<center>

   <div class="container">

    <h3> All Student List</h3>

    <table class="table">

        <thead>

          <tr>

            <th scope="col">Id</th>

            <th scope="col">Name</th>

            <th scope="col">Class</th>

            <th scope="col">Roll</th>

          </tr>

        </thead>

        <tbody>

            {% for i in stu %}

          <tr>

            <th scope="row">{{i.id}}</th>

            <td>{{i.name}}</td>

            <td>{{i.class_name}}</td>

            <td>{{i.roll_no}}</td>

          </tr>

          {% endfor %}

        </tbody>

      </table>

    </div>

</center>

{% endblock %}

Location(your app folder):

django-project/home/

Open views.py

from django.shortcuts import render, redirect, HttpResponse

import datetime

# Create your views here.

from home.models import StudentList

def homeIndex(request):

    if request.method == 'POST':

        name = request.POST.get('name', '')

        class_name = request.POST.get('classname', '')

        roll = request.POST.get('roll', '')

        print(name, class_name, roll)

        saveme = StudentList(name=name, class_name=class_name, roll_no=roll)

        saveme.save()

        return redirect('/')

        #print("This method is post")

    #return HttpResponse(f'The sum of a+b is: {a+b}')

    return render(request, 'home/index.html')

def testIndex(request):

    #Fetch The All Student ghggxcdhjghgdhfgdhjfg

    stu = StudentList.objects.all()

    print(stu.count())

    #

    show = {'stu':stu}

    return render(request, 'home/test.html', show)

def test1Index(request):

    return render(request, 'home/test1.html')

def test2Index(request):

    return render(request, 'home/test2.html')

 

Django-Project models objects

def testIndex(request):

   #Fetch The All Student 

    stu = StudentList.objects.all()

    print(stu.count()) #All Count result

    #filter fetch

    filterfetch = StudentList.objects.filter(name='name')

    #Update

    for i in filterfetch:

        i.name = 'updated name'

        i.save()

    #Delete

    for dlt in filterfetch:

        dlt.delete()    

    

    show = {'stu':stu}

    return render(request, 'home/test.html', show)

 

noimg Visit Link: digitalocean.com