Django学习-武沛齐教程【day1】

参考

https://www.bilibili.com/video/BV1NL41157ph

快速上手

创建项目

django-admin startproject 项目名称

默认项目文件介绍

mysite/
-manage.py    【项目的管理、启动项目、创建app、数据管理】【不要动】
-mysite/
  --__init__.py
  --settings.py 【项目配置】【常常操作】
  --urls.py     【URL和函数对应关系】【常常操作】
  --asgi.py     【接收网络请求】【不要动】
  --wsgi.py     【接收网络请求】【不要动】

运行项目:python manage.py runserver

创建APP

python manage.py startapp 应用名称

默认应用文件介绍

mysite/
-app01/
  --__init__.py
  --admin.py    【固定,不用动】django默认提供的admin后台管理
  --apps.py     【固定,不用动】app启动类
  --migrations/ 【固定,不用动】数据库变更记录
    ---__init__.py
  --models.py   【重要】对数据库操作
  --tests.py    【固定,不用动】单元测试
  --views.py    【重要】函数

  1. 注册APP,mysite/setting.py
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'app01.apps.App01Config', # 注册APP
]
  1. 编写URL和视图函数对应关系,mysite/urls.py
from app01 import views

urlpatterns = [
    path('index/', views.index),
]
  1. 编写视图函数,app01/views.py
from django.shortcuts import render,HttpResponse

def index(request):
    return HttpResponse("欢迎")

  1. templates模版对应视图函数,app01/templates
def user_list(request):
    # 去app目录下的templates目录寻找user_list.html(根据app的注册顺序,逐一去他们的templates中找)
    return render(request,"user_list.html")
  1. 静态文件,app01/static
{% load static%}
<!DOCTYPE html>
<head>
    <title>Document</title>
    <link rel="stylesheet" href="{% static 'plugins/bootstrap-3.4.1-dist/css/bootstrap.css' %}">
</head>
<body>
    <script src="{% static 'js/jquery.slim.min.js' %}"></script>
    <script src="{% static 'plugins/bootstrap-3.4.1-dist/js/bootstrap.js' %}"></script>
</body>
</html>

模板语法

  • 代码
# views.py
def tpl(request):
    name = '张三'
    roles = ['管理员', 'CEO', '保安']
    user_info = {"name": 'lisi', "salary": 10000, "role": "CEO"}
    data_list = [
        {"name": 'zhangsan', "salary": 5000, "role": "管理员"},
        {"name": 'lisi', "salary": 6000, "role": "CEO"},
        {"name": 'wangwu', "salary": 7000, "role": "保安"}
    ]
    return render(request, 'tpl.html', {"n1": name, "n2": roles, "n3": user_info, "n4": data_list})


<!DOCTYPE html>
<html lang="en">
  <body>
    <h1>模版语法学习</h1>
    <div>n1为单个元素:{
   
   { n1 }}</div>
    <hr />
    <div>n2为list:{
   
   { n2 }}</div>
    <div>使用.0123代表下标{
   
   { n2[0] }}</div>
    <div>{
   
   { n2[1] }}</div>
    <div>{
   
   { n2[2] }}</div>
    <div>
      {% for item in n2 %}
        <span>list循环展示{
   
   { item }}</span>
      {% endfor %}
    </div>
    <hr />
    <div>n3为字典:{
   
   { n3 }}</div>
    <div>{
   
   { n3.name }}</div>
    <div>{
   
   { n3.salary }}</div>
    <div>{
   
   { n3.role }}</div>
    {% for k, v in n3.items %}
      <div>字典循环{
   
   { k }} = {
   
   { v }}</div>
    {% endfor %}
    <hr />
    <div>n4为列表,元素为字典:{
   
   { n4 }}</div>
    <div>{
   
   { n4[1] }}</div>
    <div>{
   
   { n4[1].name }}</div>
    {% for item in n4 %}
      <div>{
   
   { item.name }}</div>
    {% endfor %}

    {% if n1 == '张三' %}
      <h1>aaaa</h1>
    {% else %}
      <h1>bbbb</h1>
    {% endif %}
  </body>
</html>

视图函数的render内部

  1. 读取含有模版语法的HTML文件
  2. 内部进行渲染(模版语法执行并替换数据),得到只包含HTML标签的字符串
  3. 将渲染完成的字符串返回给用户浏览器

请求和响应

# views.py
def something(request):
    # request是一个对象,封装了用户发送过来的所有请求相关数据
    # 1、【请求】获取请求方式 GET/POST
    print(request.method)
    # 2、【请求】在URL上传递值
    print(request.GET)
    # 3、【请求】通过请求体中提交数据
    print(request.POST)

    # 4、【响应】HttpResponse("返回内容"),字符串内容返回给请求者
    return HttpResponse("返回")

    # 5、【响应】读取HTML的内容+渲染 》》字符串,返回给用户浏览器
    # return render(request,"something.html",{"title":"title"})

    # 6、【响应】让浏览器重定向到其他页面
    # return redirect("https://www.baidu.com")

POST请求错误:CSRF verification failed. Request aborted.

在form表单中加入{% csrf_token %},随机生成的校验机制

<!DOCTYPE html>
<html lang="en">
<body>
    <form action="/login/" method="post">
        {% csrf_token %}
        <input type="text" name="user" placeholder="用户名">
        <input type="password" name="pwd" placeholder="密码">
        <input type="submit" value="提交">
    </form>
</body>
</html>
def login(request):
    if request.method== "GET":
        return render(request,"login.html")
    else:
        # <QueryDict: {'csrfmiddlewaretoken': ['49xkDn1H448ehzVqKezwcK3x64Z3Py3d0eq
        #  lBqfIpTIQ6GLtmblEJ8QXza2iMYbf'], 'user': ['zhangsan'], 'pwd': ['123']}>
        print(request.POST)
        return HttpResponse("登录成功")

数据库操作

  • MySQL数据库+pymysql,过于繁琐
import pymysql

# 1.连接MySQL
conn = pymysql.connect(host="127.0.0.1",post=3306,user='root',passwd="root123",charset="utf8",db="unicom")
cursor=conn.cursor(cursor=pymysql.cursors.DictCursor)
# 2.发送指令
cursor.execute("insert into admin(username,password,mobile) values('zhangsan','123','15000000000')")
conn.commit()
# 3.关闭连接
cursor.close()
conn.close()

Django操作数据库更简单,内部提供了ORM框架,ORM将代码翻译成为sql语句

安装第三方模块

conda install mysqlclient

ORM

ORM可以做两件事:

  1. 创建、修改、删除数据库中的表(不用写SQL语句)【无法创建数据库,自己创建】
  2. 操作表中的数据(不用写SQL语句)

自己创建数据库

  1. 启动Mysql服务

    mysql -u root -p root123456

  2. 创建数据库test

    create database test default charset utf8 collate utf8_general_ci;

Django连接数据库

在mysite/settings.py中进行配置和修改

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'test', #数据库的名字
        'HOST': '127.0.0.1', #数据库地址
        'PORT': '3306', #端口
        'USER': 'root', #用户名
        "PASSWORD": 'root123456', #密码
    }
}

django操作表

在app01/models.py中创建、修改、删除表

from django.db import models

class UserInfo(models.Model):
    name=models.CharField(max_length=32)
    password=models.CharField(max_length=64)
    age=models.IntegerField()
    
'''相当于
create table 
app01_userinfo(
    id bigint auto_increment primary key,
    name varchar(32),
    password varchar(64),
    age int
)
'''

命令行执行:

python manage.py makemigrations

python manage.py migrate

注意:

  • APP需要提前在setting.py中注册

  • 在表中新增列时,由于已存在列中可能已有数据,所以新增列必须要指定新增列对应的数据

    1. 设置默认值

      age = models.IntegerField(default=2)

    2. 允许为空

      data = models.IntegerField(null=True,blank=True)

  • 如果在开发中要对表结构进行调整,在models.py中操作类即可,再执行

    python manage.py makemigrations

    python manage.py migrate

操作表中数据

#views.py
from app01.models import Department

def orm(request):
    # 1、新建数据
    Department.objects.create(title="销售部")
    Department.objects.create(title="运营部")
    Department.objects.create(title="IT部")
    
    # 2、删除数据
    Department.objects.filter(id=1).delete() # 删除符合条件的数据
    Department.objects.all().delete() # 删除全部数据
    
    # 3、获取数据
    # data_list=[行对象,行对象,...] QuerySet类型
    data_list=Department.objects.all() # 获取全部数据
    data_list = Department.objects.filter(title="运营部") # 获取所有符合条件的数据
    for obj in data_list:
        print(obj.id,obj.title)
    data = Department.objects.filter(title="运营部").first() # 获取符合条件的第一条数据
    
    # 4、更新数据
    Department.objects.all().update(title="IT部") # 更新所有数据
    Department.objects.filter(id=3).update(title="IT部") # 更新符合条件的数据
    
    return HttpResponse("成功")

案例,用户管理

1、展示用户列表

  • urls.py
urlpatterns = [
    # 案例 用户管理
    path('infolist/',views.infolist),
]
  • views.py
    • 获取所有用户信息
    • HTML渲染
#views.py
from app01.models import Department,UserInfo

def infolist(request):
    # 1、获取数据库中所有的用户信息
    data_list=UserInfo.objects.all()
    # 2、渲染,返回给用户
    return render(request,"infolist.html",{"data_list":data_list})
  • templates/infolist.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <table>
        <thead>
            <tr>
                <th>id</th>
                <th>姓名</th>
                <th>密码</th>
                <th>年龄</th>
            </tr>
        </thead>
        <tbody>
            {% for item in data_list %}
                <tr>
                    <td>{
   
   {item.id}}</td>
                    <td>{
   
   {item.name}}</td>
                    <td>{
   
   {item.password}}</td>
                    <td>{
   
   {item.age}}</td>
                </tr>
            {% endfor %}
        </tbody>
    </table>
</body>
</html>

2、添加用户

  • urls.py
urlpatterns = [
    # 案例用户管理
    path('infolist/',views.infolist),
    path('infoadd/',views.infoadd),
]
  • views.py
    • GET,看到页面,输入内容
    • POST,写入到数据库
def infoadd(request):
    # GET,看到页面,输入内容
    if request.method=="GET":
        return render(request,"infoadd.html")
    # POST,写入到数据库
    user=request.POST.get("name")
    pwd=request.POST.get("pwd")
    age=request.POST.get("age")

    #添加到数据库
    UserInfo.objects.create(name=user,password=pwd,age=age)
    return redirect("/infolist/")
  • templates/infoadd.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <!--不写action,默认是给当前地址提交post-->
    <form method="post">
        {% csrf_token %}
        <input type="text" name="name" placeholder="用户名">
        <input type="password" name="pwd" placeholder="密码">
        <input type="text" name="age" placeholder="年龄">
        <input type="submit" value="提交">
    </form>
</body>
</html>

3、删除用户

  • urls.py
urlpatterns = [
    # 案例用户管理
    path('infolist/',views.infolist),
    path('infoadd/',views.infoadd),
    path('infodelete/',views.infodelete),
]
  • views.py
def infodelete(request):
    nid=request.GET.get('nid')
    UserInfo.objects.filter(id=nid).delete()
    return redirect('/infolist/')
  • templates/infolist.html
<tbody>
    {% for item in data_list %}
        <tr>
            <td>{
   
   {item.id}}</td>
            <td>{
   
   {item.name}}</td>
            <td>{
   
   {item.password}}</td>
            <td>{
   
   {item.age}}</td>
            <td><a href="/infodelete/?nid={
   
   {item.id}}">删除</a></td>
        </tr>
    {% endfor %}
</tbody>

猜你喜欢

转载自blog.csdn.net/weixin_65656674/article/details/126574491