Skip to content

Commit af57c70

Browse files
author
DESKTOP-DH18EE5\Tao
committed
update
1 parent 516aa01 commit af57c70

4 files changed

Lines changed: 35 additions & 30 deletions

File tree

378 Bytes
Binary file not shown.

two/djangoAPI/api/views.py

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def indexGET(request):
1010
# request.body: {}
1111
# request.content_type: {}
1212
# request.method: {}
13-
# request.META.HTTP_HOST: {}
13+
# request.META['HTTP_HOST']: {}
1414
# """.format(
1515
# request.path,
1616
# request.body,
@@ -29,31 +29,32 @@ def indexGET(request):
2929
# # write header
3030
# rtnRes.__setitem__('memo', 'Go home kid')
3131

32-
# #-------------------------------------
33-
# # deal with GET
34-
35-
# if request.method == 'GET':
36-
# # 刪掉網址中的 /api/
37-
# preRe= re.compile('^/api/')
38-
# apiName = preRe.sub('', request.path)
39-
40-
# parameterGET = dict(request.GET)
41-
# # http://localhost:8000/api/index?aaa=b;hhh=t
42-
# # {'aaa': ['b'], 'hhh': ['t']}
43-
# for eachP in parameterGET:
44-
# if len(parameterGET[eachP])==1:
45-
# parameterGET[eachP] = parameterGET[eachP][0]
32+
#-------------------------------------
33+
# deal with GET
34+
if request.method == 'GET':
35+
# 刪掉網址中的 /api/
36+
preRe= re.compile('^/api/')
37+
apiName = preRe.sub('', request.path)
38+
#playData
39+
parameterGET = dict(request.GET)
40+
# http://localhost:8000/api/index?aaa=b;hhh=t
41+
# {'aaa': ['b'], 'hhh': ['t']}
42+
for eachP in parameterGET:
43+
if len(parameterGET[eachP])==1:
44+
parameterGET[eachP] = parameterGET[eachP][0]
4645

47-
# data = ''
48-
# try:
49-
# data = getattr(apiList, apiName)(parameterGET)
50-
# if data['status']:
51-
# data = json.dumps(data['data'])
52-
# else:
53-
# data = data['err']
54-
# except:
55-
# data = '沒有這支 api'
56-
# # http://localhost:8000/api/playData?name=Charmander
46+
data = ''
47+
try:
48+
data = getattr(apiList, apiName)(parameterGET)
49+
if data['status']:
50+
data = json.dumps(data['data'])
51+
else:
52+
data = data['err']
53+
except:
54+
data = '沒有這支 api'
55+
# http://localhost:8000/api/playData?name=Charmander
56+
else:
57+
data='你只能用GET'
5758
rtnRes.write(data)
5859
return rtnRes
5960

@@ -64,13 +65,11 @@ def indexPOST(request):
6465
if request.method == 'POST':
6566
if request.content_type == 'application/json':
6667
parameterPOST = json.loads( str(request.body, encoding='utf8') )
67-
apiName = parameterPOST['api']
6868
try:
6969
apiName = parameterPOST['api']
7070
data = getattr(apiList, apiName)(parameterPOST)
7171
except:
7272
data = "api 為必要欄位"
73-
7473
else:
7574
data = "如用 GET 請加上 api 名稱"
7675

4 Bytes
Binary file not shown.

two/djangoAPI/djangoAPI/settings.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,25 @@
3737
'django.contrib.sessions',
3838
'django.contrib.messages',
3939
'django.contrib.staticfiles',
40-
'api'
40+
'api',
41+
# 'corsheaders',
4142
]
4243

4344
MIDDLEWARE = [
4445
'django.middleware.security.SecurityMiddleware',
4546
'django.contrib.sessions.middleware.SessionMiddleware',
4647
'django.middleware.common.CommonMiddleware',
47-
# 'django.middleware.csrf.CsrfViewMiddleware',
48+
#'django.middleware.csrf.CsrfViewMiddleware',
49+
#'corsheaders.middleware.CorsMiddleware',
50+
'django.middleware.common.CommonMiddleware',
4851
'django.contrib.auth.middleware.AuthenticationMiddleware',
4952
'django.contrib.messages.middleware.MessageMiddleware',
5053
'django.middleware.clickjacking.XFrameOptionsMiddleware',
5154
]
52-
55+
# CORS_ORIGIN_WHITELIST = (
56+
# 'localhost:8050',
57+
# )
58+
# CORS_ALLOW_CREDENTIALS=True
5359
ROOT_URLCONF = 'djangoAPI.urls'
5460

5561
TEMPLATES = [

0 commit comments

Comments
 (0)