Skip to content

Commit 516aa01

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

8 files changed

Lines changed: 40 additions & 14 deletions

File tree

7 Bytes
Binary file not shown.
338 Bytes
Binary file not shown.
-9 Bytes
Binary file not shown.

two/djangoAPI/api/api/playData.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
def playData(par):
44

55
try:
6-
name = par['name'][0]
6+
name = par['name']
77
except:
88
return {
99
'status': False,

two/djangoAPI/api/urls.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
from . import views
33

44
urlpatterns = [
5-
url(r'^$', views.empty),
6-
url(r'.*', views.index)
5+
url(r'^$', views.indexPOST),
6+
url(r'.*', views.indexGET)
77
]

two/djangoAPI/api/views.py

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44

55
from . import apiList as apiList
66

7-
def empty(request):
8-
return HttpResponse("You need to give me an api name")
9-
10-
def index(request):
7+
def indexGET(request):
118
# resStr = """
129
# request.path: {}
1310
# request.body: {}
@@ -35,23 +32,52 @@ def index(request):
3532
# #-------------------------------------
3633
# # deal with GET
3734

38-
# preRe= re.compile('^/api/')
39-
# applicationPath = preRe.sub('', request.path)
40-
# print(applicationPath)
41-
4235
# if request.method == 'GET':
36+
# # 刪掉網址中的 /api/
37+
# preRe= re.compile('^/api/')
38+
# apiName = preRe.sub('', request.path)
39+
4340
# parameterGET = dict(request.GET)
4441
# # http://localhost:8000/api/index?aaa=b;hhh=t
4542
# # {'aaa': ['b'], 'hhh': ['t']}
43+
# for eachP in parameterGET:
44+
# if len(parameterGET[eachP])==1:
45+
# parameterGET[eachP] = parameterGET[eachP][0]
46+
4647
# data = ''
4748
# try:
48-
# data = getattr(apiList, applicationPath)(parameterGET)
49+
# data = getattr(apiList, apiName)(parameterGET)
4950
# if data['status']:
5051
# data = json.dumps(data['data'])
5152
# else:
5253
# data = data['err']
5354
# except:
5455
# data = '沒有這支 api'
56+
# # http://localhost:8000/api/playData?name=Charmander
57+
rtnRes.write(data)
58+
return rtnRes
5559

56-
# rtnRes.write(data)
60+
def indexPOST(request):
61+
rtnRes = HttpResponse()
62+
# #-------------------------------------
63+
# # deal with POST NEED TO DISABLE CSRF
64+
if request.method == 'POST':
65+
if request.content_type == 'application/json':
66+
parameterPOST = json.loads( str(request.body, encoding='utf8') )
67+
apiName = parameterPOST['api']
68+
try:
69+
apiName = parameterPOST['api']
70+
data = getattr(apiList, apiName)(parameterPOST)
71+
except:
72+
data = "api 為必要欄位"
73+
74+
else:
75+
data = "如用 GET 請加上 api 名稱"
76+
77+
rtnRes.write(data)
5778
return rtnRes
79+
80+
# {
81+
# "api": "playData",
82+
# "name": "Charmander"
83+
# }
-47 Bytes
Binary file not shown.

two/djangoAPI/djangoAPI/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
'django.middleware.security.SecurityMiddleware',
4545
'django.contrib.sessions.middleware.SessionMiddleware',
4646
'django.middleware.common.CommonMiddleware',
47-
'django.middleware.csrf.CsrfViewMiddleware',
47+
# 'django.middleware.csrf.CsrfViewMiddleware',
4848
'django.contrib.auth.middleware.AuthenticationMiddleware',
4949
'django.contrib.messages.middleware.MessageMiddleware',
5050
'django.middleware.clickjacking.XFrameOptionsMiddleware',

0 commit comments

Comments
 (0)