知識點整理:
内容整理
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>
XXOO管理:
MySQL
SQLAlchemy
主機管理(8列):
IP
端口
業務線
...
使用者表:
使用者名
密碼
功能:
1、 登入
2、主機管理頁面
- 檢視所有的主機資訊(4列)
- 增加主機資訊(8列) ** 模态對話框
3、檢視詳細
url:
"detail" -> detail
def detail(reqeust):
nid = request.GET.get("nid")
v = select * from tb where id = nid
...
4、删除
del_host -> delete_host
def delete_host(request):
nid = request.POST.get('nid')
delete from tb where id = nid
return redirect('/home')
模态對話框
修改 home.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body style="margin: 0">
<div style="height: 48px;background-color: #dddddd"></div>
<div>
<form action="/home" method="post">
<input type="text" name="username" placeholder="使用者名">
<input type="text" name="email" placeholder="郵箱">
<input type="text" name="gender" placeholder="性别">
<input type="submit" value="添加">
</form>
</div>
<div>
<table>
<!--django支援 for 循環,與Python不一樣,這是模闆語言for循環 有開頭就有結尾 endfor-->
{% for row in user_list %}
<tr>
<td>{{ row.username }}</td>
<td>{{ row.gender }}</td>
<td>{{ row.email }}</td>
<td>
<a href="/detail?nid={{ row.id }}">檢視詳細</a>
<a class="del" href="#" row-id="{{ row.id }}">删除</a>
</td>
</tr>
{% endfor %}
</table>
</div>
<!--綁定事件-->
$('.del').click(function(){
var row_id = $(this).attr('row-id');
$('#nid').val(row_id);
})
<div>
<form action="/del_host" method="post">
<input style="display: none" id="nid" type="text" name="nid">
<p>
<input type="submit">
<input type="botton">
</p>
</form>
</div>
</body>
</html>
image.png
- 效果圖: