django 开发实战--第二章开始我们第一个项目
原创
©著作权归作者所有:来自51CTO博客作者wx63186321c235c的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一章:
一、python+django+selenium 搭建简易自动化测试平台_傲娇的喵酱的博客_django自动化测试平台
1.创建项目(python3)
Microsoft Windows [版本 10.0.14393]
(c) 2016 Microsoft Corporation。保留所有权利。
C:\Users\Administrator>D:
D:\>cd python3
D:\python3>cd Scripts
D:\python3\Scripts>django-admin.exe startproject BBT
2.创建应用
D:\python3\Scripts>django-admin.exe startproject BBT
D:\python3\Scripts>cd BBT
D:\python3\Scripts\BBT>python3 manage.py startapp sign
创建应用完成后,将该应用加入项目,修改settings.py文件。
3.创建首页
3.1创建首页模板
在应用sign下创建模板文件夹(templates)
在templates文件夹下创建首页页面home.html
接下来编写home.html内容
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>自动化系统首页</title>
</head>
<body bgcolor="#f5f5dc">
<h1 style="text-align: center;">BBT自动化系统首页</h1>
<div style='text-align: center'>
</div>
<div style="text-align: center;margin-top: 30px">
<input type="submit" name="Submit" value="webUI自动化" onclick=window.open("/cc/index/") style="font-size:20px;width: 200px; height: 60px;cursor:pointer">
</div>
</body>
</html>
3.2配置URL
修改BBT/BBT/urls.py文件
from django.conf.urls import url
from django.contrib import admin
from sign import views #导入sign应用views文件
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^home/$',views.home), #添加home/配置路径
]
3.3去views页面../sign/views.py 文件创建 home函数
3.4启动程序,访问http://127.0.0.1:8000/home/
4.修改.../urls.py 文件,添加以下路径
这样访问http://127.0.0.1:8000/会直接跳到我们首页。
5.现在添加404页面
5.1在templates文件夹下创建404.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8" http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>404-对不起!您访问的页面不存在</title>
<style type="text/css">
.head404{ width:580px; height:234px; margin:50px auto 0 auto; background:url(https://www.daixiaorui.com/Public/images/head404.png) no-repeat; }
.txtbg404{ width:499px; height:169px; margin:10px auto 0 auto; background:url(https://www.daixiaorui.com/Public/images/txtbg404.png) no-repeat;}
.txtbg404 .txtbox{ width:390px; position:relative; top:30px; left:60px;color:#eee; font-size:13px;}
.txtbg404 .txtbox p {margin:5px 0; line-height:18px;}
.txtbg404 .txtbox .paddingbox { padding-top:15px;}
.txtbg404 .txtbox p a { color:#eee; text-decoration:none;}
.txtbg404 .txtbox p a:hover { color:#FC9D1D; text-decoration:underline;}
</style>
</head>
<body bgcolor="#494949">
<div class="head404"></div>
<div class="txtbg404">
<div class="txtbox">
<p>对不起,您请求的页面不存在、或已被删除、或暂时不可用</p>
<p class="paddingbox">请点击以下链接继续浏览网页</p>
<p>》<a style="cursor:pointer" onclick="history.back()">返回上一页面</a></p>
<p>》<a href="http://127.0.01:8000/">返回网站首页</a></p>
</div>
</div>
</body>
</html>
</html>
5.2.修改views.py
from django.shortcuts import render
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.shortcuts import render_to_response
# Create your views here.
def home(request):
return render(request,'home.html')
#404页面
@csrf_exempt
def page_not_found(request):
return render_to_response('404.html')
5.3修改urls.py,代码如下
添加:
handler404=views.page_not_found
如下:
from django.conf.urls import url
from django.contrib import admin
from sign import views #导入sign应用views文件
urlpatterns = [
url(r'^$', views.home),
url(r'^admin/', admin.site.urls),
url(r'^home/$',views.home), #添加home/配置路径
]
handler404=views.page_not_found
5.4修改setting.py
(1.)DEBUG修改为False,
(2.)ALLOWED_HOSTS添加指定域名或者IP,
OK 这就配置完事了,现在我们启动应用,访问http://127.0.0.1:8000/a