zoukankan      html  css  js  c++  java
  • python全栈开发day61-django简单的出版社网站展示,添加,删除,编辑(单表的增删改查)

    day61
    django内容回顾:
        1. 下载:
            pip install django==1.11.14
            pip install -i 源 django==1.11.14
            pycharm
        2. 创建项目
            命令行: djang-admin startproject 项目名
            pycharm file ——》 new project ——》 选django 填项目名称 选解释器 app01
            
        3. 启动项目:
            命令行: python manage.py runserver 
            pycharm  配置修改 点绿三角
        4. django的配置 settings.py
            1. templates 模板 路径
            2. 注释csrf中间件 
            3. 静态文件 
                STATIC_URL = '/static/'        # 别名
                STATICFILES_DIRS=[]   #静态文件的路径
            4. 数据库的配置
            
        5. APP
            创建APP:
                命令行: python manage.py startapp app01
                pycharm  tools ——》run manage.py task ——》 startapp app01
            
            注册APP:
                在settings.py中的INSTALLED_APPS添加:
                'app01' 或者 'app01.apps.App01Config',
        6. views.py 写你的函数     业务逻辑
            默认参数 request  请求相关内容
                request:
                    request.method   请求方法  GET/POST
                    request.POST     post请求提交的数据    字典
                    request.GET      GET请求URL上携带的参数
                    
                基础必备三件套:
                from django.shorcuts import HttpResponse,render,redirect
                HttpResponse('返回的字符串')        ——》 页面显示的内容
                render(request,'HTML文件名')        ——》返回一个HTML文件
                redirect('跳转的URL')                ——》告诉浏览器向另一个URL发请求
                
          7. ORM 
            1. 对象和关系型数据的映射  通过操作对象的方式来操作数据库
            
            2. 映射关系:
                类    ——》 数据表
                对象 ——》数据行
                属性 ——》 字段
                
            3. ORM功能:
                操作数据表
                操作数据行
                
            4. mysql数据库:
                1. 创建mysql数据库
                2. 配置:
                    DATABASES = {
                        'default': {
                            'ENGINE': 'django.db.backends.mysql',  # 引擎
                            'NAME': 'day60',      # 数据库名字
                            'HOST': '127.0.0.1',  # host地址
                            'PORT': 3306,         # 端口号
                            'USER': 'root',       # 用户名
                            'PASSWORD': '',       # 密码
                        }
                    }
                3. 告诉django使用pymysql来连数据库:
                    在项目同名的文件下的__init__.py中写下面的代码:
                        import pymysql
                        pymysql.install_as_MySQLdb()
                4. 在app01/models.py中写类(继承models.Model):
                    class Userinfo(models.Model):
                        user = models.CharField(max_length=32)
                        pwd = models.CharField(max_length=32)
                5. 执行两条数据库迁移的命令:
                    python manage.py makemigrations   ——》 models变更情况记录到APP下的migrations文件夹下
                    python manage.py migrate          ——》把所有的变更更新到数据库中
    
                6. ORM操作:
                    from app01 import models
                    # 查询所有的对象
                    models.Userinfo.objects.all()   
                    # 获取一个对象。如果查不到或者查到多个就会报错。
                    models.Userinfo.objects.get(user='alex',pwd='alexdsb')
                    # 创建一个对象
                    models.Userinfo.objects.create(user='alex',pwd='alexdsb')
            
                
        8. form表单
            1. method='post'  action=''   提交方式  提交地址
            2. input标签都有name属性 
            3. 一个type='submit' 的按钮或者input标签 (bootstrap的button按钮默认有提交事件)
    今日内容:
    1.单表的增删改查
            
            
            
            
    课上笔记

    django简单的出版社网站展示,添加,删除,编辑

    展示函数和html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>出版社列表</title>
        <style>
            html{
                user-select: none;
            }
        </style>
    </head>
    <body>
    <table border="1" style="border-collapse: collapse;" cellpadding="5" bgcolor="#f0f8ff">
        <thead>
            <tr>
                <th>序号</th>
                <th>ID</th>
                <th>名称</th>
                <th colspan="2">操作</th>
            </tr>
        </thead>
        <tbody>
            {% for publisher in publishers %}
            <tr>
                <td>{{forloop.counter}}</td>
                <td>{{ publisher.id }}</td>
                <td>{{ publisher.name }}</td>
                <td>
                    <a href="/del_publisher/?id={{ publisher.id  }}">
                        <button>删除</button>
                    </a>
                </td>
                <td>
                    <a href="/edit_publisher/?id={{ publisher.id  }}">
                        <button>编辑</button>
                    </a>
                </td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
    <a href="/add_publisher/">添加一个出版社</a>
    <p style="color: red">{{ err_msg }}</p>
    </body>
    </html>
    publisher_list.html
    def publisher_list(request):
        publishers = models.publisher.objects.all().order_by('id')
        return render(request,'publisher_list.html', {'publishers': publishers})
    View Code

    添加:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>添加出版社</title>
    </head>
    <body>
        <h1>添加出版社</h1>
        <form action="" method="post">
            <input type="text" name="name">
            <p style="color: red;">{{ err_msg }}</p>
            <button>提交</button>
    
        </form>
    </body>
    </html>
    add_publisher.html
    def add_publisher(request):
        err_msg = ''
    
        if request.method == "POST":
            new_name = request.POST.get('name')
            if new_name:
                name = models.publisher.objects.filter(name=new_name)
                if not name:
                    models.publisher.objects.create(name=new_name)
                    return redirect('/publisher/')
    
                else:
                    err_msg = '数据已存在!'
            else:
                err_msg = '数据为空!'
        return render(request, 'add_publisher.html', {"err_msg": err_msg})
    add_publisher

    删除:

    def del_publisher(request):
        err_msg = ''
        del_id = request.GET.get('id')
        del_obj_list = models.publisher.objects.filter(id=del_id)
        if del_obj_list:
            del_obj_list.delete()
            return redirect('/publisher/')
        else:
            err_msg = '您要删除的数据不存在!'
        return render(request, 'publisher_list.html', {"err_msg": err_msg})
    del_publisher

    编辑:

    def edit_publisher(request):
        err_msg = ''
        edit_id = request.GET.get('id')
        edit_obj_list = models.publisher.objects.filter(id=edit_id)
        if request.method=='POST':
            new_name = request.POST.get('name')
            edit_name_list = models.publisher.objects.filter(name=new_name)
            if not edit_name_list and edit_obj_list and new_name:
                edit_obj_list[0].name = new_name
                edit_obj_list[0].save()
                return redirect('/publisher/')
            elif not edit_obj_list:
                err_msg = '您要修改的数据不存在!'
            elif not new_name:
                err_msg = '修改后名字不能为空'
            else:
                err_msg = '数据库已存在该名称'
        return render(request, 'edit_publisher.html', {"err_msg": err_msg})
    edit_publisher
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>编辑出版社</title>
    </head>
    <body>
        <h1>编辑出版社</h1>
        <form action="" method="post">
            <input type="text" name="name">
            <p style="color: red;">{{ err_msg }}</p>
            <button>提交</button>
    
        </form>
    </body>
    </html>
    edit_publisher.html
  • 相关阅读:
    初试kafka消息队列中间件一 (只适合初学者哈)
    3月22日 html(三)css样式表
    3月20日html(二) 图片热点,网页划分,表单
    3月19日 html(一) html基础内容
    3月18日 全部练习题(一)
    3月18日 全部练习题(二)
    3月15日
    3月13日 函数
    3月13日 冒泡排序
    3月12日 数组
  • 原文地址:https://www.cnblogs.com/wuchenggong/p/9365746.html
Copyright © 2011-2022 走看看