pip 安装 Django
$ pip install Django
世界上最小的Django项目
你好Django
创建视图
vi hello.py
from django.http import HttpResponse
def index(request):
return HttpResponse('Hello World')
URL模式
把视图绑定到网站结构上
在 hello.py 中创建关联
from django.conf.urls import url
from django.http import HttpResponse
def index(request):
return HttpResponse('Hello World')
url patterns = (
url(r'^$', index),
)
Django设置
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',
),
)
...
运行示例
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')
url patterns = (
url(r'^$', index),
)
if __name__ == __main__:
from djano.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
$ python hello.py runserver
http://127.0.0.1:8000 -> look 'Hello World'