Python使用教程【五】使用Django创建web项目,实现登录界面

开发工具:Pycharm
数据库:mysql
1、打开Pycharm软件,通过File->New Project->Django创建Django项目,填写应用名称同时创建应用;
在这里插入图片描述2、运行项目后,在浏览器输入http://127.0.0.1:8000/访问;
在这里插入图片描述3、templates目录下新建index.html文件,代码如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>登录管理</title>
<meta name="description" content="登录界面">
<meta name="keywords" content="登录界面">
<link href="" rel="stylesheet">
<style>
	body,p,div,ul,li,h1,h2,h3,h4,h5,h6{
		margin:0;
		padding: 0;
	}
	body{
		background: #E9E9E9;
	}
	#login{
		width: 400px;
		height: 250px;
		background: #FFF;
		margin:200px auto;
		position: relative;
	}
	#login h1{
		text-align:center;
		position:absolute;
		left:120px;
		top:-40px;
		font-size:21px;
	}
	#login form p{
		text-align: center;
	}
	#user{
		background:rgba(0,0,0,.1) no-repeat;
		width: 200px;
		height: 30px;
		border:solid #ccc 1px;
		border-radius: 3px;
		padding-left: 32px;
		margin-top: 50px;
		margin-bottom: 30px;
	}
	#pwd{
		background: rgba(0,0,0,.1) no-repeat;
		width: 200px;
		height: 30px;
		border:solid #ccc 1px;
		border-radius: 3px;
		padding-left: 32px;
		margin-bottom: 30px;
	}
	#submit{
		width: 232px;
		height: 30px;
		background: rgba(0,0,0,.1);
		border:solid #ccc 1px;
		border-radius: 3px;
	}
	#submit:hover{
		cursor: pointer;
		background:#D8D8D8;
	}
</style>
</head>
<body>
<div id="login">
<h1>登录管理</h1>
	<form action="" method="post">
		<p><input type="text" name="user" id="user" placeholder="用户名"></p>
		<p><input type="password" name="passw0rd" id="pwd" placeholder="密码"></p>
		<p><input type="submit" id="submit" value="登录"></p>
	</form>
</div>
</body>
</html>

4、在views.py文件中定义index动作,添加代码如下:

def index(request):
    return render(request, "index.html")

5、在urls.py文件中添加index的url,添加代码如下:

    path('', views.index),
    path('index/', views.index)

6、运行项目,访问http://127.0.0.1:8000/index/就可以看到登录页面啦~
在这里插入图片描述

  • 2
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值