Views.py
class ProfileCreate(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
model = User
form_class = ProfileForm
template_name = "profile/profile_new.html"
slug_field = "username"
def get_login_url(self):
return reverse('account_login')
def test_func(self):
a = self.request.user.username
# print (a)
b = self.kwargs['slug']
# print (b)
x = self.get_object().full_name
# print (x)
y = ''
if a == b:
if x == y:
return True
else:
def get_absolute_url(self):
return reverse('profile:view_profile', kwargs={"slug": self.request.user.username})
class ProfileEdit(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
model = User
form_class = ProfileForm
template_name = "profile/profile_new.html"
slug_field = "username"
def get_login_url(self):
return reverse('account_login')
def test_func(self):
a = self.request.user.username
#print (a)
b = self.kwargs['slug']
#print (b)
x = self.get_object().full_name
#print (x)
y = ''
if a == b:
if x != y:
return True
else:
def get_absolute_url(self):
print("failing")
return reverse('profile:profile_view', kwargs={"slug": self.request.user.username})
解释 LoginRequiredMixin强制用户登录。 UserPassesTestMixin检查
如果full_name是''(空)
如果登录用户的用户名与slug字段相同
test_func()
在返回True时都是正确的,但在else
循环中,即,如果测试条件失败,则视图应重定向到' profile_view'但它没有这样做。我在终端中获得输出"failing"
。只有当我在get_absolute_url()
函数中执行相同操作时才会起作用。完整的get_absolute_url()
函数不起作用。我在这做错了什么?
注意:整个过程似乎在这里循环
GET / user1 / edit / HTTP / 1.1" 302 0
GET / as / login /?next = / user1 / edit / HTTP / 1.1" 302 0
这两个URL保持循环。