目的我想登陆成功后显示我的后台管理(实现过程):
新建home.html 在templates目录下代码如下:
Title
js
女
[email protected]
bs
女
[email protected]
cs
男
[email protected]
- 然后在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('CMDB
')
-
效果图:
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('CMDB
')
- 修改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 :
Title
{% for row in user_list %}
js
女
[email protected]
{% endfor %}
-
效果图:
另一种修改:
- views.py和urls.py 代码同上不修改
- home.html 代码如下:
Title
{% for row in user_list %}
{{ row.username }}
{{ row.gender }}
{{ row.email }}
{% endfor %}
-
效果图:
获取用户输入数据展示:
- 修改 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('CMDB
')
- 修改 home.html
placeholder能在输入显示自己所写的内容(如效果图)
Title
{% for row in user_list %}
{{ row.username }}
{{ row.gender }}
{{ row.email }}
{% endfor %}
-
效果图: