1.前言
使用Django開發web應用的時候需要,先使用pip安裝才可以使用
2.關于目前Django架構的結構了解

1.setting.py是web應用的基本設定,其中定義了使用的資料庫以及認證子產品(非常重要)
2.urls.py就是用于管理目前的通路的url對應的調用的子產品所使用的方法
在使用目前的子產品方法之前需要導入目前的子產品:使用
web路徑為.
3.templates檔案就是用于存放目前所需要的靜态的html頁面的
3.sqlite3資料展示
4.功能的基本展示
使用者資訊的清單展示
添加使用者
修改使用者
删除操作使用a标簽實作
5.操作中遇到的問題
1.不能使用post請求的問題:
解決在目前的form标簽的下一行添加 {% csrf_token %}
2.重定向頁面的問題:需要使用/結尾就可以了
6.web的基本操作
1.轉發需要使用:from django.shortcuts import render
render(request, “updateUser.html”, model)
這裡的第二個參數是html頁面,model就是一個簡單的dict字典資料
2.重定向需要使用:from django.shortcuts import redirect
redirect(to="…/users/") # 這裡的user後面一定要加斜杠(/)
7.建立需要的user.py元件
class User:
def __init__(self, _id=None, _username=None, _password=None):
self.id = _id
self.username = _username
self.password = _password
8.建立usersController.py元件
from django.shortcuts import render
from django.shortcuts import redirect
import sqlite3
from .user import User # 如果是web環境需要通過from . 的方法是導入本地的子產品類
def to_add_user_page(request):
print("通路目前的添加使用者頁面")
return render(request, "adduser.html")
# 重定向需要使用目前的redirect子產品來實作定向操作、
def add_user(request):
request.encoding = 'utf-8'
if 'username' in request.POST and request.POST['username']:
username = request.POST["username"]
if 'password' in request.POST and request.POST['password']:
password = request.POST["password"]
if username and password:
print("目前添加的資料為:username:{0},password:{1}".format(username, password))
try:
conn = sqlite3.connect("db.sqlite3")
cursor = conn.cursor()
cursor.execute("insert into users(username,password) values(?,?)", (username, password))
conn.commit()
except Exception as e:
print("出現了錯誤:{0}".format(e))
finally:
close_database(cursor, conn)
print("執行添加使用者資訊操作")
else:
print("沒有資料如何添加使用者資訊")
return redirect(to="../users/")
def to_update_user_page(request):
model = {}
if "id" in request.GET and request.GET["id"]:
id = request.GET["id"]
if id:
try:
conn = sqlite3.connect("db.sqlite3")
cursor = conn.cursor()
cursor.execute("select * from users where id=?", id)
user_data = cursor.fetchone()
model["updateUser"] = convert_to_user(user_data)
except Exception as e:
print("出現了錯誤:{0}".format(e))
finally:
close_database(cursor, conn)
else:
print("沒有id如何執行查找操作!")
return render(request, "updateUser.html", model)
# 通過目前的id編号更新使用者
def update_user(request):
if "id" in request.POST and request.POST['id']:
id = request.POST["id"]
username = request.POST["username"]
password = request.POST["password"]
try:
conn = sqlite3.connect("db.sqlite3")
cursor = conn.cursor()
cursor.execute("update users set username=? ,password=? where id=?", (username, password,
id))
conn.commit()
except Exception as e:
print("出現了錯誤:{0}".format(e))
finally:
close_database(cursor, conn)
else:
print("沒有id如何更新?")
return redirect(to="../users/")
# 删除資料操作
def del_user_by_id(request):
if "id" in request.GET and request.GET['id']:
id = request.GET["id"]
try:
conn = sqlite3.connect("db.sqlite3")
cursor = conn.cursor()
cursor.execute("delete from users where id=?", id)
conn.commit()
except Exception as e:
print("出現了錯誤:{0}".format(e))
finally:
close_database(cursor, conn)
else:
print("沒有id如何删除?")
return redirect(to="../users/")
# 用于向目前的資料庫中查詢所有的資料
def user_list(request):
print("通路目前的所有的使用者資訊資料")
try:
conn = sqlite3.connect("db.sqlite3")
cursor = conn.cursor()
cursor.execute("select * from users")
users = [convert_to_user(item) for item in cursor.fetchall()]
# 開始添加視圖資料
model = {"users": users}
return render(request, "index.html", model)
except Exception as e:
print("出現異常:{0}".format(e))
finally:
close_database(cursor, conn)
# 用于将目前的資料庫的資料轉換稱對應的使用者資訊對象
def convert_to_user(user_data=[]):
return User(user_data[0], user_data[1], user_data[2])
# 關閉資料庫連接配接和關閉遊标的操作
def close_database(cursor, conn):
if cursor in locals():
cursor.close()
if conn in locals():
conn.close()
9.其他的html頁面
目前的頁面放在templates檔案中
1.index.html頁面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用于管理界面</title>
</head>
<body>
<table border="1">
<tr>
<td colspan="4"><h1>歡迎使用使用者管理系統</h1></td>
</tr>
<tr>
<td colspan="4"><a href="../toAddUserPage/">添加使用者</a></td>
</tr>
<tr>
<th>使用者編号</th>
<th>使用者名</th>
<th>使用者密碼</th>
<th>操作</th>
</tr>
{% for user in users %}
<tr>
<td>{{ user.id }}</td>
<td>{{ user.username }}</td>
<td>{{ user.password }}</td>
<td><a href="../toUpdateUserPage?id={{ user.id }}">修改</a>
<a href="../delUser?id={{ user.id }}">刪除</a>
</td>
</tr>
{% endfor %}
</table>
</body>
</html>
2.目前的adduser.html頁面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>添加使用者界面</title>
</head>
<body>
<form action="../addUser/" method="post">
{% csrf_token %}
<table border="1">
<tr>
<td colspan="2"><h1>添加使用者操作</h1></td>
</tr>
<tr>
<td>使用者名:</td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td>使用者密碼:</td>
<td><input type="text" name="password"/></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="儲存"/>
<input type="button" value="傳回" onclick="history.back()"/>
</td>
</tr>
</table>
</form>
</body>
</html>
3.目前的updateUser.html頁面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>添加使用者界面</title>
</head>
<body>
<form action="../updateUser/" method="post">
{% csrf_token %}
<table border="1">
<tr>
<td colspan="2"><h1>修改使用者操作</h1></td>
</tr>
<input type="hidden" name="id" value="{{ updateUser.id }}"/>
<tr>
<td>使用者名:</td>
<td><input type="text" name="username" value="{{ updateUser.username }}"/></td>
</tr>
<tr>
<td>使用者密碼:</td>
<td><input type="text" name="password" value="{{ updateUser.password }}"/></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="儲存"/>
<input type="button" value="傳回" onclick="history.back()"/>
</td>
</tr>
</table>
</form>
</body>
</html>
10.結果
測試成功,所有的跳轉頁面和添加修改删除查詢資料都成功!這裡不顯示頁面了
11.總結
1.在使用目前的Django中需要注意
它的表達式{%%}
2.在重定向的時候
一定要在路徑的後面加斜杠
3.通路資料需要
從目前的request.GET和request.POST中去取
,這裡資料集就是一個
dict字典
,需要先判斷在取出資料
4.需要
使用{{參數}}的方式顯示dict中的資料
,
疊代需要使用{%for %}{%endfor%}
以上純屬個人見解,如有問題請聯系本人!