标签:django data play 配置文件 display 成功 mod ble 需要
知识点:urls.py配置文件本质是URL与要为该URL调用的视图函数之间的映射表
from django.conf.urls import url
from django.contrib import admin
from app01 import views
urlpatterns = [
url(r‘^user_list/‘, views.UserList.as_view()),
]
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})
配置settings.py文件
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>


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()),
]
<button>
<a href="/add_user/" style="text-decoration: none">新增用户</a>
</button>

<!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>
# 新增用户
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/‘)
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()),
]
<!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>

# 删除用户
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/‘)

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()),
]
<!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>

<!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>
# 编辑用户
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/‘)
04-在views.py中使用class编写django项目
标签:django data play 配置文件 display 成功 mod ble 需要
原文地址:https://www.cnblogs.com/gongniue/p/10838001.html