04-在views.py中使用class编写django项目
在views.py中使用class编写django项目
知识点:urls.py配置文件本质是URL与要为该URL调用的视图函数之间的映射表
1. 用户信息展示页
1.1 在urls.py中添加用户展示页路由
from django.conf.urls import url
from django.contrib import admin
from app01 import views
urlpatterns = [
url(r'^user_list/', views.UserList.as_view()),
]
1.2 在views.py中添加试图类UserList
from django import views
# django基础必备三件套
from django.shortcuts import render, redirect
# 导入models模块,获取数据库中的数据
from app01 import models
# 用户展示
class UserList(views.View):
# 根据用户发送请求的方法不同,做不同的操作
def get(self, request):
# 查询出所有用户数据
data = models.User.objects.all()
# 在页面上中展示, 将页面返回给用户
return render(request, 'user_list.html', {'users': data})
1.3 在templates中创建user_list.html文件
配置settings.py文件
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['templates'],
创建user_list.html文件,在页面上展示用户信息
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>user_list</title>
</head>
<body>
<table border="2">
<thead>
<th>序号</th>
<th>用户名</th>
<th>密码</th>
</thead>
<tbody>
{% for user in users %}
<tr>
<td>{{ user.id }}</td>
<td>{{ user.username }}</td>
<td>{{ user.password }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>
1.4 页面效果
2. 新建用户
2.1 在urls.py中添加新建用户路由
from django.conf.urls import url
from django.contrib import admin
from app01 import views
urlpatterns = [
url(r'^user_list/', views.UserList.as_view()),
url(r'^add_user/', views.AddUser.as_view()),
]
2.2 在用户展示页,添加a标签,以便跳转到新建用户页面
<button>
<a href="/add_user/" style="text-decoration: none">新增用户</a>
</button>
2.3 在templates中创建add_user.html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>add_user</title>
</head>
<body>
<form action="" method="post">
<input type="text" name="username" placeholder="用户名"><br>
<input type="password" name="password" placeholder="密码"><br>
<button>提交</button>
</form>
</body>
</html>
2.4 在views.py中添加试图类AddUser
# 新增用户
class AddUser(views.View):
# 根据用户发送请求的方法不同,做不同的操作
def get(self, request):
# 当用户请求为GET时,返回add_user.html页面
return render(request, 'add_user.html')
def post(self, request):
# 当用用户请求为POST时,获取用户在页面提交的数据
username = request.POST.get('username')
password = request.POST.get('password')
# 在数据库创建,新的用户
models.User.objects.create(username=username, password=password)
# 跳转到用户信息展示页面
return redirect('/user_list/')
3. 删除用户
3.1 在urls.py中添加删除用户路由
from django.conf.urls import url
from django.contrib import admin
from app01 import views
urlpatterns = [
url(r'^user_list/', views.UserList.as_view()),
url(r'^add_user/', views.AddUser.as_view()),
url(r'^del_user/', views.DelUser.as_view()),
]
3.2 在用户展示页面user_list.html中添加删除用户的a标签
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>user_list</title>
</head>
<body>
<table border="2">
<thead>
<th>序号</th>
<th>用户名</th>
<th>密码</th>
<th>操作</th>
</thead>
<tbody>
{% for user in users %}
<tr>
<td>{{ user.id }}</td>
<td>{{ user.username }}</td>
<td>{{ user.password }}</td>
<td>
<a href="/del_user/?id={{ user.id }}">删除</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<button>
<a href="/add_user/" style="text-decoration: none">新增用户</a>
</button>
</body>
</html>
3.3 在views.py中添加试图类DelUser
# 删除用户
class DelUser(views.View):
# 根据用户发送请求的方法不同,做不同的操作
def get(self, request):
# 获取URL中提交的数据,获得id对应值
del_id = request.GET.get('id')
# 在数据库中通过id字段获取数据,并删除
models.User.objects.filter(id=del_id)[0].delete()
# 跳转到用户信息展示页面
return redirect('/user_list/')
4. 编辑用户
4.1 在urls.py中添加编辑用户路由
from django.conf.urls import url
from django.contrib import admin
from app01 import views
urlpatterns = [
url(r'^user_list/', views.UserList.as_view()),
url(r'^add_user/', views.AddUser.as_view()),
url(r'^del_user/', views.DelUser.as_view()),
url(r'^edit_user/', views.EditUser.as_view()),
]
4.2 在用户展示页面user_list.html中添加编辑用户的a标签
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>user_list</title>
</head>
<body>
<table border="2">
<thead>
<th>序号</th>
<th>用户名</th>
<th>密码</th>
<th>操作</th>
</thead>
<tbody>
{% for user in users %}
<tr>
<td>{{ user.id }}</td>
<td>{{ user.username }}</td>
<td>{{ user.password }}</td>
<td>
<a href="/del_user/?id={{ user.id }}">删除</a>
<a href="/edit_user/?id={{ user.id }}">编辑</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<button>
<a href="/add_user/" style="text-decoration: none">新增用户</a>
</button>
</body>
</html>
4.3 在templates中创建edit_user.html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>edit_user</title>
</head>
<body>
<form action="" method="post">
<input type="text" name="username" placeholder="{{ user.username }}"><input name="edit_id" value="{{ user.id }}" style="display: none"></input><br>
<input type="password" name="password" placeholder="密码"><br>
<button>提交</button>
</form>
</body>
</html>
4.4 在views.py中添加试图类DelUser
# 编辑用户
class EditUser(views.View):
# 根据用户发送请求的方法不同,做不同的操作
def get(self, request):
# 获取URL中提交的数据,获得id对应值
edit_id = request.GET.get('id')
# 在数据库中通过id字段获取数据
edit_user = models.User.objects.filter(id=edit_id)[0]
# 在编辑用户页面返回用户
return render(request, 'edit_user.html', {'user': edit_user})
# 当用用户请求为POST时,获取用户在页面提交的数据
def post(self, request):
# 获取用户提交过来的数据,通过edit_id查找到需要修改的用户
edit_id = request.POST.get('edit_id')
edit_user = models.User.objects.filter(id=edit_id)[0]
# 获取用户在页面提交的修改数据,并且在数据中修改
edit_user.username = request.POST.get('username')
edit_user.password = request.POST.get('password')
# 将改动同步到数据库
edit_user.save()
# 编辑成功,跳转到用户展示页面
return redirect('/user_list/')
更多精彩

