前情回顧:Django框架 完成用戶登錄注冊
1.創(chuàng)建管理員
一般管理員都是直接指定,不開放頁面注冊,可以直接手動在數(shù)據(jù)庫添加,Django框架提供了方法,讓我們直接創(chuàng)建管理員,步驟如下:
- 打開pycharm下面的命令行終端
- 輸入命令
python manage.py createsuperuser
- 按照提示進(jìn)行輸入信息
打開數(shù)據(jù)庫,可以看出,已經(jīng)幫我們插入了管理員的信息
同時Django也提供了管理員登錄頁面,http://127.0.0.1:8000/admin/
這里不使用框架自帶的頁面,修改已經(jīng)寫好的用戶登錄頁面,當(dāng)做管理員登錄頁面。
2.完善管理員功能
2.1增加管理員登錄功能
新建管理員視圖,這里筆者命名為admin_controller.py,增添登錄功能,與用戶登錄功能唯一不同的是增加了一個字段校驗,判斷是不是個管理員,判斷成功才能登錄。
- 添加登錄方法,登錄成功后跳轉(zhuǎn)到用戶管理頁面
def admin_login(request):
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
if username and password:
# 進(jìn)行身份驗證 增加一條校驗
user = authenticate(username=username, password=password, is_superuser=1)
if user is not None:
# 管理員驗證成功, 自動重定向用戶管理
return redirect('/user_list')
else:
return HttpResponse('無效的憑據(jù)')
else:
return HttpResponseBadRequest('請?zhí)峁┳x者ID和密碼')
return render(request, 'admin_login.html') # 渲染 user_login.html 模板
- 添加模板 admin_login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.login-container {
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
width: 400px; /* 修改寬度 */
height: 400px;
margin: 20px; /* 添加外邊距 */
}
.login-container h2 {
text-align: center;
}
.login-container form {
display: flex;
flex-direction: column;
}
.login-container form input[type="text"],
.login-container form input[type="password"] {
margin-bottom: 15px;
padding: 10px;
margin-top: 10%;
border-radius: 4px;
border: 1px solid #ccc;
font-size: 16px;
}
.login-container form input[type="submit"] {
margin-top: 20%;
background-color: #007bff;
color: #fff;
border: none;
padding: 10px;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
transition: background-color 0.3s ease;
}
.login-container form input[type="submit"]:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<div class="login-container">
<h2>管理員登錄</h2>
<form method="post">
{% csrf_token %}
<input type="text" name="username" placeholder="Username" required>
<input type="password" name="password" placeholder="Password" required>
<input type="submit" value="Login">
</form>
</div>
</body>
</html>
- 配置url
在urls.py增加路徑
from django.urls import path
from . import reader_controller
from . import admin_Controller
urlpatterns = [
path('admin/', admin_Controller.admin_login),
path('login/', reader_controller.reader_login, name='reader_login'),
path('signup/', reader_controller.signup, name='reader_signup'),
]
2.2完善展示用戶信息功能
- 在視圖增加查詢所有用戶信息函數(shù) ,使用filter過濾信息
def get_all_reader(request):
# 查詢不包括管理員的所有用戶
normal_users = User.objects.filter(is_staff=False)
return render(request, 'user_list.html', {'users': normal_users})
- 返回給模板頁面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>管理菜單</title>
<style>
/* 菜單樣式 */
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
.sidebar {
width: 250px;
background-color: #333;
height: 100%;
position: fixed;
left: 0;
top: 0;
overflow-x: hidden;
{#padding-top: 20px;#}
}
.sidebar a {
padding: 10px 16px;
margin: 20px;
text-decoration: none;
font-size: 22px;
color: #85f112;
display: block;
transition: 0.3s;
}
.sidebar a:hover {
background-color: #0edcac;
color: black;
}
.content {
margin-left: 250px;
padding: 20px;
}
.header {
background-color: #f1f1f1;
padding: 10px;
text-align: center;
}
{# 美化表格#}
table {
border-collapse: collapse;
width: 100%;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
tr:nth-child(even) {
background-color: #f9f9f9;
}
</style>
</head>
<body>
<div class="sidebar">
<div class="header">
<h2>管理菜單</h2>
</div>
<a href="#">用戶管理</a>
<a href="#">圖書管理</a>
<a href="#">借閱管理</a>
<a href="#">數(shù)據(jù)庫日志</a>
</div>
<div class="content">
<!-- 這里是你的主要內(nèi)容 -->
<h2>用戶列表</h2>
<table>
<tr>
<th>編號</th>
<th>用戶名</th>
<th>郵箱</th>
<th>注冊時間</th>
<th>操作</th>
<!-- 這里可以根據(jù)需要顯示其他字段 -->
</tr>
{% for user in users %}
<tr>
<td>{{ user.id }}</td>
<td>{{ user.username }}</td>
<td>{{ user.email }}</td>
<td>{{ user.date_joined }}</td>
<td>
{# 注意這里路徑寫法#}
<a href="../users/{{ user.id }}/edit/">修改</a> | <a href="../delete_user/{{ user.id }}/">刪除</a>
</td>
<!-- 這里可以根據(jù)需要顯示其他字段 -->
</tr>
{% endfor %}
</table>
</div>
</body>
</html>
- 綁定URL
path('user_list/', admin_Controller.get_all_reader),
效果如圖:
2.3完善修改用戶信息功能
完成修改用戶信息,分為兩步:1,獲取到用戶信息 并回顯到頁面 2,提交新的信息到后臺
- 添加查詢用戶信息函數(shù)
def get_reader_by_id(request, id):
# 用戶編號 查詢數(shù)據(jù)
reader = User.objects.filter(id=id).first()
return render(request, 'user_edit.html', {'user': reader})
- 添加用戶編輯頁面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>修改用戶信息</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
background-color: #f7f7f7;
}
form {
max-width: 400px;
margin: 0 auto;
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
input[type="text"],
input[type="password"],
input[type="email"] {
width: 100%;
padding: 8px;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 3px;
}
button {
padding: 10px 20px;
border: none;
border-radius: 3px;
background-color: #4caf50;
color: white;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<form method="post" action="/change_user_password/">
{% csrf_token %}
<input type="hidden" name="user_id" value="{{ user.id }}">
<label for="username">用戶名:</label>
<input type="text" id="username" name="username" value="{{ user.username }}" readonly><br><br>
<label for="password">新密碼:</label>
<input type="password" id="password" name="password" ><br><br>
<label for="email">郵箱:</label>
<input type="email" id="email" name="email" value="{{ user.email }}"><br><br>
<button type="submit">修改</button>
</form>
</body>
</html>
- 綁定URL,因為需要ID查詢用戶信息,因此路徑中應(yīng)該添加int參數(shù)
path('users/<int:id>/edit/', admin_Controller.get_reader_by_id, name='user_edit'),
效果如圖
接下來將表單提交到用戶信息修改函數(shù)
因為密碼存在到數(shù)據(jù)庫的時候,進(jìn)行了加密處理,因為Django提供了一個修改密碼的方法,set_password
def change_user_password(request):
if request.method == 'POST':
try:
username = request.POST.get('username')
new_password = request.POST.get('password')
print(new_password)
new_email = request.POST.get('email')
user = User.objects.get(username=username)
if new_password:
user.set_password(new_password)
if new_email:
user.email = new_email
if new_password or new_email:
user.save()
return redirect('/user_list') # 密碼和郵箱已成功修改或更新
except User.DoesNotExist:
return HttpResponse('用戶不存在') # 用戶不存在,重定向到錯誤頁面或其他適當(dāng)?shù)奶幚?/span>
return HttpResponseBadRequest('不允許的請求') # 非 POST 請求,重定向到其他處理頁面
綁定URL
path('change_user_password/', admin_Controller.change_user_password),
2.4完善刪除用戶信息功能
類似于查詢用戶信息,都需要提供一個ID文章來源:http://www.zghlxwxcb.cn/news/detail-794597.html
def del_user(request, id):
try:
user = User.objects.get(id=id)
user.delete()
return redirect('/user_list')
# 重定向到用戶頁面
except User.DoesNotExist:
return HttpResponse('用戶不存在')
刪除成功之后,直接跳轉(zhuǎn)用戶信息管理頁面
最后附上urls.py文章來源地址http://www.zghlxwxcb.cn/news/detail-794597.html
from django.urls import path
from . import reader_controller
from . import admin_Controller
urlpatterns = [
path('admin/', admin_Controller.admin_login),
path('login/', reader_controller.reader_login, name='reader_login'),
path('signup/', reader_controller.signup, name='reader_signup'),
path('user_list/', admin_Controller.get_all_reader),
path('users/<int:id>/edit/', admin_Controller.get_reader_by_id, name='user_edit'),
path('change_user_password/', admin_Controller.change_user_password),
path('delete_user/<int:id>/', admin_Controller.del_user, name='delete_user'),
]
到了這里,關(guān)于Django 框架添加管理員,完成對普通用戶信息管理的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!