目的我想登陸成功后顯示我的后臺(tái)管理(實(shí)現(xiàn)過程):
新建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>alex123@163.com</td>
</tr>
<tr>
<td>bs</td>
<td>女</td>
<td>alex456@163.com</td>
</tr>
<tr>
<td>cs</td>
<td>男</td>
<td>alex123@qq.com</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),
]
image.png
image.png
- 修改 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提交過來的數(shù)據(jù)(用戶不存在返回None)
user = request.POST.get('user',None)
pwd = request.POST.get('pwd',None)
if user == 'root' and pwd == "123":
# 去跳轉(zhuǎn)到(重定向)
return redirect('/home')
else:
# 用戶密碼不正確
error_msg = "用戶名或密碼錯(cuò)誤"
# 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>')
image.png
image.png
-
效果圖:
image.png
django使用for循環(huán)
- 修改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提交過來的數(shù)據(jù)(用戶不存在返回None)
user = request.POST.get('user',None)
pwd = request.POST.get('pwd',None)
if user == 'root' and pwd == "123":
# 去跳轉(zhuǎn)到(重定向)
return redirect('/home')
else:
# 用戶密碼不正確
error_msg = "用戶名或密碼錯(cuò)誤"
# 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>')
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),
]
image.png
- 修改 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 循環(huán),與Python不一樣,這是模板語言for循環(huán) 有開頭就有結(jié)尾 endfor-->
{% for row in user_list %}
<tr>
<td>js</td>
<td>女</td>
<td>alex123@163.com</td>
</tr>
{% endfor %}
</table>
</div>
</body>
</html>
image.png
-
效果圖:
image.png
另一種修改:
- 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 循環(huán),與Python不一樣,這是模板語言for循環(huán) 有開頭就有結(jié)尾 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>
image.png
-
效果圖:
image.png
獲取用戶輸入數(shù)據(jù)展示:
image.png
- 修改 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提交過來的數(shù)據(jù)(用戶不存在返回None)
user = request.POST.get('user',None)
pwd = request.POST.get('pwd',None)
if user == 'root' and pwd == "123":
# 去跳轉(zhuǎn)到(重定向)
return redirect('/home')
else:
# 用戶密碼不正確
error_msg = "用戶名或密碼錯(cuò)誤"
# 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":
# 獲取用戶提交的數(shù)據(jù) POST 請(qǐng)求中
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>')
image.png
- 修改 home.html
placeholder能在輸入顯示自己所寫的內(nèi)容(如效果圖)
<!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 循環(huán),與Python不一樣,這是模板語言for循環(huán) 有開頭就有結(jié)尾 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>
image.png
image.png
-
效果圖:
image.png
image.png