How do I write a single-file Django application?

后端 未结 11 1867
余生分开走
余生分开走 2020-12-14 19:29

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, li

相关标签:
11条回答
  • 2020-12-14 20:03

    The code below represents a single file django app. It was shamelessly copied from this interesting book chapter, and can be run using python singlefile.py runserver (if the file is called singlefile.py). Works at least in Django 1.9 and 2.0.

    import sys
    from django.conf import settings
    
    settings.configure(
        DEBUG=True,
        SECRET_KEY='thisisthesecretkey',
        ROOT_URLCONF=__name__,
        MIDDLEWARE_CLASSES=('django.middleware.common.CommonMiddleware',
                            'django.middleware.csrf.CsrfViewMiddleware',
                            'django.middleware.clickjacking.XFrameOptionsMiddleware')
    )
    
    from django.conf.urls import url
    from django.http import HttpResponse
    
    
    def index(request): return HttpResponse('Hello World')
    
    urlpatterns = (url(r'^$', index), )
    
    if __name__ == "__main__":
        from django.core.management import execute_from_command_line
        execute_from_command_line(sys.argv)
    

    ... it does look a lot like the answer by un1t, except for some details.

    0 讨论(0)
  • 2020-12-14 20:04

    You also may want to take a look at web.py. (Tutorial)

    It's another compact, but powerful web framework.

    Sample from the main page:

    import web
    
    urls = ('/(.*)', 'hello')
    app = web.application(urls, globals())
    
    class hello:        
        def GET(self, name):
            if not name: 
                name = 'world'
            return 'Hello, ' + name + '!'
    
    if __name__ == "__main__":
        app.run()
    
    0 讨论(0)
  • 2020-12-14 20:06

    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)
    

    [...]

    0 讨论(0)
  • 2020-12-14 20:11

    Well, the easiest way to do that is to mimic the django project arbo in one file. So in one module, assure there is :

    Root_module :
        Root_module.settings
        Root_module.urls
        Root_module.app_in_the_module
        Root_module.app_in_the_module.models
        Root_module.app_in_the_module.views
    

    Then code is as a normal Django project. What you must know is that Django does not need anything to be in a specific place. Standard names and paths are at beat, convention, at worst, shortcut to prevent you from defining a setting.

    If you know Django very well, you don't even need to mimic the arbo, just write you django app making all the data from the above modules interconnected the way they should be.

    0 讨论(0)
  • 2020-12-14 20:15

    Then what you need is not Django. What you need is exactly what micropy does.

    0 讨论(0)
提交回复
热议问题