Django简易json api接口封装。
Project description
django-apis
Django简易json api接口框架。使用Django Form做接口参数验证。
安装
pip install django-apis
使用
简单的接口开发
- 所有视图参数按django标准,添加request或其它路径参数。
- 可以定义Form来完成请求参数的校验。
- 在检验不通过的情况下,抛出
django_apis.exceptions.InputValidationError
。 django_apis.exceptions.InputValidationError
会把django的异常信息转化为文本格式。
- 在检验不通过的情况下,抛出
apiview
中methods
参数表示允许的HTTP请求类型。- 可以是字符串,如:
"get"
。 - 也可以是字符串数组,如:
["get", "post"]
。 - 大小写均可。
- 可以是字符串,如:
forms.py
from django.forms import Form
from django.forms.fields import CharField
class EchoInput(Form):
msg = CharField(max_length=16, required=True)
views.py
from django_apis.views import get_apiview
from django_apis.views import get_json_payload
from django_apis.exceptions import InputValidationError
from .forms import EchoInput
apiview = get_apiview()
@apiview(methods="GET")
def ping(request):
return "pong"
@apiview(methods="POST")
def echo(request):
payload = get_json_payload(request)
form = EchoInput(payload)
if not form.is_valid():
raise InputValidationError(form)
return form.cleaned_data["msg"]
@apiview(methods="GET")
def getRequestInfo(request):
result = {}
for key, value in request.META.items():
if isinstance(value, str):
result[key] = value
return result
配置项
- DJANGO_APIS_ALLOWED_METHODS
- DJANGO_APIS_APIVIEW
- DJANGO_APIS_ENABLE_REQUEST_LOG
- DJANGO_APIS_REQUEST_LOG_NAME
版本记录
v0.1.4
- 版本首发。
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
django-apis-0.1.4.tar.gz
(9.5 kB
view details)
Built Distribution
File details
Details for the file django-apis-0.1.4.tar.gz
.
File metadata
- Download URL: django-apis-0.1.4.tar.gz
- Upload date:
- Size: 9.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0def2f70c85ca62226cc9b0bee05fde05fd93c25bf80c2f80696dee25579c5af |
|
MD5 | d56c6899d0caeb04da54af0edbee1688 |
|
BLAKE2b-256 | c58bb4cd35e7ec9ec854772b0a7c3026cc3a2a45ae514e617c9bbb3e8230b855 |
File details
Details for the file django_apis-0.1.4-py3-none-any.whl
.
File metadata
- Download URL: django_apis-0.1.4-py3-none-any.whl
- Upload date:
- Size: 11.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a7dd00d9726094d253f09cffb371293b82ebb3e5654c1b4067935eaaa1033557 |
|
MD5 | cf5b590234cac277fa0bb5bad2914d49 |
|
BLAKE2b-256 | 89ad697da8a4a4f44e0a1c8afbfcb2c2b9ca9d8dce7f551158ccec1ac59db662 |