Django简单CURD操作

 

url.py文件(路由分配url)


from django.contrib import admin
from django.urls import path
from app import views

urlpatterns = [
    path('classes/',views.classes),
    path('add_class/',views.add_class),
    path('del_class/',views.del_class),
    path('edit_class/',views.edit_class),
]

函数定义

from django.shortcuts import render,redirect
import pymysql

def classes(request):


    conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='collage',charset='utf8')
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)

    cursor.execute('select * from class')
    classes_list=cursor.fetchall()
    cursor.close()
    conn.close()
    return render(request, 'classes.html', {'classes_list':classes_list})


def add_class(request):
    if request.method=='GET':
        return render(request, 'add_class.html')
    else:
        title=request.POST.get('title')
        conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='collage', charset='utf8')
        cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)

        cursor.execute('insert into class(title) values(%s)',[title,])
        conn.commit()
        cursor.close()
        conn.close()

        return redirect('/classes/')


def del_class(request):

    id=request.GET.get('id')

    conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='collage', charset='utf8')
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)

    cursor.execute('delete from class where id=%s', [id, ])
    conn.commit()
    cursor.close()
    conn.close()

    return redirect('/classes/')

def edit_class(request):

    if request.method == 'GET':
        id=request.GET.get('id')

        conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='collage', charset='utf8')
        cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)

        cursor.execute('select id,title from class where id=%s', [id, ])
        result=cursor.fetchone()
        cursor.close()
        conn.close()
        return render(request,'edit_class.html',{'result':result})
    else:
        id=request.GET.get('id')
        title=request.POST.get('title')

        conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='collage', charset='utf8')
        cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)

        cursor.execute('update class set title=%s where id=%s', [title,id, ])
        conn.commit()
        cursor.close()
        conn.close()

        return redirect('/classes/')

渲染模板

classes.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>班级</title>
</head>
<body>
    <h1>班级列表</h1>
    <div>
        <a href="/add_class/">添加</a>
    </div>
    <table>
        <thead>
            <tr>
                <td>ID</td>
                <td>班级名称</td>
                <td>操作</td>
            </tr>
        </thead>
        <tbody>
            {% for row in classes_list %}
            <tr>
                <td>{{ row.id }}</td>
                <td>{{ row.title }}</td>
                <td>
                    <a href="/edit_class/?id={{ row.id }}">编辑</a>
                    <a href="/del_class/?id={{ row.id }}">删除</a>
                </td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
</body>
</html>

add_class.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加班级</title>
</head>
<body>
    <h1>添加班级</h1>
    <form method="post" action="/add_class/">
        <p>班级名称:<input type="text" name="title"/></p>
        <input type="submit" value="提交">
    </form>

</body>
</html>

edit_class.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>修改班级</title>
</head>
<body>
    <h1>修改班级</h1>
     <form method="post" action="/edit_class/?id={{ result.id }}">
        <p>班级名称:<input type="text" name="title" value="{{ result.title }}"/></p>
        <input type="submit" value="提交">
    </form>
</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值