import os
import sys
# settings.py部分:
from django.conf import settings
DEBUG = os.environ.get('DEBUG', 'on') == 'on'
SECRET_KEY = os.environ.get('SECRET_KEY', os.urandom(32))
ALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS', 'localhost').split(',')
settings.configure(
DEBUG=DEBUG,
SECRET_KEY=SECRET_KEY,
ALLOWED_HOSTS=ALLOWED_HOSTS,
ROOT_URLCONF=__name__,
MIDDLEWARE_CLASSES=(
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
),
)
from django.core.wsgi import get_wsgi_application
# view.py部分:
from django.http import HttpResponse
def index(request):
return HttpResponse('Hello World')
# url.py部分:
from django.conf.urls import url
urlpatterns = (
url(r'^$', index),
)
application = get_wsgi_application()
if __name__ == "__main__":
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
Web服务器网关接口(WSGI)关于Web服务器如何与Django之类的应用框架通信的规范,有PEP333指定,并在PEP3333中改进。有许多使用WSGI规范的Web服务器,包括apache下的mod_wsgi, Gunicorn, uWSGI, CherryPy, Tornado 和 Chaussette。
gunicorn hello --log-file=-
export DEBUG=off
export ALLOWED_HOSTS=localhost,example.com
python hello.py runserver