8

我創建了一個Django應用程序。我在應用程序中有一個註冊頁面(簡單的HTML表單),並且在註冊時有一個電子郵件字段。現在我想在用戶註冊時執行電子郵件驗證。就像給用戶發送一封電子郵件(以註冊表格的形式發送電子郵件)。通過谷歌搜索,我發現有一個Django的方法'send_email'可以用於上述。但作爲Django的新手,我無法理解爲此而創建的變化和功能。有人可以幫助我做到這一點,或指出一些幫助我的教程。或者除了Django的'send_email'方法之外,還有其他方法可以實現這一點。任何幫助,將不勝感激在Django中實現電子郵件驗證

我會貼我現有的代碼在這裏。

VIEWS.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> 
+1

rv_k請重新考慮您的答案,因爲Django註冊不再維護,並會導致很多人不好的建議。乾杯。 – miguelfg 2014-08-07 12:16:03

回答

8

退房django-registration作爲一個可插入應用程序。

如果您想自己推出,也可以使用該代碼作爲參考。

迴應評論:

Django的註冊是您的最佳選擇。它不包含html模板,你只需稍微調整你的表單。它有很好的documentation

django-registration是否附帶我可以使用的樣本模板? 否,原因有二:

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. 
+0

我檢查了它,但我已經用html表單創建了我的註冊表單。它是否能夠使用django註冊電子郵件verfication與我的應用程序單獨。 – 2011-03-31 04:36:42

+1

看看這個SO問題以及http://stackoverflow.com/questions/1325983/django-send-email-in-html-with-django-registration – DTing 2011-03-31 04:44:14

4

您應該檢查django-allauth,它確實你尋找什麼,和大量的更多的東西。 django-registration曾經被推薦,但不再維護,因爲它在it's bitbucket project中說過。 (因爲它已被回覆here

+0

這可能在理論上回答這個問題,但最好包括未來用戶的答案的基本部分,並提供鏈接供參考。 [link-dominated answers](// meta.stackexchange.com/questions/8231)可能通過[link rot](// en.wikipedia.org/wiki/Link_rot)失效。 – Mogsdad 2016-04-14 17:18:13