目的我想登陆成功后显示我的后台管理(实现过程):
新建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>
- 然后在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>alex123@163.com</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>
-
效果图: