如需了完整內容請訪問:https://www.emperinter.info/2022/05/31/django-view/
settings.py
中指定根級URL配置文件:ROOT_URLCONF = 'project.urls'
,默認實現了。urlpatterns
: /
,一般在最後加r
表示字符串不轉義urls.py
文件,定義本應用的url配置,在工程urls.py
文件中使用include()
方法: url(r'^', include('myApp.urls',namespace='myApp')),
request
一個HttpRequest的實例views.py
中定義templates
下定義404.html
request_path
:導致出錯的網址。settings.py
DEBUG
如果為True
永遠不會調用404.html頁面ALLOWED_HOSTS = [*]
HttpRequest
對象HttpRequest
對象path
method
encoding
utf-8
GET
POST
FILES
COOKIES
session
is_ajax()
XMLHttpRequest
發起的,返回True
get()
www.baidu.com/abc?a=1&b=2&c=3
getlist()
www.baidu.com/abc?a=1&a=2&c=3
(注意有兩個a)# 獲取get傳遞的數據
def get1(request):
a = request.GET.get("a")
b = request.GET["b"]
c = request.GET.get("c")
return HttpResponse(a + " " + b + " " + c)
def get2(request):
a = request.GET.getlist('a')
a1 = a[0]
a2 = a[1]
b = request.GET.get('c')
return HttpResponse(a1 + " " + a2 + " " + b)
CSRF
: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',
]
def showregister(request):
name = request.POST.get("username")
gender = request.POST.get("gender")
age = request.POST.get("age")
hobby = request.POST.getlist("hobby") # checkbox
info = str("name:\t" + name + "\tgender:" +gender + "\tage:" + age + "\thobby"+ str(hobby))
print(info)
return HttpResponse(info)
如需了解更多請訪問:https://www.emperinter.info/2022/05/31/django-view/