如何编写单文件 Django 应用程序? [英] How do I write a single-file Django application?

查看:27
本文介绍了如何编写单文件 Django 应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在单个文件中编写一个非常小的 Django 应用程序,需要所有适当的模块和内容,然后能够将其作为普通 Python 脚本运行,如下所示:

I want to write a very small Django application in a single file, requiring all the appropriate modules and stuff, and then be able to run that as a normal Python script, like this:

$ python myapp.py

你可以假设我不会渲染 HTML,所以我不需要模板(我会返回 JSON 或其他一些自动生成的字符串).

You can assume I won't render HTML, so I don't need templates (I'll return JSON or some other auto-generated string).

推荐答案

您可能需要考虑 Simon Willison 的图书馆:

You might want to consider Simon Willison's library:

来自自述:

djng 是一个依赖于宏框架 (Django) 的微框架.

djng is a micro-framework that depends on a macro-framework (Django).

我对微框架的定义:可以让你创建一个完整的单个模块中的 Python Web 应用程序:

My definition of a micro-framework: something that lets you create an entire Python web application in a single module:

import djng

def index(request):
    return djng.Response('Hello, world')

if __name__ == '__main__':
    djng.serve(index, '0.0.0.0', 8888)

[...]

这篇关于如何编写单文件 Django 应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆