2014-03-13 100 views
2

我想讓用戶通過發送activation email來點擊活動。我猜它目前沒有包含在Django 1.6中。在Django中編碼的user-registration應用似乎可以達到這個目的。但我對forms.py提供的DefaultForm有一些疑問。我想要在其中包含更多的字段。我在那裏如何實現class RegistrationForm(forms.Form)。如果我安裝這個應用程序,是不是直接在那裏更改包含更多字段是一個好主意,有沒有更好的方法來實現相同。django通過電子郵件發送用戶註冊和身份驗證

views.py中,我看到一些方法如下面沒有實現。我沒有清楚的瞭解這些方法需要做什麼。我應該將網址重定向到頁面嗎?

def register(self, request, **cleaned_data): 
raise NotImplementedError 

def activate(self, request, *args, **kwargs): 

     raise NotImplementedError 

    def get_success_url(self, request, user): 
     raise NotImplementedError 

回答

7

您需要先讓他們註冊並暫時將其標記爲is_active=False。事情是這樣的:

from django.contrib.auth.models import User 
from django.core.mail import send_mail 
from django.http import HttpResponseRedirect 

def signup(request): 
    # form to sign up is valid 
    user = User.objects.create_user('username', 'email', 'password') 
    user.is_active=False 
    user.save() 

    # now send them an email with a link in order to activate their user account 
    # you can also use an html django email template to send the email instead 
    # if you want 
    send_mail('subject', 'msg [include activation link to View here to activate account]', 'from_email', ['to_email'], fail_silently=False) 

return HttpResponseRedirect('register_success_view') 

然後,一旦他們點擊它把它們帶到下一個視圖(注意電子郵件中的鏈接:你需要把一個鏈接的電子郵件,讓你知道它是用戶這可能。是16位的鹽或某事的下面視圖使用user.pk:!

def activate_view(request, pk): 
    user = User.objects.get(pk=pk) 
    user.is_active=True 
    user.save() 
    return HttpResponseRedirect('activation_success_view') 

希望幫助好運

+0

激活鏈接會是什麼樣子?如何區分不同的註冊鏈接?謝謝 – Stallman

0

檢查了這一點...我希望它可以幫助出的不僅是解u需要,但也解釋..因爲我認爲django註冊應用程序是默認的Django用戶。所以如果你想在你的註冊表單中有額外的字段,請開始考慮自己定製你的Django用戶及其身份驗證。你不需要在這裏Django的註冊應用.. 這裏有一些教程,多數民衆贊成將有助於

http://blackglasses.me/2013/09/17/custom-django-user-model/

http://www.caktusgroup.com/blog/2013/08/07/migrating-custom-user-model-django/

還有更多...

2

基本上你可以使用Django的用戶模型(https://docs.djangoproject.com/en/1.9/ref/contrib/auth/)。但是,在用戶模型中,電子郵件不是必填字段。您需要修改模型以使電子郵件成爲必填字段。

在您的看法,您可能需要使用下面的方法:

1)報名:報名後,設置user.is_active =假,並調用函數SEND_EMAIL包括電子郵件中的激活鏈接。在鏈接中,您可能希望包含用戶的信息(例如,user.id),因此當用戶單擊鏈接時,您知道要激活哪個用戶。

2)send_email:發送一個鏈接到用戶的電子郵件地址進行驗證。該鏈接包括用戶的ID。例如: http://127.0.0.1:8000/activation/?id=4

3)激活:使用id = request.GET.get('id')從URL獲取id信息。查詢用戶=其ID爲ID的用戶。設置user.is_active = True。

其實我實現了一個可重複使用的應用程序,就像你的請求一樣,如果你有興趣,請檢查這個(https://github.com/JunyiJ/django-register-activate)。

希望有所幫助。祝你好運!

相關問題