我在Django python中工作,写了这两条语句来检查数据库中是否存在电子邮件或用户名,但消息来自电子邮件和用户名,但我希望如果我在注册表单中输入任何一个,它应该单独显示
if User.objects.filter(email=email).exists():
# error_message = ''
email_error = ('', 'Email is taken. ')[User.objects.filter(email=email).exists()]
messages.success(request, email_error)
elif User.objects.filter(username=username).exists():
username_error = ('', 'Username is taken. ')[User.objects.filter(username=username).exists()]
messages.success(request, username_error)
return redirect('/register/')
if User.objects.filter(email=email).exists():
# error_message = ''
email_error = ('', 'Email is taken. ')[User.objects.filter(email=email).exists()]
messages.success(request, email_error)
return redirect('/register/')
elif User.objects.filter(username=username).exists():
username_error = ('', 'Username is taken. ')[User.objects.filter(username=username).exists()]
messages.success(request, username_error)
return redirect('/register/')
仅从两种条件重定向
如果你想有Or行为(电子邮件存在、用户名存在或两者都存在(,你需要执行以下操作:
email_taken = User.objects.filter(email=email).exists()
username_taken = User.objects.filter(username=username).exists()
if email_taken:
messages.error("Email is taken.")
if username_taken:
messages.error("Username is taken.")
if email_taken or username_taken:
return redirect('/register/')