django用户注册和电子邮件认证

eag*_*arn 4 django django-forms django-views user-registration

我想通过发送用户activation email点击来激活用户.我想它目前尚未合并Django 1.6. Django编码的用户注册应用似乎就是为了这个目的.但是我对forms.py中DefaultForm提供的内容有些怀疑.我想要包含更多字段.如何在那里实现它.如果我安装这个应用程序,更改直接包含更多字段是一个好主意,有没有更好的方法来实现相同.class RegistrationForm(forms.Form)

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
Run Code Online (Sandbox Code Playgroud)

Aar*_*ier 8

您需要先让他们注册并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')
Run Code Online (Sandbox Code Playgroud)

然后,一旦他们点击电子邮件中的链接,它就会将他们带到下一个视图(注意:您需要在电子邮件中放置一个链接,以便您知道它是哪个用户.这可能是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')
Run Code Online (Sandbox Code Playgroud)

希望有所帮助.祝好运!