article

Friday, May 1, 2020

Python Django Simple CRUD (Create Read Update Delete)


Python Django Simple CRUD (Create Read Update Delete)

#views.py
from django.shortcuts import render, redirect
from myapp.models import Member

# Create your views here.

def index(request):
    members = Member.objects.all()
    context = {'members': members}
    return render(request, 'index.html', context)

def create(request):
    member = Member(firstname=request.POST['firstname'], lastname=request.POST['lastname'])
    member.save()
    return redirect('/')

def edit(request, id):
    members = Member.objects.get(id=id)
    context = {'members': members}
    return render(request, 'edit.html', context)

def update(request, id):
    member = Member.objects.get(id=id)
    member.firstname = request.POST['firstname']
    member.lastname = request.POST['lastname']
    member.save()
    return redirect('/')

def delete(request, id):
    member = Member.objects.get(id=id)
    member.delete()
    return redirect('/')
#models.py
from django.db import models

# Create your models here.
class Member(models.Model):
    firstname = models.CharField(max_length=40)
    lastname = models.CharField(max_length=40)
	
    created = models.DateTimeField(auto_now_add=True)
	
    def __str__(self):
        return self.firstname + " " + self.lastname

    class Meta:
        ordering = ['created']
		
    class Meta:  
        db_table = "blog_member"
#urls.py
from django.contrib import admin  
from django.urls import path  
from myapp import views  
from django.conf.urls import url

urlpatterns = [  
    path('admin/', admin.site.urls),  
    path('',views.index),
    url(r'^create$', views.create, name='create'),
    url(r'^edit/(?P\d+)$', views.edit, name='edit'),
    url(r'^edit/update/(?P\d+)$', views.update, name='update'),
    url(r'^delete/(?P\d+)$', views.delete, name='delete'),
] 
//templates/index.html
{% extends 'base.html' %}
{% block body %}
<form class="form-inline"  action="create" method="POST">
    {% csrf_token %}
    <div class="form-group">
        <label for="firstname">Firstname</label>
        <input type="text" name="firstname" class="form-control" style="width:30%;" required="required"/>
        <label for="lastname">Lastname</label>
        <input type="text" name="lastname" class="form-control" style="width:30%;" required="required"/>
        <button type="submit" class="btn btn-sm btn-primary"><span class="glyphicon glyphicon-plus"></span> ADD</button>
    </div>
</form>
<br />
<table class="table table-bordered">
    <thead class="alert-warning">
        <tr>
            <th>Firstname</th>
            <th>Lastname</th>
            <th>Action</th>
        </tr>
    </thead>
    <tbody>
        {% for member in members %}
        <tr>
            <td>{{ member.firstname }}</td>
            <td>{{ member.lastname }}</td>
            <td><center><a class="btn btn-sm btn-warning" href="edit/{{ member.id }}"><span class="glyphicon glyphicon-edit"></span> Edit</a> <a class="btn btn-sm btn-danger" href="delete/{{ member.id }}"><span class="glyphicon glyphicon-trash"></span> Delete</a></center></td>
        </tr>
        {% endfor %}
    </tbody>
</table>
{% endblock %}
//templates/edit.html
{% extends 'base.html' %}
{% block body %}
    <form method="POST" action="update/{{ members.id }}">
        {% csrf_token %}
        <div class="form-group">
            <label for="firstname">Firstname</label>
            <input type="text" name="firstname" value="{{ members.firstname }}" class="form-control" required="required"/>
        </div>
        <div class="form-group">
            <label for="lastname">Lastname</label>
            <input type="text" name="lastname" value="{{ members.lastname }}" class="form-control" required="required"/>
        </div>
        <div class="form-group">
            <button type="submit" class="btn btn-sm btn-warning"><span class="glyphicon glyphicon-edit"></span> Update</button>
        </div>
    </form>
{% endblock %}
//templates/base.html
<!DOCTYPE html>
<html lang="en">
<head>
    {% load static %}
    <link rel="stylesheet" type="text/css" href="{% static 'css/bootstrap.css' %}"/>
    <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
</head>
<body>
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <a class="navbar-brand">Cairocoders</a>
        </div>
    </nav>
    <div class="col-md-3"></div>
    <div class="col-md-6 well">
        <h3 class="text-primary">Python Django Simple CRUD (Create Read Update Delete)</h3>
        <hr style="border-top:1px dotted #ccc;"/>
        {% block body %}

        {% endblock %}
    </div>
</body>
</html>

Related Post