1 import sys 2 from django.conf import settings 3 #设置 4 settings.configure( 5 DEBUG = True, 6 SECRET_KEY = "thisisthesecretkey", 7 ROOT_URLCONF=__name__, 8 MIDDLEWARE_CLASSES=( 9 "django.middleware.common.CommonMiddleware", 10 "django.middleware.csrf.CsrfViewMiddleware", 11 "django.middleware.clickjacking.XFrameOptionsMiddleware", 12 ), 13 ) 14 15 from django.conf.urls import url 16 from django.http import HttpResponse 17 #视图View 18 def index(request): 19 return HttpResponse("Hello World") 20 #路由url 21 urlpatterns = ( 22 url(r"^$",index), 23 ) 24 #相当于简易版manage.py 25 if __name__=="__main__" : 26 from django.core.management import execute_from_command_line 27 28 execute_from_command_line(sys.argv)
1.先创建一个py文件,写入上述代码;
2.执行
python hello.py runserver
拓展版:
import os import sys from django.conf import settings DEBUG = OS.environ.get("DEBUG","on") == "on" SECRET_KEY = os.environ.get("SECRET_KEY",so.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.conf.urls import url from django.core.wsgi import get_wsgi_application from django.http import HttpResponse def index(request): return HttpResponse("Hello World") 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)
用到知识点
1.wsgi
2.Gunicorn
3.Twelve Factor App
引发问题:
1.一个项目该如何管理开发丶阶段运行和形成产品环境下的不同设置?
模板版:
Django的startproject命令创建的布局可以转换为一个可复用的模板(模板可以是一个目录或ZIP文件)
import os import sys from django.conf import settings DEBUG = os.environ.get("DEBUG","on") == "on" SECRET_KEY = os.environ.get("SECRET_KEY","{{ secret_key }}") 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.conf.urls import url from django.core.wsgi import get_wsgi_application from django.http import HttpResponse def index(request): return HttpResponse("Hello World") 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)
将文件改名为project_name.py文件放入project_name目录中
执行:django-admin.py startproject foo --template=project_name
神奇的事情发生了,foo的文件夹下会有一个foo.py文件,文件内容和project_name.py一样
什么是无状态?
HTTP本身是一个无状态的协议,意思是每一个到达服务器的请求都独立于之前的请求.如有需要某个特定的状态,则需要把它加到应用层上.例如Cookie.
占位图片服务器
1.用上述的模板创建一个项目名为placeholder
视图层:(分析)
创建两个视图来生成响应
1.按照请求的宽度和高度渲染占位图像
2.渲染主页面的内容,解释项目运作以及生成一些样例图片的方法
路由层:
url会捕获width和height参数并传给视图(数字类型)
表单:
表单用于验证POST和GET内容,也可以用于URL或者Cokkie的值