* Request 보내기
## python
>>> import requests, json
>>> tmpData = {
"cond" : {
"VIENNA_CODE" : "0101"
}
}
>>> jsonData = json.dumps(tmpData)
>>> r = requests.post("http://localhost:8888/mongodb/", data=jsonData)
## 오류일경우
>>> r.status_code
500
>>> with open("d:\\err.html", "w", encoding="utf-8") as f:
f.write(r.text)
## 정상일경우
>>> r.status_code
200
* Django API 만들기
## urls.py
from django.conf.urls import url
from django.contrib import admin
from tripletBuilder import views
from tripletBuilder import mongo
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^mongodb/', mongo.getMongo),
]
## mongo.py
@csrf_exempt
def getMongo(request):
''' AJAX 조회 '''
logger.info('## getMongo ########################################################')
cond_json = json.loads(request.body.decode('utf-8'))
cond = cond_json["cond"]
print(cond)
```
{
"VIENNA_CODE" : "0101"
}
```
result_json = cond
return JsonResponse(result_json)
'Python > Django' 카테고리의 다른 글
[Django] 장고 간단하게 시작하기 메뉴얼 (0) | 2019.07.25 |
---|