django_project_demo/app/templates/task_list.html
2024-08-24 03:25:23 +00:00

189 lines
7.5 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{% extends 'layout.html' %}
{% block content %}
<div class="container">
<div class="panel panel-default">
<div class="panel-heading">任务表单</div>
<div class="panel-body">
<!--不使用post提交数据,用ajax请求-->
<form id="addForm">
<div class="clearfix">
{% for field in form %}
<div class="col-xs-6">
{# style="position: relative" 相对定位,absolute绝对定位#}
<div class="form-group" style="position: relative;margin-bottom: 20px">
<label>{{ field.label }}</label>
{{ field }}
<span class="error" style='color: red;position: absolute'></span>
</div>
</div>
{% endfor %}
<div class="col-xs-12">
<button id='saveBtn' type="button" class="btn btn-primary">保存</button>
</div>
</div>
</form>
</div>
</div>
<div class="panel panel-default">
<!-- Default panel contents -->
<div class="panel-heading">
<span class="glyphicon glyphicon-th-list" aria-hidden="true"></span>
任务列表
</div>
<!-- Table -->
<table class="table table-bordered">
<thead>
<tr>
<th>ID</th>
<th>任务级别</th>
<th>任务名称</th>
<th>详细内容</th>
<th>任务负责人</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for item in queryset %}
<tr>
<th>{{ item.id }}</th>
<th>{{ item.get_level_display }}</th>
<td>{{ item.title }}</td>
<td>{{ item.detail }}</td>
<td>{{ item.user.username }}</td>
<td>
<a class="btn btn-primary btn-xs" href="/task/{{ item.id }}/edit/">修改</a>
<a class="btn btn-danger btn-xs" href="/task/{{ item.id }}/delete/">删除</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<ul class="pagination">
{{ page_nav }}
</ul>
<h1>Ajax学习</h1>
<input id="btn1" type="button" class="btn-primary" value="ajax示例1传递固定数据"/>
<input type="text" id="txtUser" placeholder="用户名">
<input type="text" id="txtAge" placeholder="年龄">
<input id="btn2" type="button" class="btn-primary" value="ajax示例2传递表单数据"/>
<form id="form3">
<input type="text" name="txtEmail" placeholder="邮箱">
<input type="text" name="txtMore" placeholder="介绍">
<input id="btn3" type="button" class="btn-primary" value="ajax示例3一次打包表单数据并传递"/>
</form>
</div>
{% endblock %}
{% block js %}
<script type="text/javascript">
// 利用jquery来绑定按钮事件
$(function () {
// 页面加载完成后,代码自动执行
bindBtn1Event();
bindBtn2Event();
bindBtn3Event();
bindSaveBtnEvent();
})
function bindBtn1Event() {
$('#btn1').click(function () {
$.ajax({
url: '/task/ajax/',
type: 'post',
{#向后端传递的数据#}
data: {
n1: 123,
n2: 'abc',
},
{#因为后端返回的数据是字符串形式的字典所以在此指定数据类型为json格式 数据来源task.py#}
dataType: "JSON",
success: function (res) {
console.log(res.data);
console.log(res.status);
}
})
})
}
function bindBtn2Event() {
$('#btn2').click(function () {
$.ajax({
url: '/task/ajax/',
type: 'post',
{#获取文本框的内容向后端传递的数据#}
data: {
name: $('#txtUser').val(),
age: $('#txtAge').val(),
},
{#因为后端返回的数据是字符串形式的字典所以在此指定数据类型为json格式 数据来源task.py#}
dataType: "JSON",
success: function (res) {
console.log(res.data);
console.log(res.status);
}
})
})
}
function bindBtn3Event() {
$('#btn3').click(function () {
$.ajax({
url: '/task/ajax/',
type: 'post',
{#获取表单所有文本框的内容并打包向后端传递的数据#}
data: $('#form3').serialize(),
{#因为后端返回的数据是字符串形式的字典所以在此指定数据类型为json格式 数据来源task.py#}
dataType: "JSON",
success: function (res) {
console.log(res.data);
console.log(res.status);
}
})
})
}
function bindSaveBtnEvent() {
$('#saveBtn').click(function () {
// 每次先清除span标签内容
$('.error').empty()
$.ajax({
url: '/task/add/',
type: 'post',
{#获取表单所有文本框的内容并打包向后端传递的数据#}
data: $('#addForm').serialize(),
{#因为后端返回的数据是字符串形式的字典所以在此指定数据类型为json格式 数据来源task.py#}
dataType: "JSON",
success: function (res) {
//console.log(res.error_msg);
//console.log(res.status);
if (res.status) {
alert('添加数据成功');
// 用JS实现页面刷新
location.reload()
} else {
// 不成功要返回错误信息,each循环
// 错误信息包括字段名,和错误信息
// 浏览器自动会为文本框添加一个id属性,格式为id_title
// 所以在循环读出错误信息时,拼接一个id属性
// msg是一个列表所以读出msg[0]
$.each(res.error_msg, function (name, msg) {
$('#id_' + name).next().text(msg[0])
})
}
}
})
})
}
</script>
{% endblock %}