使用类定义增删改查接口
This commit is contained in:
@@ -7,7 +7,7 @@
|
||||
<body>
|
||||
<h2>创建用户</h2>
|
||||
<form action="/myapp/user/" method="post">
|
||||
{% csrf_token %} <!-- 关键:添加这行代码,新版默认自带安全组件,提交时要提交csrf的token -->
|
||||
{# {% csrf_token %} <!-- 关键:添加这行代码,新版默认自带安全组件,提交时要提交csrf的token -->#}
|
||||
姓名: <input type="text" name="name"> <br>
|
||||
城市: <input type="text" name="city"> <br>
|
||||
性别: <input type="text" name="sex"> <br>
|
||||
|
||||
45
templates/user_edit.html
Normal file
45
templates/user_edit.html
Normal file
@@ -0,0 +1,45 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>编辑用户</title>
|
||||
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<h2>编辑用户</h2>
|
||||
<form action="#">
|
||||
<input type="text" name="id" value="{{ user.id }}" style="display: none">
|
||||
姓名: <input type="text" name="name" value="{{ user.name }}"> <br>
|
||||
城市: <input type="text" name="city" value="{{ user.city }}"> <br>
|
||||
性别: <input type="text" name="sex" value="{{ user.sex }}"> <br>
|
||||
年龄: <input type="text" name="age" value="{{ user.age }}"> <br>
|
||||
<input type="submit" value="提交" id="btn"> <br>
|
||||
</form>
|
||||
<script>
|
||||
$('#btn').click(function (){
|
||||
var id = $('input[name=id]').val();
|
||||
var name = $('input[name=name]').val();
|
||||
var city = $('input[name=city]').val();
|
||||
var sex = $('input[name=sex]').val();
|
||||
var age = $('input[name=age]').val();
|
||||
data = {'id':id, 'name':name, 'city': city, 'sex': sex, 'age': age};
|
||||
$.ajax({
|
||||
type: 'PUT',
|
||||
url: '/myapp/user/',
|
||||
data: data,
|
||||
success: function (result){
|
||||
if(result.code == 200){
|
||||
alert('更新用户成功')
|
||||
location.reload()
|
||||
}else{
|
||||
alert('更新用户失败')
|
||||
}
|
||||
},
|
||||
error: function (){
|
||||
alert('服务器接口异常')
|
||||
}
|
||||
})
|
||||
})
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -3,29 +3,65 @@
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>用户信息管理</title>
|
||||
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<h2>用户信息管理</h2>
|
||||
<table border="1">
|
||||
<thead>
|
||||
<tr>
|
||||
<th style="display: none">ID</th>
|
||||
<th>姓名</th>
|
||||
<th>城市</th>
|
||||
<th>性别</th>
|
||||
<th>年龄</th>
|
||||
<th>操作</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for i in users %}
|
||||
<tr>
|
||||
<td style="display: none">{{ i.id }}</td>
|
||||
<td>{{ i.name }}</td>
|
||||
<td>{{ i.city }}</td>
|
||||
<td>{{ i.sex }}</td>
|
||||
<td>{{ i.age }}</td>
|
||||
<td>
|
||||
<button> <a href="/myapp/user_edit?id={{ i.id }}" target="_blank">编辑</a> </button>
|
||||
<button onclick="delUser(this)">删除</button>
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
<button><a href="/myapp/user_add" target="_blank">创建用户</a></button>
|
||||
</body>
|
||||
<script>
|
||||
function delUser(obj){
|
||||
confirm = confirm("是否删除该用户?")
|
||||
if(confirm){
|
||||
id = $(obj).parent().parent().find("td:eq(0)").text()
|
||||
console.log(id)
|
||||
data = {'id': id}
|
||||
$.ajax({
|
||||
type: 'DELETE',
|
||||
url: '/myapp/user/',
|
||||
data: data,
|
||||
success: function (result){
|
||||
if(result.code == 200){
|
||||
alert('删除用户成功')
|
||||
location.reload()
|
||||
}else{
|
||||
alert('删除用户失败')
|
||||
}
|
||||
|
||||
},
|
||||
error: function (){
|
||||
alert('服务器接口异常')
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
</script>
|
||||
</html>
|
||||
Reference in New Issue
Block a user