HTML模板
{% load staticfiles %}
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="lt-ie9 lt-ie8 lt-ie7" lang="en"> <![endif]-->
<!--[if IE 7]> <html class="lt-ie9 lt-ie8" lang="en"> <![endif]-->
<!--[if IE 8]> <html class="lt-ie9" lang="en"> <![endif]-->
<!--[if gt IE 8]><!--> <html lang="en"> <!--<![endif]-->
<head>
<title>S.P.M School</title>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="{% static 'css/style.css' %}">
<link href="//fonts.googleapis.com/css?family=Lobster&subset=latin,latin-ext" rel="stylesheet" type="text/css">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet"
href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<link rel="stylesheet" href="css/style.css">
<!--[if lt IE 9]><script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
<style>
table, th, td {
border: 0px solid black;
}
</style>
<script>
function validateForm() {
var mail = document.forms["myForm"]["email"].value;
var psd = document.forms["myForm"]["password"].value;
var atpos = mail.indexOf("@");
var dotpos = mail.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=mail.length) {
alert("Not a valid e-mail address");
return false;
}
if (psd == null || psd == "") {
alert("Not a valid Password");
return false;
}
}
</script>
</head>
<body>
<div id="page">
<div id="header">
<div id="section">
{% if user.is_authenticated %}
<a href="{% url 'post_new' %}" class="top-menu"><span class="glyphicon glyphicon-plus"></span></a>
{% endif %}
<div class="headline">
<font size="10": color="white"><center>S.P.M.L.P School</center></font>
</div>
<span>303nishanth@gmail.com <br />
<br />
+91 9961685892</span> </div>
<ul>
<li class="#"><a href="/">Home</a></li>
<li><a href="{% url 'about' %}">About Us</a></li>
<li><a href="{% url 'event' %}">Event</a></li>
<li><a href="{% url 'calendar' %}">School Calendar</a></li>
<li><a href="{% url 'contact' %}">Contact us</a></li>
</ul>
<br><br>
<section class="container">
<div class="login">
<div id="section"><br><br>
<h1><center>Login As<font color="red"> Admin</font></center></h1><br>
</div>
<form name="myForm" action="{% url 'class_info' %}" onsubmit="return validateForm();" method="post">
<input type='hidden' name='csrfmiddlewaretoken' value='wu5eSIDElr8EsVDgXmHmFNCCmSLdhyK5' />
{%csrf_token%}
<div class="text">
<p><center><input type="text" name="email" placeholder="Username or Email"></p></center>
<p><center><input type="password" name="password" placeholder="Password"></p></center>
<p class="remember_me">
</p>
<center><p class="submit"><input type="submit" name="commit" value="Login"></p></center>
</form>
</div>
</section>
</body>
</html>
views.py
def login_submit(request):
context = {}
email = request.POST.get('email','')
psd = request.POST.get('password', '')
user = authenticate(username=email, password=psd)
if user is not None:
# the password verified for the user
if user.is_active:
context['message'] = "User is valid, active and authenticated"
return render (request,'class_info.html',context)
else:
context['message']= "The password is valid, but the account has been disabled!"
else:
# the authentication system was unable to verify the username and password
context['message']= "The username and password were incorrect."
return render (request,'login.html',context)
当我使用此模板和查看时,我可以使用任何电子邮件地址和密码登录。但我想使用我的电子邮件地址和密码登录(我的意思是只使用一个电子邮件地址登录)。我有可能吗?
答案 0 :(得分:1)
您可以使用自定义身份验证编写严格的条件。像:
user = your_custom_authenticate(username=username, password=psd)
当方法your_custom_authenticate
有一些方法可以做你想做的事。 view
全部由你自己控制。如果context
是您的电子邮件,则只需将render
然后email
设置为另一条路径即可。当然,如果您的下一页需要session
件事,那么您应该在session
中保存一些用户信息,这完全取决于您。
答案 1 :(得分:0)
您可以使用检查当前username
是否为用户名的函数来包装默认def custom_auth(username=None, password=None):
user = None
if username == 'my_personal@email.com': # only this email is allowed
user = authenticate(username=email, password=psd)
return user
def login_submit(request):
context = {}
email = request.POST.get('email','')
psd = request.POST.get('password', '')
user = custom_auth(username=email, password=psd)
if user is not None:
...
else:
...
return render (request,'login.html',context)
,如下所示:
which node