How do I write a single-file Django application?

django, python, web-applications

Solution

You might want to consider Simon Willison's library:

- djng—a Django powered microframework

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)

[...]

Problem

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).

Original source

Related problems