python web框架Django学习(二)html
目录:前端
3、Django建立APPpython
4、建立登陆页面,实现用户交互,后台管理用户(非数据库方式)web
=================================================================================================
数据库
3、Django建立Appdjango
=================================================================================================浏览器
一、首先使用django-admin建立好一个django项目服务器
django-admin startproject test01app
二、在test01目录下面执行命令框架
D:\python2.7.13\exercise\test01>python manage.py startapp cmdb #app名称为cmdb
D:\python2.7.13\exercise\test01>python manage.py startapp openstack
三、查看目录(使用pycharm打开查看),确认是否建立成功!
四、实现浏览器中访问项目cmdb
1) 新建立的cmdb App中的目录有:
2) 修改cmdb中的views.py文件,具体配置以下:
from django.shortcuts import render from django.shortcuts import HttpResponse #加入 # Create your views here. def home(request): #定义函数home return HttpResponse('<h1>这是个人第一个Django--App程序!!!<h1>') HttpResponse()
3) 修改项目中的urls.py文件
from django.conf.urls import url
from django.contrib import admin
from django.shortcuts import HttpResponse #导入HttpServer模块
import time #加入
from cmdb import views #导入cmdb app中的views
def home(request): #定义函数home
return HttpResponse('<h1>这是个人第一个Django程序!!!<h1>')
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index.html',home), #添加index.html
url(r'^cmdb',views.home), #将cmdb app添加到url中
]
4) 开启django服务器:
5) 浏览器访问测试:
==================================================================================================
4、建立登陆页面,实现用户交互,后台管理用户(非数据库方式)
=================================================================================================
一、在项目test01目录下面建立一个templates目录,而且在templates目录中建立一个html文件index.html。
二、在html文件中写入内容以下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
label{
width: 120px;
text-align: right;
display: inline-block;
}
</style>
</head>
<body>
<form action="/login" method="post">
<p>
<label for="username">请输入用户名</label>
<input id="username" type="text" />
</p>
<p>
<label for="password">请输入密码</label>
<input id="password" type="text" />
<input type="submit" values="提交"/>
</p>
</form>
</body>
</html>
三、在APP文件cmdb目录下的views.py文件中写入如下内容:
from django.shortcuts import HttpResponse #加入
# Create your views here.
def login(request): #定义函数home
f = open('templates/index.html','r',encoding='utf8')
data = f.read()
f.close()
return HttpResponse(data)
【注意】:能够将views.py中的以上内容修改成:
from django.shortcuts import render
def login(request):
return render(request,'index.html')
而且修改项目文件test01中的setting.py中的TEMPLATES中的DIRS,修改成:
'DIRS': [os.path.join(BASE_DIR,'templates')],
表示默认模板位置在templates目录下面。
四、在项目文件夹test01下面的urls.py文件中加入
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index.html',home), #添加index.html
url(r'^login',views.login), #将cmdb app添加到url中
]
五、开启django服务器,浏览器访问效果为:
到此,浏览器可以正常访问前端页面,可是不能实现用户交互!!!
下面将实现用户输入正确的用户名和密码时跳转到百度首页;输入错误的用户名或者密码时,提示“用户名或密码错误”
六、在app文件cmdb中的views.py中加入:
from django.shortcuts import render
from django.shortcuts import redirect
def login(request):
if request.method == "POST":
user = request.POST.get('user',None) #获得用户输入的用户名
pwd = request.POST.get('pwd', None) #获得用户输入的密码
print(user,pwd) #在后台打印用户输入的用户名和密码
if user == 'root' and pwd == '123456': #判断
return redirect('http://www.baidu.com') #跳转到百度
return render(request, 'index.html')
当用户输入用户名为root,密码为123456时,跳转到百度首页!!!
七、继续设计当用户输入错误信息是,返回“用户名或密码错误”
1) 在index.html文件中加入一行:<span style="color: red;">` error_message `</span>
加入后,index.html文件为:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
label{
width: 120px;
text-align: right;
display: inline-block;
}
</style>
</head>
<body>
<form action="/login" method="post">
<p>
<label for="username">请输入用户名:</label>
<input id="username" type="text" name="user"/>
</p>
<p>
<label for="pwd">请输入密码:</label>
<input id="pwd" type="password" name="pwd"/>
<input type="submit" values="提交" />
<span style="color: red;">` error_message `</span>
</p>
</form>
</body>
</html>
2)在app文件views.py文件中修改成:
from django.shortcuts import render
from django.shortcuts import redirect
def login(request):
error_message=""
if request.method == "POST":
user = request.POST.get('user',None)
pwd = request.POST.get('pwd', None)
print(user,pwd)
if user == 'root' and pwd == '123456':
return redirect('http://www.baidu.com')
else:
error_message = "用户名或密码错误"
return render(request, 'index.html',{'error_message':error_message})
3) 当输入错误信息时,浏览器访问效果为:
到此,可以实现用户名为root密码为123456的用户登陆,而且跳转到百度首页。并且实现了当用户输入错误的用户名或密码时,提示“用户名或者密码错误”!
下面将实现后台管理用户。
八、首先在templates目录中建立一个名为home.html的HTML文件,文件内容以下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table>
<tr>
<td>张三</td>
<td>1992</td>
<td>女</td>
</tr>
<tr>
<td>李四</td>
<td>1993</td>
<td>男</td>
</tr>
<tr>
<td>王五</td>
<td>1997</td>
<td>男</td>
</tr>
</table>
</body>
<html>
九、在APP文件cmdb中的views.py中定义home函数,并把redirect中的地址改成/home
from django.shortcuts import render
from django.shortcuts import redirect
def login(request):
error_message=""
if request.method == "POST":
user = request.POST.get('user',None)
pwd = request.POST.get('pwd', None)
print(user,pwd)
if user == 'root' and pwd == '123456':
return redirect('/home')
else:
error_message = "用户名或密码错误"
return render(request, 'index.html',{'error_message':error_message})
def home(request):
return render(request,'home.html')
十、在项目文件test01的urls.py中加入映射关系from django.conf.urls import url
from django.contrib import admin
from django.shortcuts import HttpResponse #导入HttpServer模块
import time #加入
from cmdb import views #导入cmdb app中的views
def home(request): #定义函数home
return HttpResponse('<h1>这是个人第一个Django程序!!!<h1>')
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index.html',home), #添加index.html
url(r'^login',views.login), #将cmdb app添加到url中
url(r'^home',views.home)
]
十一、浏览器测试
1) 浏览器中输入用户登陆地址:127.0.0.1:8000/login
2) 输入正确的用户名和密码,正常跳转到127.0.0.1:8000/home页面
到此,当用户输入正确的用户名和密码时,可以实现跳转,而且可以可以看到以前在home.html文件中输入的三位用户的信息,可是这些用户的信息没法灵活改变,已经在html文件中写死了。
下面将进行将后台列表中的用户,用循环的方式,打印在前端页面上。
十二、首先须要在home.html文件中添加一个循环,添加后home.html文件以下:
<!DOCTYPE html>【注意】:
1) Django中在html文件中加入文件的方法:
{% for row in user_list %} #循环的开始,须要一对大括号,而且里面有两个%
<tr>
<td>` row`.`username `</td>
<td>` row`.`gender `</td>
<td>` row`.`email `</td>
</tr>
{% endfor %} #循环的结尾也得有一对大括号和两个%
2) row表示一个字典
3) 引入单变量值时须要两个大括号
{% for row in user_list %}
<tr>
<td>` row`.`username `</td>
<td>` row`.`gender `</td>
<td>` row`.`email `</td>
</tr>
{% endfor %}
这里的username、gender和email与APP文件中views.py中的username、gender、email相对应。
1三、修改APP文件中的views.py文件,加入USER_LIST列表和一个循环,修改后的views.py文件以下:
from django.shortcuts import render1四、在项目文件test01中的urls.py中加入对应关系,加入后的urls.py文件以下:
from django.conf.urls import url1五、浏览器输入127.0.0.1:8000/login,而后输入正确的用户名和密码后跳转到home.html页面的效果为: