电子邮件确认类视图



当用户单击电子邮件中的链接时,我目前正在尝试显示一个确认html页面。我在电子邮件中发送的URL是http://localhost:8000/confirm/?email=hello@example.com&conf_num=639836786717

我的类视图,为了验证它是否是正确的conf_number和电子邮件是否是这样的

class ConfirmView(DetailView):
template_name = 'confirm.html'
def get(self, request, **kwargs):
context = super(ConfirmView, self).get_context_data(**kwargs)
email = request.GET['email']
sub = Newsletter.objects.get(email=email)
if sub.conf_num == request.GET['conf_num']:
sub.confirmed = True
sub.save()
action = "added"
else:
action = 'denied'
context["email"] = email
context['action'] = action
return context

但是我在这行context = super(ConfirmView, self).get_context_data(**kwargs)上得到了这个错误AttributeError: 'ConfirmView' object has no attribute 'object',现在我不确定这是因为我在自定义类视图上调用Super吗?

基本上,如果确认电子邮件与我们在时事通讯对象中的内容匹配,我希望它发送到模板操作,如果不匹配,则添加并拒绝。如有任何帮助,我们将不胜感激。

根据文档,您尚未定义get_queryset方法get_object方法型属性

get方法应返回:return self.render_to_response(context)

编辑:正如Mugoma的回复中提到的,你应该定义其中一个。您可以在此处检查所有可用的方法:https://ccbv.co.uk/projects/Django/3.0/django.views.generic.detail/DetailView/

编辑2:另外,在我看来,这不应该是一个DetailView。DetailView通常用于显示给定模型的实例,其中:

在执行此视图时,self.object将包含视图正在操作的对象。

在您的情况下,一些基本视图应该足够了,例如TemplateView

最新更新