1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>车辆违章查询</title>
<style>
* {
font: 18px/30px Arial;
}
#container {
width: 960px;
margin: 0 auto;
}
#search {
width: 720px;
margin: 10px auto;
padding-top: 100px;
}
#search input[type=search] {
display: inline-block;
width: 480px;
height: 30px;
}
#search input[type=submit] {
display: inline-block;
width: 80px;
height: 40px;
border: None;
background-color: red;
color: white;
margin-left: 20px;
}
#result {
width: 920px;
margin: 20px auto;
border-collapse: collapse;
}
#result th {
font-weight: bolder;
border-bottom: 1px solid darkgray;
}
#result td, #result th {
text-align: center;
height: 50px;
width: 180px;
}
</style>
</head>
<body>
<div id="container">
<form id="search" action="/search" method="post">
<!-- 跨站身份伪造: 利用浏览器存储的cookie中的用户身份标识冒充用户执行操作 -->
<!-- 防范跨站身份伪造最佳的做法就是在表单中放置随机令牌 -->
<!-- 除此之外通过设置令牌还可以防范表单重复提交以及重放攻击 -->
<!-- 隐藏域 / 隐式表单域: 页面上是无法看到该内容-->
{% csrf_token %}
<input type="search" id="carno" name="carno" placeholder="请输入你的车牌号" required>
<input type="submit" value="搜索">
<a href="/add">添加新记录</a>
</form>
<hr>
<table id="result">
<thead>
<tr>
<th>车牌号</th>
<th>违章原因</th>
<th>违章时间</th>
<th>处罚方式</th>
<th>是否受理</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<p>{{ last }}</p>
</div>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script>
$(function() {
$('#search').on('submit', function(evt) {
evt.preventDefault();
var carno = $('#carno').val();
var token = $('#search input[type=hidden]').val()
$.ajax({
url: '/search2',
type: 'post',
data: {
'carno': carno,
'csrfmiddlewaretoken': token
},
dataType: 'json',
success: function(json) {
$('#result tbody').children().remove();
for (var i = 0; i < json.length; i += 1) {
var record = json[i];
var tr = $('<tr>').append($('<td>').text(record.carno))
.append($('<td>').text(record.reason))
.append($('<td>').text(record.date))
.append($('<td>').text(record.punish));
var imgName = record.isdone ? 'icon-yes.svg' : 'icon-no.svg';
tr.append($('<td>').append($('<img>').attr('src', '/static/images/' + imgName)));
$('#result tbody').append(tr);
}
}
});
});
});
</script>
</body>
</html>