Здесь объект User создается с атрибутом is_active = False . Я проверяю имя входа вместе с AuthenticationForm с теми же учетными данными, но не получаю ожидаемого ответа.
При передаче данных в форму: форма.is_valid () возвращает значение False.
https://docs.djangoproject.com/en/2.2/topics/auth/default/#django.contrib.auth.forms.AuthenticationForm В документах указано:
По умолчанию AuthenticationForm отклоняет пользователей, для флага is_active которых установлено значениеFalse.
Как установить для User.is_active значение False, но также впоследствии вводить условные блоки, чтобы соответствующее флэш-сообщение возвращалось в зависимости от состояния указанного атрибута?
def sign_in(request):
import pdb; pdb.set_trace()
form = AuthenticationForm()
if request.method == 'POST':
form = AuthenticationForm(data=request.POST)
if form.is_valid():
if form.user_cache is not None:
user = form.user_cache
if user.is_active:
login(request, user)
return HttpResponseRedirect(
reverse('home') # TODO: go to profile
)
else:
messages.error(
request,
"That user account has been disabled."
)
else:
messages.error(
request,
"Username or password is incorrect."
)
return render(request, 'accounts/sign_in.html', {'form': form})
class UserAccountSignIn(TestCase):
@classmethod
def setUpTestData(cls):
cls.inactive_user = {
'username': 'non_user',
'password': 'secret',
'is_active': False
}
User.objects.create_user(**cls.inactive_user)
def test_inactive_user_login_fail(self):
self.client.login(**self.inactive_user)
response = self.client.post(reverse('accounts:sign_in'), self.inactive_user)
self.assertContains(response, "That user account has been disabled.")
https://github.com/django/django/blob/master/django/contrib/auth/forms.py#L213
Фактические результаты: ValidationError("This account is inactive.")
Ожидаемые результаты: мигает сообщение: "That user account has been disabled."