我如何发送电子邮件并创建确认链接以单击它?

发布于 2024-12-27 11:09:39 字数 2926 浏览 1 评论 0原文

models.py:views.py

class Users(models.Model):
    username = models.CharField(max_length=255)
    slug = models.CharField(max_length=255, default='0')
    password = models.CharField(max_length=300)
    passwordrepeat = models.CharField('Repeat Password', max_length=300)
    password_token = models.CharField(max_length=300, default='0')
    email = models.CharField(max_length=255)
    email_verified = models.BooleanField(default=False)
    email_token = models.CharField(max_length=255)
    email_token_expiry = models.DateTimeField()
    tos = models.BooleanField(default=False)
    active = models.BooleanField(default=False)
    last_login = models.DateTimeField(auto_now_add=True)
    last_action = models.DateTimeField(auto_now_add=True)
    is_admin = models.BooleanField(default=False)
    role = models.CharField(max_length=255, default='0')
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now_add=True)

    def __unicode__(self):
        return self.username

class UsersModelForm(forms.ModelForm):
    class Meta:
        model = Users
        fields = ('username', 'password', 'passwordrepeat', 'email')
        widgets = {
            'password' : PasswordInput(),
            'passwordrepeat' : PasswordInput(),
        }

def register(request):
    flag = True
    possible = '0123456789abcdefghijklmnopqrstuvwxyz'
    token = ''

    current_datetime = datetime.datetime.now()

    user = UsersModelForm()
    if request.method == 'POST':
        userf = UsersModelForm(request.POST)
        username = userf.data['username']
        password = userf.data['password']
        passwordrepeat = userf.data['passwordrepeat']
        email = userf.data['email']

        password = bcrypt.hashpw(password,bcrypt.gensalt())
        passwordrepeat = bcrypt.hashpw(passwordrepeat,bcrypt.gensalt())

        userf.data['password'] = password
        userf.data['passwordrepeat'] = passwordrepeat

        if password != passwordrepeat:
            flag = False
            passVariable = {'user':user, 'flag': False}
            return render_to_response('register.html', passVariable, context_instance=RequestContext(request))

        elif password == passwordrepeat:
            for i in range(1,10):
                temp = random.choice(possible)
                token = token + temp

            print token
            if userf.is_valid():
                check = userf.save(commit=False)
                check.email_token = token
                check.email_token_expiry = current_datetime + timedelta(1)
                check.save()
                return HttpResponseRedirect('/')
    else:
        return render_to_response('register.html', {"user": user, 'flag': True}, context_instance=RequestContext(request))

将数据保存到表中后,我需要向用户发送该令牌以确认注册的电子邮件地址。我该怎么做?

如果有人帮助我修复它,我将不胜感激。

models.py:

class Users(models.Model):
    username = models.CharField(max_length=255)
    slug = models.CharField(max_length=255, default='0')
    password = models.CharField(max_length=300)
    passwordrepeat = models.CharField('Repeat Password', max_length=300)
    password_token = models.CharField(max_length=300, default='0')
    email = models.CharField(max_length=255)
    email_verified = models.BooleanField(default=False)
    email_token = models.CharField(max_length=255)
    email_token_expiry = models.DateTimeField()
    tos = models.BooleanField(default=False)
    active = models.BooleanField(default=False)
    last_login = models.DateTimeField(auto_now_add=True)
    last_action = models.DateTimeField(auto_now_add=True)
    is_admin = models.BooleanField(default=False)
    role = models.CharField(max_length=255, default='0')
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now_add=True)

    def __unicode__(self):
        return self.username

class UsersModelForm(forms.ModelForm):
    class Meta:
        model = Users
        fields = ('username', 'password', 'passwordrepeat', 'email')
        widgets = {
            'password' : PasswordInput(),
            'passwordrepeat' : PasswordInput(),
        }

views.py:

def register(request):
    flag = True
    possible = '0123456789abcdefghijklmnopqrstuvwxyz'
    token = ''

    current_datetime = datetime.datetime.now()

    user = UsersModelForm()
    if request.method == 'POST':
        userf = UsersModelForm(request.POST)
        username = userf.data['username']
        password = userf.data['password']
        passwordrepeat = userf.data['passwordrepeat']
        email = userf.data['email']

        password = bcrypt.hashpw(password,bcrypt.gensalt())
        passwordrepeat = bcrypt.hashpw(passwordrepeat,bcrypt.gensalt())

        userf.data['password'] = password
        userf.data['passwordrepeat'] = passwordrepeat

        if password != passwordrepeat:
            flag = False
            passVariable = {'user':user, 'flag': False}
            return render_to_response('register.html', passVariable, context_instance=RequestContext(request))

        elif password == passwordrepeat:
            for i in range(1,10):
                temp = random.choice(possible)
                token = token + temp

            print token
            if userf.is_valid():
                check = userf.save(commit=False)
                check.email_token = token
                check.email_token_expiry = current_datetime + timedelta(1)
                check.save()
                return HttpResponseRedirect('/')
    else:
        return render_to_response('register.html', {"user": user, 'flag': True}, context_instance=RequestContext(request))

After saving the data into table, i need to send the user with that token to confirm the email address for registration. How can i do this?

It will be very much appreciated if someone help me to fix it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光是把杀猪刀 2025-01-03 11:09:39

使用 django 注册,它免费提供所有这些逻辑以及更多功能。请注意,从一些模板示例开始会更容易。

Use django registration, it comes with all this logic and more, for free. Note that it's easier to start with some template examples.

暮凉 2025-01-03 11:09:39

来自 https://docs.djangoproject.com/en/dev/topics/ 的文档电子邮件/

from django.core.mail import send_mail

send_mail('Subject here', 'Here is the message.', '[email protected]',
    ['[email protected]'], fail_silently=False)

:-)

From the docs at https://docs.djangoproject.com/en/dev/topics/email/

from django.core.mail import send_mail

send_mail('Subject here', 'Here is the message.', '[email protected]',
    ['[email protected]'], fail_silently=False)

:-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文