我想通过向用户发送一封激活电子邮件
来让他们活跃起来。我猜它目前尚未合并到 Django 1.6
中。user-registration用 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
最佳答案
您需要先让他们注册并暂时将其标记为 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')
然后,一旦他们单击电子邮件中的链接,他们就会进入下一个 View (注意:您需要在电子邮件中添加一个链接,以便您知道它是哪个用户。这可能是 16 位数字的盐或其他内容。下面的 View 使用user.pk
:
def activate_view(request, pk):
user = User.objects.get(pk=pk)
user.is_active=True
user.save()
return HttpResponseRedirect('activation_success_view')
希望有帮助。祝你好运!
关于django 通过电子邮件进行用户注册和身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22393159/