我想通过向用户发送activation email
点击来激活用户。我想它目前尚未包含在Django 1.6
中。在Django中编码的user-registration应用似乎就是为了这个目的。但我对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
答案 0 :(得分: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')
然后,一旦他们点击电子邮件中的链接,就会将他们带到下一个视图(注意:您需要在电子邮件中放置一个链接,以便您知道它是哪个用户。这可能是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')
希望有所帮助。祝你好运!
答案 1 :(得分:2)
基本上你可以使用django的用户模型(https://docs.djangoproject.com/en/1.9/ref/contrib/auth/)。但是,在用户模型中,电子邮件不是必填字段。您需要修改模型以使电子邮件成为必填字段。
在您的视图中,您可能需要以下方法:
1)注册:注册后,设置user.is_active = False并调用函数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 = user。 set user.is_active = True。
实际上我实现了一个像您的请求这样的可重用应用程序,如果您有兴趣,请查看(https://github.com/JunyiJ/django-register-activate)。
希望有所帮助。祝你好运!
答案 2 :(得分:0)
检查出来...我希望它不仅可以解决你需要的解决方案而且还有解释。因为我认为django-registration app是默认的Django用户。因此,如果您想在注册表单中添加额外的字段,请开始考虑自己定制您的Django用户及其身份验证。你这里不需要django注册应用程序.. 以下是一些有用的教程
http://www.caktusgroup.com/blog/2013/08/07/migrating-custom-user-model-django/
还有更多......