Python Django中电子邮件验证的实现

Python Django中电子邮件验证的实现,python,django,django-models,django-templates,django-settings,Python,Django,Django Models,Django Templates,Django Settings,我已经创建了一个Django应用程序。我在应用程序中有一个注册页面(简单的HTML表单),注册时它有一个电子邮件字段。现在我想在用户注册时实现电子邮件验证。例如向用户发送电子邮件(注册表格中给出的电子邮件)。通过谷歌搜索,我发现有一种Django方法“send_email”,可以用于上述用途。但作为一名Django新手,我无法理解为此而创建的更改和功能。有人能帮我做这件事吗,或者给我介绍一些能帮我解决问题的教程。或者除了Django的“send_email”方法之外,还有其他方法来实现这一点吗。

我已经创建了一个Django应用程序。我在应用程序中有一个注册页面(简单的HTML表单),注册时它有一个电子邮件字段。现在我想在用户注册时实现电子邮件验证。例如向用户发送电子邮件(注册表格中给出的电子邮件)。通过谷歌搜索,我发现有一种Django方法“send_email”,可以用于上述用途。但作为一名Django新手,我无法理解为此而创建的更改和功能。有人能帮我做这件事吗,或者给我介绍一些能帮我解决问题的教程。或者除了Django的“send_email”方法之外,还有其他方法来实现这一点吗。任何帮助都将不胜感激

我将把我现在的代码粘贴到这里

视图.PY

def registrationForm(request):
    if request.method == "POST":  
        firstName = request.POST.get("firstName")
        lastName = request.POST.get("lastName")
        email = request.POST.get("email")
        password = request.POST.get("password")
        sex = request.POST.get("sex")
        birthday = request.POST.get("birthday")
        print request.POST.get("sex")
        UniversityDetails(firstName=firstName,lastName=lastName,email=email,password=password,sex=sex,birthday=birthday).save()
        return render_to_response('registrationForm.html')
    return render_to_response("registrationForm.html")

def login(request):
    if request.POST:            
        email=request.POST.get("username")
        password = request.POST.get("password")
        print email
        print password
        user = UniversityDetails.objects.filter(email=email,password=password)
        print user
        if(not user):
            return render_to_response("registrationForm.html",{'invalid': True })
        else:
            return render_to_response("login.html")
    return render_to_response("registrationForm.html")
registrationForm.html

<html>
<head>
  <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>

  <script>
  $(document).ready(function() {
    $("#datepicker").datepicker();
  });
  </script>
</head>

<title>
Login/Registration Page
</title>

    <script type="text/javascript">

   //Created / Generates the captcha function    
    function DrawCaptcha()
    {
        var a = Math.ceil(Math.random() * 10)+ '';
        var b = Math.ceil(Math.random() * 10)+ '';       
        var c = Math.ceil(Math.random() * 10)+ '';  
        var d = Math.ceil(Math.random() * 10)+ '';  
        var e = Math.ceil(Math.random() * 10)+ '';  
        var f = Math.ceil(Math.random() * 10)+ '';  
        var g = Math.ceil(Math.random() * 10)+ '';  
        var code = a + ' ' + b + ' ' + ' ' + c + ' ' + d + ' ' + e + ' '+ f + ' ' + g;
        document.getElementById("txtCaptcha").value = code
    }

    // Validate the Entered input aganist the generated security code function   
    function ValidCaptcha(){
        var str1 = removeSpaces(document.getElementById('txtCaptcha').value);
        var str2 = removeSpaces(document.getElementById('txtInput').value);
        if (str1 == str2) return true;        
        return false;

    }

    // Remove the spaces from the entered and generated code
    function removeSpaces(string)
    {
        return string.split(' ').join('');
    }

    </script>
<body onload="DrawCaptcha();">

<div align="center">
<form name="userInputForm" method="POST" id="myFormid" action="http://10.1.0.90:8080/login/">
<div style="float:left;width:100%;">
  <p style="float:left;margin-right:10px;width:auto;"><label style="float:left;">Email id</label><br/> <input type="text" name="username" size="25" /></p>
  <p style="float:left;margin-right:10px;width:auto;"><label style="float:left;">Password</label><br/><input type="password" name="password" size="25" />

{% if invalid %}
    Mismatch in your email/password. 
{% endif %}

</p>

 </div> 
    <p style="clear:both;float:left;"><input type="submit" value="Log in" /></p> 
</div>
</form><
<script type="text/javascript">
function userCheck(){
    /*document.getElementById('myFormid').action = "http://10.1.0.90:8080/login/";
    if ((document.userInputForm.username.value.length==0)) */
}
</script>
<form name ="myform"  method="POST" id='FormID'>
<table>
<tr>
<td>First name</td>
<td>
<input type="text" name="firstName" value=""  maxlength="100" />
<b id="firstNameID" style="font-family:Times New Roman;color:#B4045F;font-size:14px;">
</td>
</tr>

<tr>
<td>Last name</td>
<td>
<input type="text" name="lastName" value="" maxlength="100" />
</td>
</tr>

<tr>
<td>E-mail</td>
<td>
<input type="text" name="email" value="" maxlength="100" />
</td>
</tr>
<tr>
<td>Password</td>
<td>
<input type="password" name="password" value="" maxlength="100"  />
<b id="passwordID" style="font-family:Times New Roman;color:#B4045F;font-size:14px;">
</td>
</tr>
<tr>
<td>Gender:</td>
<td>
<input type="radio" name="sex" value="male" /> Male
<input type="radio" name="sex" value="female" /> Female
</td>
</tr>
<tr>
<td>Birthday</td>
<td>
<input type="text" name="birthday" id='datepicker' value="" maxlength="100" />
</td>
</tr>
</tr>
</table>
<table>
<tr>
    <td>

    </td>
</tr>
<tr>
    <td>
        <input type="text" id="txtCaptcha" 
            style="background-image:url(1.jpg); text-align:center; border:none;
            font-weight:bold; font-family:Modern" />
        <input type="button" id="btnrefresh" value="Refresh" onclick="DrawCaptcha();" />
    </td>
</tr>
<tr>
    <td>
        <input type="text" id="txtInput"/>    
    </td>

<td> <br> <br>
<input type="button" value="Sign Up" onClick="isEmpty();"/>
</td>
</tr>
</table>
<script type="text/javascript">
function isEmpty(){
    if  ((document.myform.firstName.value.length==0)) 
        {
        document.getElementById('firstNameID').innerHTML = 'Please fill this field';
        return true;
        }
    else if ((document.myform.password.value.length==0)) 
        {
        document.getElementById('passwordID').innerHTML = 'Please fill this field';
        return true;
        }
    else if (! ValidCaptcha())
        {
            alert("Captcha entered wrong");
        }

    else 
        { 
        document.getElementById('FormID').action = "http://10.1.0.90:8080/registrationForm/";
        document.getElementById('FormID').submit();
        return false; 
        }
}
</script>
</body>


</html>

$(文档).ready(函数(){
$(“#日期选择器”).datepicker();
});
登录/注册页面
//创建/生成验证码函数
函数DrawCaptcha()
{
var a=Math.ceil(Math.random()*10)+'';
var b=Math.ceil(Math.random()*10)+'';
var c=Math.ceil(Math.random()*10)+'';
var d=Math.ceil(Math.random()*10)+'';
var e=Math.ceil(Math.random()*10)+'';
var f=Math.ceil(Math.random()*10)+'';
var g=Math.ceil(Math.random()*10)+'';
风险值代码=a+“”+b+“”+c+“”+d+“”+e+“”+f+“”+g;
document.getElementById(“txtCaptcha”).value=code
}
//根据生成的安全代码函数验证输入的输入
函数ValidCaptcha(){
var str1=removespace(document.getElementById('txtCaptcha').value);
var str2=removespace(document.getElementById('txtInput').value);
if(str1==str2)返回true;
返回false;
}
//从输入和生成的代码中删除空格
函数RemoveSpace(字符串)
{
返回字符串.split(“”).join(“”);
}
电子邮件id

密码
{%如果无效%} 您的电子邮件/密码不匹配。 {%endif%}

< 函数userCheck(){ /*document.getElementById('myFormid')。操作=”http://10.1.0.90:8080/login/"; if((document.userInputForm.username.value.length==0))*/ } 名字 姓 电子邮件 密码 性别: 男性 女性 生日

函数isEmpty(){ if((document.myform.firstName.value.length==0)) { document.getElementById('firstNameID')。innerHTML='请填写此字段'; 返回true; } else if((document.myform.password.value.length==0)) { document.getElementById('passwordID')。innerHTML='请填写此字段'; 返回true; } 如果(!ValidCaptcha()),则为else { 警报(“验证码输入错误”); } 其他的 { document.getElementById('FormID')。操作=”http://10.1.0.90:8080/registrationForm/"; document.getElementById('FormID').submit(); 返回false; } }
作为可插拔应用程序签出

此外,如果您想自己滚动,也可以将代码用作参考

评论回复:

django注册将非常适合您。它不附带html模板,您只需稍微调整表单即可。它有很好的效果

django注册是否附带了我可以立即使用的示例模板? 不,有两个原因:

1. Providing default templates with an application is generally
   hard to impossible, because different sites can have such
   wildly different design and template structure. Any attempt to
   provide templates which would work with all the possibilities
   would probably end up working with none of them.

2. A number of things in django-registration depend on the
   specific :ref:`registration backend <backend-api>` you use,
   including the variables which end up in template
   contexts. Since django-registration has no way of knowing in
   advance what backend you're going to be using, it also has no
   way of knowing what your templates will need to look like.

Fortunately, however, django-registration has good documentation
which explains what context variables will be available to
templates, and so it should be easy for anyone who knows Django's
template system to create templates which integrate with their own
site.
1。为应用程序提供默认模板通常是
很难不可能,因为不同的网站可以有这样的
完全不同的设计和模板结构。任何企图
提供能够处理所有可能性的模板
最终可能不会和他们一起工作。
2.django注册中的许多事情取决于
specific:ref:'您使用的注册后端',
包括在模板中结束的变量
上下文。因为django注册没有办法知道
提前你将要使用的后端,它也没有
了解模板需要的外观的方法。
幸运的是,django注册有很好的文档
这解释了哪些上下文变量可用于
模板,所以对任何了解Django的人来说都应该很容易
模板系统用于创建与自己的模板集成的模板
地点。
你应该检查一下,它能满足你的需求,还有很多其他的东西。 以前是推荐的,但现在不再像中所说的那样进行维护。
(如回复所示)

我检查了它,但我已经用html表单创建了我的注册表单。我的应用程序是否仍然可以单独使用django注册进行电子邮件验证。请检查此SO问题。请重新考虑您的答案,因为django注册已不再保留,并将导致许多人被错误推荐。干杯。这理论上可能回答了这个问题,但最好在这里为将来的用户提供答案的基本部分,并提供链接供参考。可以通过以下方式变得无效。