內容整理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
?