目的我想登陸成功後顯示我的背景管理(實作過程):
建立home.html 在templates目錄下代碼如下:
<!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>
<table>
<tr>
<td>js</td>
<td>女</td>
<td>[email protected]</td>
</tr>
<tr>
<td>bs</td>
<td>女</td>
<td>[email protected]</td>
</tr>
<tr>
<td>cs</td>
<td>男</td>
<td>[email protected]</td>
</tr>
</table>
</div>
</body>
</html>
image.png
- 然後在urls.py 添加以下代碼:
from django.conf.urls import url
from django.contrib import admin
from cmdb import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
# url(r'h.html/', views.home),
url(r'^login', views.login),
url(r'^home', views.home),
]
- 修改 views.py 代碼如下:
from django.shortcuts import render
# Create your views here.
from django.shortcuts import HttpResponse
from django.shortcuts import render
# 重定向
from django.shortcuts import redirect
def login(request):
# 包含使用者送出的所有資訊
# 擷取使用者送出方法
# print(request.method)
# 判斷使用者名和密碼
error_msg = ""
if request.method == "POST":
# 擷取使用者通過POST送出過來的資料(使用者不存在傳回None)
user = request.POST.get('user',None)
pwd = request.POST.get('pwd',None)
if user == 'root' and pwd == "123":
# 去跳轉到(重定向)
return redirect('/home')
else:
# 使用者密碼不正确
error_msg = "使用者名或密碼錯誤"
# user = request.POST['user']
# pwd = request.POST['pwd']
# print(user,pwd)
return render(request, 'login.html', {'error_msg': error_msg})
def home(request):
return render(request, 'home.html')
# def login(request):
# # f = open('templates/login.html', 'r', encoding='utf-8')
# # date = f.read()
# # f.close()
# # return HttpResponse(date)
# return render(request,'login.html')
# def home(request):
# return HttpResponse('<h1>CMDB</h1>')
- 效果圖:
django使用for循環
- 修改views.py:
from django.shortcuts import render
# Create your views here.
from django.shortcuts import HttpResponse
from django.shortcuts import render
# 重定向
from django.shortcuts import redirect
def login(request):
# 包含使用者送出的所有資訊
# 擷取使用者送出方法
# print(request.method)
# 判斷使用者名和密碼
error_msg = ""
if request.method == "POST":
# 擷取使用者通過POST送出過來的資料(使用者不存在傳回None)
user = request.POST.get('user',None)
pwd = request.POST.get('pwd',None)
if user == 'root' and pwd == "123":
# 去跳轉到(重定向)
return redirect('/home')
else:
# 使用者密碼不正确
error_msg = "使用者名或密碼錯誤"
# user = request.POST['user']
# pwd = request.POST['pwd']
# print(user,pwd)
return render(request, 'login.html', {'error_msg': error_msg})
USER_LIST = [
{'username':'alex','email':'asdfasdf',"gender":'男'},
]
for index in range(20):
temp = {'username':'alex'+str(index),'email':'asdfasdf',"gender":'男'}
USER_LIST.append(temp)
def home(request):
return render(request, 'home.html',{'user_list': USER_LIST})
# def login(request):
# # f = open('templates/login.html', 'r', encoding='utf-8')
# # date = f.read()
# # f.close()
# # return HttpResponse(date)
# return render(request,'login.html')
# def home(request):
# return HttpResponse('<h1>CMDB</h1>')
- 修改urls.py :
from django.conf.urls import url
from django.contrib import admin
from cmdb import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
# url(r'h.html/', views.home),
url(r'^login', views.login),
url(r'^home', views.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>
<table>
<!--django支援 for 循環,與Python不一樣,這是模闆語言for循環 有開頭就有結尾 endfor-->
{% for row in user_list %}
<tr>
<td>js</td>
<td>女</td>
<td>[email protected]</td>
</tr>
{% endfor %}
</table>
</div>
</body>
</html>
另一種修改:
- views.py和urls.py 代碼同上不修改
- 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>
<table>
<!--django支援 for 循環,與Python不一樣,這是模闆語言for循環 有開頭就有結尾 endfor-->
{% for row in user_list %}
<tr>
<td>{{ row.username }}</td>
<td>{{ row.gender }}</td>
<td>{{ row.email }}</td>
</tr>
{% endfor %}
</table>
</div>
</body>
</html>
擷取使用者輸入資料展示:
- 修改 views.py:
from django.shortcuts import render
# Create your views here.
from django.shortcuts import HttpResponse
from django.shortcuts import render
# 重定向
from django.shortcuts import redirect
def login(request):
# 包含使用者送出的所有資訊
# 擷取使用者送出方法
# print(request.method)
# 判斷使用者名和密碼
error_msg = ""
if request.method == "POST":
# 擷取使用者通過POST送出過來的資料(使用者不存在傳回None)
user = request.POST.get('user',None)
pwd = request.POST.get('pwd',None)
if user == 'root' and pwd == "123":
# 去跳轉到(重定向)
return redirect('/home')
else:
# 使用者密碼不正确
error_msg = "使用者名或密碼錯誤"
# user = request.POST['user']
# pwd = request.POST['pwd']
# print(user,pwd)
return render(request, 'login.html', {'error_msg': error_msg})
USER_LIST = [
{'username':'alex','email':'asdfasdf',"gender":'男'},
{'username':'alex','email':'asdfasdf',"gender":'男'},
{'username':'alex','email':'asdfasdf',"gender":'男'},
]
# for index in range(20):
# temp = {'username':'alex'+str(index),'email':'asdfasdf',"gender":'男'}
# USER_LIST.append(temp)
def home(request):
if request.method == "POST":
# 擷取使用者送出的資料 POST 請求中
u = request.POST.get('username')
e = request.POST.get('email')
g = request.POST.get('gender')
temp = {'username': u, 'email': e, "gender": g}
USER_LIST.append(temp)
return render(request, 'home.html',{'user_list': USER_LIST})
# def login(request):
# # f = open('templates/login.html', 'r', encoding='utf-8')
# # date = f.read()
# # f.close()
# # return HttpResponse(date)
# return render(request,'login.html')
# def home(request):
# return HttpResponse('<h1>CMDB</h1>')
-
修改 home.html
placeholder能在輸入顯示自己所寫的内容(如效果圖)
<!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>
</tr>
{% endfor %}
</table>
</div>
</body>
</html>