Django简易json api接口封装。
Project description
django-apis
Django简易json api接口框架。使用Django Form做接口参数验证。
安装
pip install django-apis
使用
简单的接口开发
- 所有视图参数按django标准,添加request或其它路径参数。
- 可以定义Form来完成请求参数的校验。在检验不通过的情况下,抛出
django_apis.exceptions.ValidationError
。django_apis.exceptions.ValidationError
会把django的异常信息转化为文本格式。 apiview
中methods
参数表示允许的HTTP请求类型。可以是字符串,也可以是字符串数组。
from django.forms import Form
from django.forms.fields import CharField
from django_apis.views import get_apiview
from django_apis.views import get_json_payload
from django_apis.exceptions import ValidationError
apiview = get_apiview()
@apiview(methods="GET")
def ping(request):
return "pong"
class EchoInput(Form):
msg = CharField(max_length=16, required=True)
@apiview(methods="POST")
def echo(request):
payload = get_json_payload(request)
form = EchoInput(payload)
if form.is_valid():
return form.cleaned_data["msg"]
else:
raise ValidationError(form)
@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.1
- 版本首发。
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.1.tar.gz
(8.9 kB
view details)
Built Distribution
File details
Details for the file django-apis-0.1.1.tar.gz
.
File metadata
- Download URL: django-apis-0.1.1.tar.gz
- Upload date:
- Size: 8.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6c4887821c963fc02b771c4d30583d569d76de7691e72712931ba3a1c9e4bb67 |
|
MD5 | cf74011d4c9be9097e09800e0507e222 |
|
BLAKE2b-256 | b652ca82c196be1bc3b60b646b7d67623409a34251b58cf43ef91dc9e7b3b1bd |
File details
Details for the file django_apis-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: django_apis-0.1.1-py3-none-any.whl
- Upload date:
- Size: 11.1 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 | b9e38503498bf356b9e334bbdcbac07ac2fb35eb86ed64767c8d6a3c65c74960 |
|
MD5 | aa3d8eabaf911367169e59df0b5aded9 |
|
BLAKE2b-256 | dcdfbbc6c84cfa48369234205d95cf24e5d4b088f9fedb5e9e202c63e05a847a |