安装python和django不提
首先来一个hello world
import sys
#settings
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',
),
)
# view
from django.http import HttpResponse
def index(request):
return HttpResponse('Hello World')
# urls
from django.conf.urls import url
urlpatterns = (
url(r'^$',index),
)
if __name__ == "__main__":
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
以上代码保存为hello.py
在cmd上敲命令
python hello.py runserver
注意路径问题
System check identified no issues (0 silenced).
August 28, 2018 - 19:13:06
Django version 2.1, using settings None
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
默认本地地址127.0.0.1:8000
于是浏览器上可以显示第一个hello world了
本段代码是把一般网站的文件融合在了一起,比如视图、url、配置等,核心是''' return HttpResponse('Hello World')'''遇到请求返回helloworld
一些小修改:
添加wsgi应用
跨系统配置
import sys
import os
from django.conf import settings
from django.conf.urls import url
from django.core.wsgi import get_wsgi_application
from django.http import HttpResponse
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
settings.configure(
DEBUG = DEBUG,
SECRET_KEY = 'thisisthesecretkey',
ROOT_URLCONF = __name__,
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.middleware.clickjacking.XFrameoptionsMiddleware',
),
)
# view
def index(request):
return HttpResponse('Hello World')
# urls
application = get_wsgi_application()
urlpatterns = (
url(r'^$',index),
)
if __name__ == "__main__":
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
可以在系统直接配置DEBUG
如set DEBUG=off(windows)
export DEBUG=off(linux)