本脚本基于python2.7 django 1.11.4
python -v 查看python版本
python -m django --version 查看django版本
使用命令python hello.py runserver 运行脚本。
使用浏览器访问127.0.0.1:8000即可见HelloWorld。
# @Author: user
# @Date: 2017-11-27T21:50:33+08:00
# @Last modified by: user
# @Last modified time: 2017-11-27T23:09:03+08:00
import sys,os
from django.conf import settings
settings.configure(
DEBUG=True,
SECRET_KEY='thisisthesecrekey',
ROOT_URLCONF = '__main__',
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_page(request):
return HttpResponse('Hello World')
urlpatterns = (
url(r'^$',index_page),
)
if __name__ == '__main__':
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
备注:MIDDLEWARE_CLASSES 一个都不能少。