Python - Django - ORM 操做數據

查詢數據(查詢管理員):

app01/models.py 中定義的類,也就是建立的表html

from django.db import models


# 類必須繼承 models.Model
class Admin(models.Model):
    # 建立一個主鍵自增的字段
    id = models.AutoField(primary_key=True)  # AutoField 爲自增的字段
    # 建立一個 varchar 類型的不能爲空的字段
    # varchar 類型須要指定最大長度
    username = models.CharField(null=False, max_length=20)
    password = models.CharField(null=False, max_length=20)

admin 表中的數據python

在 app01/views.py 中寫上獲取數據的函數數據庫

from django.shortcuts import render
from app01 import models


def admin_list(request):
    admins = models.Admin.objects.all()  # 獲取全部數據
    print(admins[0].id, admins[0].username, admins[0].password)
    return render(request, "admin_list.html", {"admin_list": admins})

而後在 mysite/urls.py 中寫上對應關係django

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

from django.conf.urls import url
from django.shortcuts import HttpResponse, render, redirect
from app01 import views  # 從 app01 中導入函數


# 保存路徑和函數的對應關係
urlpatterns = [
    url(r'^admin_list/', views.admin_list)
]

admin_list.html:app

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>管理員列表</title>
</head>
<body>

<table border="1">
    <thead>
    <tr>
        <th>id</th>
        <th>用戶名</th>
        <th>密碼</th>
    </tr>
    </thead>
    <tbody>

    {% for user in admin_list %}
        <tr>
        <td>{{ user.id }}</td>
        <td>{{ user.username }}</td>
        <td>{{ user.password }}</td>
        </tr>
    {% endfor %}

    </tbody>
</table>
</body>
</html>

這裏用到了 for 循環來讀取數據函數

{% for user in admin_list %}
    <tr>
    <td>{{ user.id }}</td>
    <td>{{ user.username }}</td>
    <td>{{ user.password }}</td>
    </tr>
{% endfor %}

運行結果post

添加數據(添加管理員):

在 admin_list.html 中增長一個添加管理員的選項url

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>管理員列表</title>
</head>
<body>

<table border="1">
    <thead>
    <tr>
        <th>id</th>
        <th>用戶名</th>
        <th>密碼</th>
    </tr>
    </thead>
    <tbody>

    {% for user in admin_list %}
        <tr>
        <td>{{ user.id }}</td>
        <td>{{ user.username }}</td>
        <td>{{ user.password }}</td>
        </tr>
    {% endfor %}

    </tbody>
</table>

<a href="/add_admin/">添加管理員</a>

</body>
</html>

點擊後會跳轉到 /add_admin/ 進行相關的操做orm

add_admin.html:htm

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加管理員</title>
</head>
<body>

<form action="/add_admin/" method="post">
    <p>用戶名:
        <input type="text" name="username">
    </p>
    
    <p>密碼:
        <input type="text" name="password">
    </p>
    
    <p>
        <input type="submit" value="提交">
    </p>
</form>

</body>
</html>

app01/views.py 下的代碼

from django.shortcuts import render, redirect
from app01 import models


def admin_list(request):
    admins = models.Admin.objects.all()  # 獲取全部數據
    print(admins[0].id, admins[0].username, admins[0].password)
    return render(request, "admin_list.html", {"admin_list": admins})


def add_admin(request):
    if request.method == "POST":
        user = request.POST.get("username", None)
        pwd = request.POST.get("password", None)
        # 去數據庫中新增一條數據
        models.Admin.objects.create(username=user, password=pwd)
        # 添加成功後跳轉
        return redirect("/admin_list/")

    return render(request, "add_admin.html")

在 mysite/urls.py 中添加對應關係

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

from django.conf.urls import url
from django.contrib import admin
from django.shortcuts import HttpResponse, render, redirect
from app01 import views


# 保存路徑和函數的對應關係
urlpatterns = [
    url(r'^admin_list/', views.admin_list),
    url(r'^add_admin/', views.add_admin)
]

運行結果:

點擊「添加管理員」

輸入用戶名和密碼,點擊「提交」

管理員添加成功

相關文章
相關標籤/搜索