Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
Python-100-Days
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
huangkq
Python-100-Days
Commits
70b6b6e2
Commit
70b6b6e2
authored
May 25, 2018
by
jackfrued
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
更新了Django实例代码
parent
52252f57
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
48 additions
and
15 deletions
+48
-15
views.py
Day31-Day35/car/search/views.py
+36
-10
add.html
Day31-Day35/car/templates/add.html
+11
-5
search2.html
Day31-Day35/car/templates/search2.html
+1
-0
No files found.
Day31-Day35/car/search/views.py
View file @
70b6b6e2
from
datetime
import
datetime
from
json
import
JSONEncoder
from
django
import
forms
from
django.http
import
JsonResponse
from
django.shortcuts
import
render
from
django.shortcuts
import
render
,
redirect
from
search.models
import
CarRecord
...
...
@@ -13,6 +14,7 @@ from search.models import CarRecord
# return JsonResponse(obj, encoder=, safe=False)
# from django.core.serializers import serialize
# return HttpResponse(serialize('json', obj), content_type='application/json; charset=utf-8')
MAX_AGE
=
14
*
24
*
60
*
60
class
CarRecordEncoder
(
JSONEncoder
):
...
...
@@ -24,8 +26,22 @@ class CarRecordEncoder(JSONEncoder):
def
ajax_search
(
request
):
current_time
=
datetime
.
now
()
.
ctime
()
# Cookie是保存在浏览器临时文件中的用户数据(通常是识别用户身份的ID/token或者是用户的偏好设置)
# 因为每次请求服务器时在HTTP请求的请求头中都会携带本网站的Cookie数据
# 那么服务器就可以获取这些信息来识别用户身份或者了解用户的偏好 这就是所谓的用户跟踪
# 因为HTTP本身是无状态的 所以需要使用Cookie/隐藏域/URL重写这样的技术来实现用户跟踪
# 从请求中读取指定的cookie - 通过cookie的名字找到对应的值
# 如果请求中没有指定名字的cookie可以通过get方法的第二个参数设置一个默认的返回值
last_visit_time
=
request
.
COOKIES
.
get
(
'last_visit_time'
)
if
request
.
method
==
'GET'
:
return
render
(
request
,
'search2.html'
)
response
=
render
(
request
,
'search2.html'
,
{
'last'
:
last_visit_time
if
last_visit_time
else
'你是第一次访问我们的网站'
})
# 通过render渲染页面后先用set_cookie方法设置cookie后再返回HttpResponse对象
# 第一个参数是cookie的名字 第二个参数是cookie的值 第三个参数是过期时间(秒)
response
.
set_cookie
(
'last_visit_time'
,
current_time
,
max_age
=
MAX_AGE
)
return
response
else
:
carno
=
request
.
POST
[
'carno'
]
record_list
=
list
(
CarRecord
.
objects
.
filter
(
carno__icontains
=
carno
))
...
...
@@ -58,21 +74,31 @@ def search(request):
return
render
(
request
,
'search.html'
,
ctx
)
class
CarRecordForm
(
forms
.
Form
):
class
CarRecordForm
(
forms
.
Model
Form
):
carno
=
forms
.
CharField
(
min_length
=
7
,
max_length
=
7
,
label
=
'车牌号'
,
error_messages
=
{
'carno'
:
'请输入有效的车牌号'
})
reason
=
forms
.
CharField
(
max_length
=
50
,
label
=
'违章原因'
)
punish
=
forms
.
CharField
(
max_length
=
50
,
required
=
False
,
label
=
'处罚方式'
)
"""
# 执行额外的表单数据验证
def clean_carno(self):
_carno = self.cleaned_data['carno']
if not condition:
raise forms.ValidationError('...')
return _carno
"""
class
Meta
:
model
=
CarRecord
fields
=
(
'carno'
,
'reason'
,
'punish'
)
def
add
(
request
):
errors
=
[]
if
request
.
method
==
'GET'
:
f
=
CarRecordForm
()
f
=
CarRecordForm
(
initial
=
{
'reason'
:
'打警察'
,
'punish'
:
'牢底坐穿'
}
)
else
:
f
=
CarRecordForm
(
request
.
POST
)
if
f
.
is_valid
():
CarRecord
(
**
f
.
cleaned_data
)
.
save
()
f
=
CarRecordForm
()
else
:
errors
=
f
.
errors
.
values
()
return
render
(
request
,
'add.html'
,
{
'f'
:
f
,
'errors'
:
errors
})
f
.
save
()
return
redirect
(
'/search2'
)
return
render
(
request
,
'add.html'
,
{
'f'
:
f
})
Day31-Day35/car/templates/add.html
View file @
70b6b6e2
...
...
@@ -14,14 +14,20 @@
<h2>
添加违章记录
</h2>
<hr>
<p>
{% for
err in error
s %}
<div
class=
"err"
>
{{
err
}}
</div>
{% for
hint in hint
s %}
<div
class=
"err"
>
{{
hint
}}
</div>
{% endfor %}
</p>
<form
action=
"/add"
method=
"post"
>
<table>
{{ f.as_table }}
</table>
{% for field in f.visible_fields %}
<div>
{{ field.label }}
{{ field }}
{% for error in field.errors %}
<span
class=
"err"
>
{{ error }}
</span>
{% endfor %}
</div>
{% endfor %}
{% csrf_token %}
<input
type=
"submit"
value=
"添加"
>
</form>
...
...
Day31-Day35/car/templates/search2.html
View file @
70b6b6e2
...
...
@@ -73,6 +73,7 @@
</tbody>
</table>
<p>
{{ last }}
</p>
</div>
<script
src=
"https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"
></script>
<script>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment