django學習筆記
# 創建Django工程
django-admin startproject 【工程名稱】
1 mysite 2 - mysite # 對整個程序進行配置 3 - init 4 - settings # 配置文件 5 - url # URL對應關系 6 - wsgi # 遵循WSIG規範,uwsgi + nginx 7 - manage.py # 管理Django程序: 8 - python manage.py 9 - python manage.py startapp xx10 - python manage.py makemigrations 11 - python manage.py migrate
# 運行Django功能
python manage.py runserver 127.0.0.1:8000
# 創建app
python manage.py startapp cmdb
python manage.py startapp openstack
python manage.py startapp xxoo....
app:
migrations 數據修改表結構
admin Django為我們提供的後臺管理
apps 配置當前app
models ORM,寫指定的類 通過命令可以創建數據庫結構
tests 單元測試
views 業務代碼
1、配置模板的路徑
TEMPLATES = [ { ‘BACKEND‘: ‘django.template.backends.django.DjangoTemplates‘, ‘DIRS‘: [os.path.join(BASE_DIR, ‘templates‘)], ‘APP_DIRS‘: True, ‘OPTIONS‘: { ‘context_processors‘: [‘django.template.context_processors.debug‘, ‘django.template.context_processors.request‘, ‘django.contrib.auth.context_processors.auth‘, ‘django.contrib.messages.context_processors.messages‘, ], }, }, ]
2.配置靜態目錄
1 static 2 3 STATICFILES_DIRS = ( 4 os.path.join(BASE_DIR, ‘static‘), 5 ) 6 7 8 <link rel="stylesheet" href="/static/commons.css" />
內容整理
1. 創建Django工程
django-admin startproject 工程名
2. 創建APP
cd 工程名
python manage.py startapp cmdb
3、靜態文件
project.settings.py
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
4、模板路徑
DIRS ==> [os.path.join(BASE_DIR,‘templates‘),]
5、settings中
middlerware
# 註釋 csrf
6、定義路由規則
url.py
"login" --> 函數名
7、定義視圖函數
app下views.py
def func(request):
# request.method GET / POST
# http://127.0.0.1:8009/home?nid=123&name=alex
# request.GET.get(‘‘,None) # 獲取請求發來的而數據
# request.POST.get(‘‘,None)
# return HttpResponse("字符串")
# return render(request, "HTML模板的路徑")
# return redirect(‘/只能填URL‘)
8、模板渲染
特殊的模板語言
-- {{ 變量名 }} def func(request): return render(request, "index.html", {‘current_user‘: "alex"}) index.html <html> .. <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‘]}) index.html <html> .. <body> <div>{{current_user}}</div> <ul> {% for row in user_list %} {% if row == "alex" %} <li>{{ row }}</li> {% endif %} {% 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‘}}) index.html <html> .. <body> <div>{{current_user}}</div> <a> {{ user_list.1 }} </a> <a> {{ user_dict.k1 }} </a> <a> {{ user_dict.k2 }} </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‘}}) index.html <html> .. <body> <div>{{current_user}}</div> <a> {{ user_list.1 }} </a> <a> {{ user_dict.k1 }} </a> <a> {{ user_dict.k2 }} </a> {% if age %} <a>有年齡</a> {% if age > 16 %} <a>老男人</a> {% else %} <a>小鮮肉</a> {% endif %} {% else %} <a>無年齡</a> {% endif %} </body> </html>
django學習筆記