Day 62 Django第三天
?
2、GET請求能夠被cache,GET請求能夠被保存在瀏覽器的瀏覽歷史里面(密碼等重要數據GET提交,別人查看歷史記錄,就可以直接看到這些私密數據)POST不進行緩存。3、GET參數是帶在URL后面,傳統IE中URL的最大可用長度為2048字符,其他瀏覽器對URL長度限制實現上有所不同。POST請求無長度限制(目前理論上是這樣的)。4、GET提交的數據大小,不同瀏覽器的限制不同,一般在2k-8K之間,POST提交數據比較大,大小靠服務器的設定值限制,而且某些數據只能用 POST 方法「攜帶」,比如 file。5、全部用POST不是十分合理,最好先把請求按功能和場景分下類,對數據請求頻繁,數據不敏感且數據量在普通瀏覽器最小限定的2k范圍內,這樣的情況使用GET。其他地方使用POST。6、GET 的本質是「得」,而 POST 的本質是「給」。而且,GET 是「冪等」的,在這一點上,GET 被認為是「安全的」。但實際上 server 端也可以用作資源更新,但是這種用法違反了約定,容易造成 CSRF(跨站請求偽造)。REF:maximum length of HTTP GET request?http://stackoverflow.com/questions/2659952/maximum-length-of-http-get-request http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.15 Request-URI Too Longhttp://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.2.1 General Syntaxhttp://www.cnblogs.com/xiaotaomaomao/articles/986070.htmlhttp://www.cnblogs.com/TankXiao/archive/2012/02/13/2342672.html HTTP協議詳解post方式相比get安全,攜帶數據更大,我準備所有數據都用post方式獲取,這樣好嗎?http://segmentfault.com/q/1010000000213082 http://www.cnblogs.com/hyddd/archive/2009/04/09/1432744.html 淺談CSRF攻擊方式
?
Post 與GET請求的對比.解釋.
https://my.oschina.net/leejun2005/blog/136820
?
?
?
?
?
在IE8 下的URL地址總長度為:4076,超過該長度會自動忽略后面的內容;
在firefox 25下的URL地址總長度可以達到:7530,超過該長度會訪問錯誤;
在chrome?29.0.1547.62?的最大總長度達到:7675,超過該長度會訪問錯誤;
https://www.jianshu.com/p/512389822f8b? ?關于 post 與get請求參數長度限制的問題,(其實get與post都沒有長度限制.)
?https://blog.csdn.net/xnf1991/article/details/52157378 ? post 與GET的區別
?
?
ORM(object rational mapping )
對象關系映射(英語:(Object Relational Mapping,簡稱ORM,或O/RM,或O/R mapping),是一種程序技術,用于實現面向對象編程語言里不同類型系統的數據之間的轉換 。從效果上說,它其實是創建了一個可在編程語言里使用的--"虛擬對象數據庫"。
?增刪改查
1. ?查 ?
?暫時注釋掉 csrf, 在mysite->settings.py配置文件中
?
MIDDLEWARE = ['django.middleware.security.SecurityMiddleware','django.contrib.sessions.middleware.SessionMiddleware','django.middleware.common.CommonMiddleware', # 'django.middleware.csrf.CsrfViewMiddleware','django.contrib.auth.middleware.AuthenticationMiddleware','django.contrib.messages.middleware.MessageMiddleware','django.middleware.clickjacking.XFrameOptionsMiddleware', ]?
settings里的templates文件不動
TEMPLATES = [{'BACKEND': 'django.template.backends.django.DjangoTemplates','DIRS': [os.path.join(BASE_DIR, 'templates')],'APP_DIRS': True,'OPTIONS': {'context_processors': ['django.template.context_processors.debug','django.template.context_processors.request','django.contrib.auth.context_processors.auth','django.contrib.messages.context_processors.? 在setting里設置app配置文件 (不需要)
INSTALLED_APPS = ['django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles', 'app01.apps.App01Config',在 app01->apps.py中設置,(不需要設置)
from django.apps import AppConfigclass App01Config(AppConfig):name = 'app01'?
設置靜態文件的路徑 并新建一個static文件夾存放 靜態文件.
STATIC_URL = '/static/' STATICFILES_DIRS=[os.path.join(BASE_DIR,'static') ]?設置路徑的對應關系,在mysite的urls.py 文件中進行設置.
from django.conf.urls import url from django.contrib import admin from app01 import viewsurlpatterns = [url(r'^admin/', admin.site.urls), url(r'^Publisher_list',views.Publisher_list)將bootstrap、jquery 庫放到static目錄下?
在 app01->modules里設置:
from django.db import models# Create your models here. #圖書管理系統,書,作者,出版社#出版社 class Publisher(models.Model):id = models.AutoField(primary_key=True) #自增的ID主鍵#創建一個varchar(64)的唯一的不為空的字段name= models.CharField(max_length=64,null=False,unique=True)設置數據庫文件在settings文件夾下?
DATABASES = {'default':{#連接數據庫類型'ENGINE':'django.db.backends.mysql',#連接數據庫地址'HOST':'127.0.0.1',#數據庫名稱'NAME':'day62',#用戶'USER':'root',#密碼'PASSWORD':'123456'} }?
?
?
設置對應的函數.
from django.shortcuts import render,HttpResponse,redirect from app01 import models # Create your views here. #展示出版社列表 def Publisher_list(request):# 去數據庫查出所有的出版社,填充到htnml中,給用戶返回ret =models.Publisher.objects.all().order_by('id')return render(request,'publisher_list.html',{'publisher_list':ret})
publisher_list html文件新建一個放在template里
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>publisher_list</title> </head> <body> <table><thead><tr><th>ID</th><th>序號</th><th>書名</th></tr></thead><tbody> {% for publisher in publisher_list %} <tr> <td>{{ forloop.counter }}</td> <td>{{ publisher.id }}</td> <td>{{ publisher.name }}</td> <td>
<a href="/delete_publisher/?id={{ publisher.id }}">刪除</a>
<a href="/edit_publisher/?id={{ publisher.id }}">編輯</a>
</td>
</tr>
查看頁面
?
?
?
?
?
?
?
??
?二、增
在url里添加路徑對應關系
urlpatterns = [url(r'^admin/', admin.site.urls),url(r'^Publisher_list/',views.Publisher_list), url(r'^Publisher_add/',views.Publisher_add)?
在views里添加代碼
def Publisher_add(request):# error_msg =''
#如果是POST請求,我就會取到用戶填寫的數據
if request.method =='POST':
new_name =request.POST.get('publisher_name',None)
if new_name:
#通過ORM去數據庫里新建一條記錄
models.Publisher.objects.create(name=new_name)
#引導用戶訪問出版社列表頁,查看是否添加成功-->
return redirect('/Publisher_list/')
# else: error_msg ='出版社名字不能為空'
return render(request,'publisher_add.html')
定義靜態網頁 publisher_add.html
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>publisher_add</title> </head> <body> <h1>添加出版社</h1> <form action="/Publisher_add/"method="post"><input type="text" name=" publisher_name"><input type="submit"value="提交"><p style="color: red">{{ error }}</p> </form> </body> </html>
訪問頁面
?查看到的日志信息?
Not Found: /favicon.ico [03/May/2018 14:57:04] "GET /favicon.ico HTTP/1.1" 404 2193 [03/May/2018 14:57:05] "GET /Publisher_add/ HTTP/1.1" 200 327 [03/May/2018 14:57:17] "POST /Publisher_add/ HTTP/1.1" 302 0 [03/May/2018 14:57:17] "GET /Publisher_list/ HTTP/1.1" 200 1080
?
?
三、刪除
?① 在urls里添加如下代碼,如紅色表示:
from django.conf.urls import url from django.contrib import admin from app01 import viewsurlpatterns = [url(r'^admin/', admin.site.urls),url(r'^Publisher_list/',views.Publisher_list),url(r'^Publisher_add/',views.Publisher_add), url(r'^Publisher_del/',views.Publisher_del),
② 更改 publisher_list html頁面 如紅色表示的
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>publisher_list</title> </head> <body> <table border="2"><thead><tr><th>ID</th><th>序號</th><th>書名</th><th>操作</th></tr></thead><tbody>{% for publisher in publisher_list %}<tr><td>{{ forloop.counter }}</td><td>{{ publisher.id }}</td><td>{{ publisher.name }}</td><td> <a href="/Publisher_del/?id={{ publisher.id }}">刪除</a> # Publisher_del 為url 的路徑,不能寫錯<a href="/Publisher_del/?id={{ publisher.name }}">編輯</a> # Publisher_del 為url 的路徑,不能寫錯 </td> </tr> {% endfor %}</tbody>
</table>
</body>
</html>
③ 編輯 publisher_del 函數?
?
def Publisher_del(request):print(request.GET)print('='*120)#刪除指定的數據#1.從 GET請求的參數里面拿到將要刪除的數據的id值del_id = request.GET.get('id',None)#字典取值,取不到默認為None GET 請求獲取數據#如果能取到id值if del_id:#去數據庫刪除當前id值的數據#根據id值查找到數據del_obj = models.Publisher.objects.get(id=del_id) 通過orm查找數據庫字段print(del_obj)#刪除數據del_obj.delete()#返回刪除后的頁面,跳轉到出版社的列表頁,查看刪除是否成功return redirect('/Publisher_list/')else:return HttpResponse('要刪除的數據不存在')打印出來的結果:
[03/May/2018 15:46:11] "GET /favicon.ico HTTP/1.1" 404 2308 [03/May/2018 15:46:12] "GET /Publisher_list/ HTTP/1.1" 200 1332 <QueryDict: {'id': ['4']}> ======================================================================================================================== Publisher object [03/May/2018 15:46:14] "GET /Publisher_del/?id=4 HTTP/1.1" 302 0 [03/May/2018 15:46:14] "GET /Publisher_list/ HTTP/1.1" 200 1094?
頁面的樣式:
四、編輯
修正下html文件
?
?更改urls文件
from django.conf.urls import url from django.contrib import admin from app01 import viewsurlpatterns = [url(r'^admin/', admin.site.urls),url(r'^Publisher_list/',views.Publisher_list),url(r'^Publisher_add/',views.Publisher_add),url(r'^Publisher_del/',views.Publisher_del),url(r'^Publisher_edit/',views.Publisher_edit)
views文件
def Publisher_edit(request):# 用戶修改完出版社的名字,點擊提交按鈕,給我發來新的出版社名字print(request.method)if request.method == 'POST': print(request.POST)# 結果為GET # 獲取新的出版社名字edit_id = request.POST.get('id')new_name = request.POST.get('publisher_name')# 更新出版社# 根據id取到編輯的是哪個出版社edit_publisher = models.Publisher.objects.get(id=edit_id)edit_publisher.name = new_nameedit_publisher.save() # 把修改同步提交到數據庫。return redirect('/Publisher_list/')# 跳轉到出版社列表頁,查看是否修改成功.else:# 從GET請求的URL中取到ID參數edit_id = request.GET.get('id', None) print(edit_id) #結果為 11if edit_id:# 獲取到當前編輯的出版社對象;publisher_obj = models.Publisher.objects.get(id=edit_id) print(publisher_obj) #結果為 11,dfdafreturn render(request, 'publisher_edit.html', {'publisher': publisher_obj})else:return HttpResponse('編輯的出版社不存在')
靜態html文件
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>publisher_edit</title> </head> <body> <h1>編輯出版社</h1><form action="/Publisher_edit/" method ='post'><input type="text" name = 'id' value="{{ publisher.id }}"style="display: none"><input type="text" name="publisher_name" value="{{ publisher.name }}"><input type="submit" value="提交"><p style="color: red">{{ error }}</p> </form> </body> </html>執行點擊編輯按鍵
?打印結果?
GET
11
11,dfdfda
[03/May/2018 21:10:13] "GET /Publisher_edit/?id=11 HTTP/1.1" 200 414
?
轉載于:https://www.cnblogs.com/mengbin0546/p/8980207.html
總結
以上是生活随笔為你收集整理的Day 62 Django第三天的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: oracle 11g ocp 笔记(15
- 下一篇: vue骨架屏、时间选择器、轮播图。。你想