python3 django框架開發(三) 連線MySQL,HTML進行登入,註冊操作
阿新 • • 發佈:2018-11-29
轉載請註明: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>© 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>