gpt4 book ai didi

Django 确认电子邮件。将值从 false 更改为 true

转载 作者:行者123 更新时间:2023-12-02 06:34:32 25 4
gpt4 key购买 nike

这是我的模型:

from django.db import models

class Meeting(models.Model):
name = models.CharField(max_length=255)
time = models.DateTimeField()
confirmed = models.BooleanField(default=False)

这是我的表格:

from django import forms

class MeetingForm(forms.Form):
name = forms.CharField(max_length=100)
time = forms.DateTimeField()
user_name = forms.CharField(max_length=100)
user_email = forms.EmailField()

如何创建一个 View ,通过链接向用户发送邮件,并且当用户单击此链接时,确认字段会将值更改为 true?

链接是我最大的问题。

最佳答案

你可以做这样的事情

型号

import random
import string

class Meeting(models.Model):
name = models.CharField(max_length=255)
time = models.DateTimeField()
confirmed = models.BooleanField(default=False)
key = models.CharField(max_length=32)

def save(self, *args, **kwargs):
'''
On save generate key which will be sent in email
'''
self.key = ''.join(random.choice(string.letters) for i in xrange(32))
super(Meeting, self).save(*args, **kwargs)
return self

View .py

def confirm(request, key):
try:
meeting = Meeting.objects.get(key=key)
except Meeting.DoesNotExist:
# wrong key, do something, redirect to somewhere etc
template = 'error.html'
else:
meeting.confirmed = True
meeting.save()
template = 'success.html'
render(request, template)

def send_email(request):
email = request.user.email
meeting = Meeting.objects.get(...your condition here...)
send_mail('Confirmation email',
# this is a link you wanted to
'<a href="%s">confirm</a>' % reverse('user_confirm',
kwargs={'key': meeting.key}),
'from@youremail.com', [email])
render(request, 'mailsent.html')

url.py

urlpatterns = patterns('',
url(r'^confirm/(?P<key>[\w]{32})/$', 'yourapp.views.confirm',
name='user_confirm'),
)

关于Django 确认电子邮件。将值从 false 更改为 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12483742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com