Django 不重定向电子邮件验证
Django not redirecting for email verification
注意:我是 django 的新手
我一直在尝试构建一个类似于博客 posts 的应用程序。
我使用 allauth 进行社交登录,auth_views.LoginView.as_view
进行自定义登录。
当有人创建帐户时,他们的电子邮件未经过验证,我在基于函数的视图中使用了 @verified_email_required
装饰器。
现在,当我使用基于 class 的视图时,我希望验证用户以添加或更新 posts..
下面是我在 views.py
中的代码
class LogUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
model = Log
fields = ['title', 'content', 'image']
slug_url_kwarg = 'question'
slug_field = 'slug'
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
def test_func(self):
post = self.get_object()
if self.request.user == post.author:
if EmailAddress.objects.get(user=self.request.user).verified == True:
return True
else:
print('Not verified')
send_email_confirmation(self.request, self.request.user)
return redirect('log-detail', question=post.slug)
else:
return False
当用户未验证时。我根据 send_email_confirmation
功能收到了电子邮件,但我没有被重定向到日志详细信息页面。我仍然可以在未经验证的情况下更新 post。
urls.py:
from django.urls import path
from .views import LogListView, LogDetailView, LogCreateView, LogUpdateView, SolutionDetailView, SolutionsCreateView
urlpatterns = [
path('', LogListView.as_view(), name='home'),
path('new-question', LogCreateView.as_view(), name='log-create'),
path('question/<slug:question>', LogDetailView.as_view(), name='log-detail'),
path('question/update/<slug:question>', LogUpdateView.as_view(), name='log-update'),
path('add-solution/<slug:question>', SolutionsCreateView.as_view(), name='log-solution-create'),
path('solution/<slug:solution>', SolutionDetailView.as_view(), name='log-solution'),
]
send_email_confirmation
功能由allauth提供。
编辑:
给出的解决方案有效..
但它引发了一个新错误:
AttributeError at /new-question Generic detail view LogCreateView must be called with either an object pk or a slug in the URLconf.
加题class
class LogCreateView(LoginRequiredMixin, VerificationRequiredMixin, CreateView):
model = Log
fields = ['title', 'content', 'image']
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
test_func
只是应该 return 用户是否通过测试。但是你return一个redirect
(其中return一个HttpResponseRedirect
)的值是truthy就认为你的用户通过了测试。相反,您应该简单地编写自己的混合来执行此操作:
from django.core.exceptions import PermissionDenied
class VerificationRequiredMixin:
def get_verification_redirect(self):
raise NotImplementedError(
'{} is missing the implementation of the get_verification_redirect() method.'.format(self.__class__.__name__)
)
def dispatch(self, request, *args, **kwargs):
if EmailAddress.objects.get(user=self.request.user).verified:
return super().dispatch(request, *args, **kwargs)
print('Not verified')
send_email_confirmation(self.request, self.request.user)
return self.get_verification_redirect()
class LogUpdateView(LoginRequiredMixin, UserPassesTestMixin, VerificationRequiredMixin, UpdateView):
model = Log
fields = ['title', 'content', 'image']
slug_url_kwarg = 'question'
slug_field = 'slug'
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
def test_func(self):
post = self.get_object()
return self.request.user == post.author
def get_verification_redirect(self):
post = self.get_object()
return redirect('log-detail', question=post.slug)
class LogCreateView(LoginRequiredMixin, VerificationRequiredMixin, CreateView):
model = Log
fields = ['title', 'content', 'image']
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
def get_verification_redirect(self):
return redirect('some_view')
注意:我是 django 的新手
我一直在尝试构建一个类似于博客 posts 的应用程序。
我使用 allauth 进行社交登录,auth_views.LoginView.as_view
进行自定义登录。
当有人创建帐户时,他们的电子邮件未经过验证,我在基于函数的视图中使用了 @verified_email_required
装饰器。
现在,当我使用基于 class 的视图时,我希望验证用户以添加或更新 posts..
下面是我在 views.py
中的代码class LogUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
model = Log
fields = ['title', 'content', 'image']
slug_url_kwarg = 'question'
slug_field = 'slug'
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
def test_func(self):
post = self.get_object()
if self.request.user == post.author:
if EmailAddress.objects.get(user=self.request.user).verified == True:
return True
else:
print('Not verified')
send_email_confirmation(self.request, self.request.user)
return redirect('log-detail', question=post.slug)
else:
return False
当用户未验证时。我根据 send_email_confirmation
功能收到了电子邮件,但我没有被重定向到日志详细信息页面。我仍然可以在未经验证的情况下更新 post。
urls.py:
from django.urls import path
from .views import LogListView, LogDetailView, LogCreateView, LogUpdateView, SolutionDetailView, SolutionsCreateView
urlpatterns = [
path('', LogListView.as_view(), name='home'),
path('new-question', LogCreateView.as_view(), name='log-create'),
path('question/<slug:question>', LogDetailView.as_view(), name='log-detail'),
path('question/update/<slug:question>', LogUpdateView.as_view(), name='log-update'),
path('add-solution/<slug:question>', SolutionsCreateView.as_view(), name='log-solution-create'),
path('solution/<slug:solution>', SolutionDetailView.as_view(), name='log-solution'),
]
send_email_confirmation
功能由allauth提供。
编辑:
给出的解决方案有效.. 但它引发了一个新错误:
AttributeError at /new-question Generic detail view LogCreateView must be called with either an object pk or a slug in the URLconf.
加题class
class LogCreateView(LoginRequiredMixin, VerificationRequiredMixin, CreateView):
model = Log
fields = ['title', 'content', 'image']
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
test_func
只是应该 return 用户是否通过测试。但是你return一个redirect
(其中return一个HttpResponseRedirect
)的值是truthy就认为你的用户通过了测试。相反,您应该简单地编写自己的混合来执行此操作:
from django.core.exceptions import PermissionDenied
class VerificationRequiredMixin:
def get_verification_redirect(self):
raise NotImplementedError(
'{} is missing the implementation of the get_verification_redirect() method.'.format(self.__class__.__name__)
)
def dispatch(self, request, *args, **kwargs):
if EmailAddress.objects.get(user=self.request.user).verified:
return super().dispatch(request, *args, **kwargs)
print('Not verified')
send_email_confirmation(self.request, self.request.user)
return self.get_verification_redirect()
class LogUpdateView(LoginRequiredMixin, UserPassesTestMixin, VerificationRequiredMixin, UpdateView):
model = Log
fields = ['title', 'content', 'image']
slug_url_kwarg = 'question'
slug_field = 'slug'
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
def test_func(self):
post = self.get_object()
return self.request.user == post.author
def get_verification_redirect(self):
post = self.get_object()
return redirect('log-detail', question=post.slug)
class LogCreateView(LoginRequiredMixin, VerificationRequiredMixin, CreateView):
model = Log
fields = ['title', 'content', 'image']
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
def get_verification_redirect(self):
return redirect('some_view')