单文件 Django 服务构建
source link: https://pylixm.top/posts/2020-11-13-django-single-file.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
今天在使用vue框架开发系统的时候,需要用到后端接口,但是接口还没有开发。便想使用最简单的方式构建一个极简的API服务,由于本人偏爱Django框架,便想能不能用Django框架简单的构建想Flask那样的单文件web服务。果然,被我找到了,摘录分享给大家。
# app.py
import os
import sys
from dataclasses import dataclass
from django.conf import settings
from django.core.wsgi import get_wsgi_application
from django.http import HttpResponseRedirect, JsonResponse
from django.urls import path
from django.utils.crypto import get_random_string
# django 的配置文件,相当于 settings.py 配置文件,可直接在这里添加和删除
# 下边已经是最简的Django 启动需要的参数了
settings.configure(
DEBUG=(os.environ.get("DEBUG", "") == "1"),
ALLOWED_HOSTS=["*"], # Disable host header validation
ROOT_URLCONF=__name__, # Make this module the urlconf
SECRET_KEY=get_random_string(
50
), # We aren't using any security features but Django requires this setting
MIDDLEWARE=["django.middleware.common.CommonMiddleware"],
)
# 使用dataclass 装饰器模拟了 model。
@dataclass
class Character:
name: str
age: int
def as_dict(self, id_):
return {
"id": id_,
"name": self.name,
"age": self.age,
}
# 列举数据,完全可以直接使用字段来构造需要返回的数据
characters = {
1: Character("Rick Sanchez", 70),
2: Character("Morty Smith", 14),
}
# Django View 部分,为方便简单直接使用 function base view
def index(request):
return HttpResponseRedirect("/characters/")
def characters_list(request):
return JsonResponse(
{"data": [character.as_dict(id_) for id_, character in characters.items()]}
)
def characters_detail(request, character_id):
try:
character = characters[character_id]
except KeyError:
return JsonResponse(
status=404,
data={"error": f"Character with id {character_id!r} does not exist."},
)
return JsonResponse({"data": character.as_dict(character_id)})
# Django route 部分,同 url.py
urlpatterns = [
path("", index),
path("characters/", characters_list),
path("characters/<int:character_id>/", characters_detail),
]
# 构建 uwsgi 使用 application 可使用 uwsgi 等 WSGI 协议的软件启动。
app = get_wsgi_application()
if __name__ == "__main__":
# 引入命令行启动方式函数,方便直接启动测试, 同 manage.py 文件
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
直接运行如下命令即可启动服务:
python app.py runserver
还可以使用 uwsgi 等应用程序启动。如使用 gunicorn 如下:
gunicorn app:app
希望可以帮助到你~
更多的Django 单文件的测试及构建方法可以到Adam Johnson(Django 技术委员会成员,英国Django MeetUp的组织者)的博客查阅。
Recommend
-
61
lamp-配置防盗链,防止图片等文件被其他网站引用,防止增加本网站的带宽成本;访问控制Directory(针对目录)、访问控制(针对单文件),对敏感网页比如admin后台登录等进行访问控制,只允许指定ip访问;
-
33
cmdp 命令行和单文件快捷提示、下载、执行工具(远程同步) 功能 单行文本 (command,code,path,content...) 上...
-
5
近日,据XDA论坛爆料, 谷歌可能会在Android 11.0上取消经典的4GB文件大小限制。 谷歌是在2014年作出这个决定的,当时市面上内置存储最大的手机仅有32GB,SD卡也广泛流行,拥有4K视频录制功能的手机也寥寥无几,...
-
19
关于 .NET 发布单文件应用 单文件应用简介 .NET Core 3.x 发布的单文件应用是自解压程序, 解压路径貌似是通过 System.IO.Path.GetTempPath() 来确定的: Windows 系统 %TEMP%\.net\
-
12
.Net 5下的单文件部署 由于.net程序没有静态链接,一直缺乏单文件部署这种干净的发布方案。对客户端程序发布并不是...
-
6
编译vue单文件组件 分类:技术 | 9月 11th, 2018 | 2,700 views |
-
4
版权声明: 本文仅用于学习、研究和交流目的,欢迎非商业性质转载。本文链接:https://ldqk.org/1483。● 下载遇到问题,请先阅读网站公告!如遇资源报毒,请参阅:
-
8
sword群晖修改Cloud Sync上传OneDrive单文件15GB大小限制前言Cloud Sync可以使群晖轻松连接到公共云服务,如百度云、Dropbox、Google Cloud Sto...扫描右侧二维码阅读全文282021/06...
-
5
V2EX › Python 2021 年了, 有没有什么好办法可以拿到一个单文件(或者绿色版) Python 解释器? Cl...
-
3
前端如何进行单文件上传云服务存储 作者:季节 2023-06-01 07:36:11 不知道大家日常使用的上传方式是否和我们团队一致,之前上传文件方案中,我司后端团队会提供一个后端上传服务接口,前端直接使用这个接口进行文...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK