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

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

问题描述

我想在一个文件中编写一个非常小的Django应用程序,需要所有相应的模块和内容,然后可以像普通的Python脚本一样运行,如下所示:

  $ python myapp.py 

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

解决方案

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





readme


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



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

  import djng 

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

如果__name__ =='__main__':
djng。 serve(index,'0.0.0.0',8888)

[...]



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

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

解决方案

You might want to consider Simon Willison's library:

From the readme:

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

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天全站免登陆