在视图.py在
class DashboardNewProfile(LoginRequiredMixin, CreateView):
form_class = ProfileForm
model = Profile
queryset = Profile.objects.all()
template_name = 'profile/profile_create.html'
def _usercheck(self):
u = self.request.user
qs = Profile.objects.all().filter(u)
if qs is None:
return HttpResponseRedirect('/profile/create/')
else:
return HttpResponseRedirect('/profile/view/')
def get_form_kwargs(self):
kwargs = super(DashboardNewProfile, self).get_form_kwargs()
kwargs.update({'user': self.request.user})
kwargs.update({'slug': self.request.user.username})
return kwargs
在网址.py在
^{pr2}$我该怎么做? 如果登录的用户已经有一个与数据库中的配置文件对象关联的配置文件。我不希望用户访问URL('/profile/create/'),而不是希望用户自动重定向到另一个URL('/profile/view')
为了更改
LoginRequiredMixin
的功能,您需要重写dispatch
方法,该方法检查用户是否登录。看起来您真正想要的是user_passes_test
装饰器:Documentation for user_passes_test
Documentation for method_decorator
Documentation for filtering
请注意,如果您愿意,还有一个UserPassesTestMixin,我只是通常将这些测试分开,以便它们可以在其他视图中使用,并且更喜欢装饰器的外观。在
相关问题 更多 >
编程相关推荐