python3 django框架开发(三) 连接MySQL,HTML进行登录,注册操作 - Go语言中文社区

python3 django框架开发(三) 连接MySQL,HTML进行登录,注册操作


转载请注明:https://blog.csdn.net/weixin_40490238/article/details/84573309

在上一篇中已经连接好了mysql,建立好用户表

现在完成 HTML 的用户登录注册

sign-in.html的登录表单:

提交路径为:signIn,一次在urls.py中设置路径

from django.urls import path
from robotWeb import views
urlpatterns = [
    path('', views.index),
    path('signIn/',views.sign_in)
]

路径signIn指向views.sign_in

views.py文件为:

from django.shortcuts import render
from robotWeb import models
# Create your views here.


def forgot(request):
    return render(request, 'forgot.html')


def index(request):
    return render(request,'sign-in.html')


def sign_in(request):
    email = request.POST['email']
    password = request.POST['password']
    user = models.userTable.objects.filter(email__exact=email,password__exact=password)
    if user:
        print("successful")
    else:
        print("error")
    return render(request, 'forgot.html')

因为这里是登录测试,因此在userTable表中自行添加了一条测试数据

登录成功后跳转:

 

注册功能和登录功能类似:views.py中

def sign_up(request):
    Name=request.POST['name']
    Email = request.POST['email']
    Password = request.POST['password']
    models.usertable.objects.create(name=Name,email=Email,password=Password)
    return render(request,'SignIn.html')

 

sign-in.html代码:


<!DOCTYPE html>
{% load static %}
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
	<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<title>Minimal and Clean Sign up / Login and Forgot Form by FreeHTML5.co</title>
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<meta name="description" content="Free HTML5 Template by FreeHTML5.co" />
	<meta name="keywords" content="free html5, free template, free bootstrap, html5, css3, mobile first, responsive" />


    <link href='https://fonts.googleapis.com/css?family=Open+Sans:400,700,300' rel='stylesheet' type='text/css'>
	<link rel="stylesheet" href={% static 'css/bootstrap.min.css' %}>
	<link rel="stylesheet" href={% static 'css/animate.css' %}>
	<link rel="stylesheet" href={% static 'css/style.css' %}>

	<!-- Modernizr JS -->
	<script src={% static 'js/modernizr-2.6.2.min.js' %}></script>
	<!-- FOR IE9 below -->
	<!--[if lt IE 9]>
	<script src={% static 'js/respond.min.js' %}></script>
	<![endif]-->

	</head>
	<body class="style-2">

		<div class="container">
			<div class="row">
				<div class="col-md-4">
					<!-- Start Sign In Form 提交到views的signIn -->
					<form action="/signIn/" method="post" class="fh5co-form animate-box" data-animate-effect="fadeInLeft">
                        {% csrf_token %}
						<h2>Sign In</h2>
						<div class="form-group">
							<label for="email" class="sr-only">Email</label>
							<input type="text" class="form-control" name="email" placeholder="email" autocomplete="off">
						</div>
						<div class="form-group">
							<label for="password" class="sr-only">Password</label>
							<input type="password" class="form-control" name="password" placeholder="Password" autocomplete="off">
						</div>
						<div class="form-group">
							<label for="remember"><input type="checkbox" id="remember"> Remember Me</label>
						</div>
						<div class="form-group">
							<p>Not registered? <a href="sign-up.html">Sign Up</a> | <a href="forgot.html">Forgot Password?</a></p>
						</div>
						<div class="form-group">
							<input type="submit" value="Sign In" class="btn btn-primary">
						</div>
					</form>
					<!-- END Sign In Form -->

				</div>
			</div>
			<div class="row" style="padding-top: 60px; clear: both;">
				<p><small>&copy; All Rights Reserved  <a href="http://47.107.56.104:8080" target="_blank" title="周定坤">周定坤</a></small></p></div>
			</div>
		</div>
	
	<!-- jQuery -->
	<script src={% static 'js/jquery.min.js' %}></script>
	<!-- Bootstrap -->
	<script src={% static 'js/bootstrap.min.js' %}></script>
	<!-- Placeholder -->
	<script src={% static 'js/jquery.placeholder.min.js' %}></script>
	<!-- Waypoints -->
	<script src={% static 'js/jquery.waypoints.min.js' %}></script>
	<!-- Main JS -->
	<script src={% static 'js/main.js' %}></script>

	</body>
</html>

 

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/weixin_40490238/article/details/84623187
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-02-25 00:16:41
  • 阅读 ( 1388 )
  • 分类:前端

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢