Django Learning - Wu Peiqi Tutorial [day1]

reference

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

Get started quickly

create project

django-admin startproject 项目名称

Introduction to Default Project Files

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

Run the project:python manage.py runserver

Create APP

python manage.py startapp 应用名称

Introduction to default application files

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

  1. Register 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. Write the corresponding relationship between URL and view function, mysite/urls.py
from app01 import views

urlpatterns = [
    path('index/', views.index),
]
  1. Write the view function, app01/views.py
from django.shortcuts import render,HttpResponse

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

  1. The templates template corresponds to the view function, app01/templates
def user_list(request):
    # 去app目录下的templates目录寻找user_list.html(根据app的注册顺序,逐一去他们的templates中找)
    return render(request,"user_list.html")
  1. Static files, 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>

template syntax

  • the code
# 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>

Inside the render of the view function

  1. Read HTML files with template syntax
  2. Render internally (template syntax execution and replace data) to get a string containing only HTML tags
  3. Return the rendered string to the user's browser

request and response

# 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 request error: CSRF verification failed. Request aborted.

Add in the form form {% csrf_token %}, randomly generated verification mechanism

<!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("登录成功")

database operation

  • MySQL database + pymysql, too cumbersome
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 is easier to operate the database. It provides an ORM framework internally, and ORM translates the code into SQL statements.

Install third-party modules

conda install mysqlclient

ORM

An ORM can do two things:

  1. Create, modify, and delete tables in the database (without writing SQL statements) [Unable to create a database, create it yourself]
  2. Operate the data in the table (without writing SQL statements)

Create your own database

  1. Start the Mysql service

    mysql -u root -p root123456

  2. create databasetest

    create database test default charset utf8 collate utf8_general_ci;

Django connects to the database

Configure and modify in mysite/settings.py

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

django-action-sheet

Create, modify, delete tables in 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
)
'''

Command line execution:

python manage.py makemigrations

python manage.py migrate

Notice:

  • APP needs to be registered in setting.py in advance

  • When adding a new column in a table, since there may already be data in the existing column, the new column must specify the data corresponding to the new column

    1. set default

      age = models.IntegerField(default=2)

    2. Allow empty

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

  • If you want to adjust the table structure during development, just operate the class in models.py, and then execute

    python manage.py makemigrations

    python manage.py migrate

Data in the operation table

#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("成功")

case, user management

1. Display user list

  • urls.py
urlpatterns = [
    # 案例 用户管理
    path('infolist/',views.infolist),
]
  • views.py
    • Get all user information
    • HTML rendering
#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. Add users

  • urls.py
urlpatterns = [
    # 案例用户管理
    path('infolist/',views.infolist),
    path('infoadd/',views.infoadd),
]
  • views.py
    • GET, see the page, enter content
    • POST, write to the database
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. Delete user

  • 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>

Guess you like

Origin blog.csdn.net/weixin_65656674/article/details/126574491