内容整理1.创建django工程django-admin startproject 工程名2.创建APPcd 工程名python manage.py startapp cmdb3.静态文件project.settings.pySTATICFILES_dirs = {os.path.join(BASE_DIR, 'static'),}4.模板路径DIRS ==> [os.path.join(BASE_DIR, 'templates'),]5.settings中middlerware注释csrf6.定义路由规则url.py'login' --> 对应一个函数名7.定义视图函数,app下views.pydef login(request):#request.method:获取用户传的方法 GET 或 POST#http://127.0.0.1:8001/home?nid=123&name=alex#request.GET.get('',None) #获取请求发来的数据#request.POST.get('',None)#return HttpResponse('字符串')return render(request, 'HTML模板的路径')return redirect('URL路径') #只能传url路径 如果return redirect('/home') /代表本地地址8.模板渲染特殊的模板语言def func(request):return render(request,'index.html',{'current_user':"alex"})indexhtml<body><div> {{current_user}}</div><body>html===>最后生成的字符串html<body><div>alex</div><body>html----for循环def func(request):return render(request,'index.html',{'current_user':"alex",'user_list':['alex','eric]}) html<body><div> {{current_user}}</div><ul>{%for row in user_list %}<li>{{ row }}<li>{% endfor %}<ul><body>html-----取索引def func(request):return render(request,'index.html',{'current_user':"alex",'user_list':['alex','eric],'user_dict':{'k1':'v1',"k2":'v2'}})html<body><a>{{ user_list.1 }}<a><a>{{ user_dict.k2 }}<a><a>{{ user_dict.k1 }}<a><body>html------条件def func(request):return render(request,'index.html',{'current_user':"alex",'age':18,'user_list':['alex','eric],'user_dict':{'k1':'v1',"k2":'v2'}})html<body><a>{{ user_list.1 }}<a><a>{{ user_dict.k2 }}<a><a>{{ user_dict.k1 }}<a>{%if age%}<a>有年龄<a>{% if age > 18 %}<a>老孩子<a>{% else %}<a>小孩子<a>{% endif %}{% else %}<a>无年龄<a>{% endif %}<body>html