johnmahugu

python -directly runnable django in a single file

Jun 11th, 2015
824
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.27 KB | None | 0 0
  1. '''
  2. python -directly runnable django in a single file
  3.  
  4. Save as djangol.py ,Run with $ python ./micro_django.py and go to http://localhost:8000/Foo
  5. '''
  6. import os, sys
  7. from django.conf.urls.defaults import patterns
  8. from django.template.response import TemplateResponse
  9.  
  10. # this module
  11. me = os.path.splitext(os.path.split(__file__)[1])[0]
  12. # helper function to locate this dir
  13. here = lambda x: os.path.join(os.path.abspath(os.path.dirname(__file__)), x)
  14.  
  15. # SETTINGS
  16. DEBUG=TEMPLATE_DEBUG=True
  17. ROOT_URLCONF = me
  18. DATABASES = { 'default': {} } #required regardless of actual usage
  19. TEMPLATE_DIRS = (here('.'), )
  20.  
  21. # VIEW
  22. def index(request, name):
  23.     return TemplateResponse(request, 'index.html', {'name': name})
  24.  
  25. # URLS
  26. urlpatterns = patterns('', (r'^(?P<name>\w+)?$', index))
  27.  
  28. if __name__=='__main__':
  29.     # set the ENV
  30.     os.environ['DJANGO_SETTINGS_MODULE'] = me
  31.     sys.path += (here('.'),)
  32.     # run the development server
  33.     from django.core import management
  34.     management.call_command('runserver','0.0.0.0:8000' )
  35. '''
  36. if __name__=='__main__':
  37.    # set the ENV
  38.    os.environ['DJANGO_SETTINGS_MODULE'] = me
  39.    sys.path += (here('.'),)
  40.    # run the development server
  41.    from django.core import management
  42.    management.execute_from_command_line()
  43. '''
Advertisement
Add Comment
Please, Sign In to add comment