Hi I am unable to display some string from my custom view backend to django template. The string from backend is able to send over to the client side browser but still unable to display. The error message that I am trying to display is a lockout message from django-axes. it is a custom view function that returns a HttpResponse(JsonResponse(response_data)) where the response_data
is as shown in the image inserted.
Below is what I have tried.
HTML
<div >
<p id="login-success" style="display: none;"></p>
<form role="agent-login" id="" action="{% url 'agent-login' %}" method="post"> {% csrf_token %} <div >
<input type="text" placeholder="Email address" name="email" >
</div>
<div >
<input type="password" placeholder="Password" name="password" >
<i id="togglePassword"></i>
</div>
<p id="password-error" style="display:none;"> {{ errors }}</p>
<div >
<button data-request="ajax-submit" data-target="[role='agent-login']" type="button"> Login now </button>
</div>
</form>
<div style="text-align: center;">
<a href="#" id="agent-forgot"> Forgot Password? </a>
</div>
</div>
views.py
def agent_login(request):
start = time.time()
sop(start)
response_data = {'login': "Failed"}
redirect_url = ''
data = []
error = False
errors = {}
result = 'FAIL'
message = ''
response_status = HTTP_400_BAD_REQUEST
if request.POST:
redirect_url = ''
data = []
error = False
errors = {}
result = 'FAIL'
message = ''
response_status = HTTP_400_BAD_REQUEST
email = request.POST.get('email')
password = request.POST.get('password')
if not email:
message = 'Please enter email.'
errors.update({'email': message})
error = True
elif not validateEmail(email):
message = 'Please enter valid email.'
errors.update({'email': message})
error = True
if not password:
message = 'Please enter password.'
errors.update({'password': message})
error = True
if not error:
try:
Uparceluser.objects.exclude(
status_id__in=[settings.DELETE, 21]
).get(email__iexact=email,usertype_id=settings.USERTYPE_AGENT)
try:
user = authenticate(request, email=email, password=password, usertype=settings.USERTYPE_AGENT)
print("user: %s" % user.name)
if user.is_authenticated and user.is_active:
if user.status_id == 2:
message = 'Your account is inactive. Please verify your email address.'
errors.update({'password': message})
elif user.status_id == 21:
message = 'Your account does not exist. Please contact administrator.'
errors.update({'password': message})
elif user.status_id == 3 and user.usertype_id == 1:
message = 'Your account is blocked. Please contact administrator.'
errors.update({'password': message})
else:
login(request, user)
data = {
"message_code": "agent_login_success"
}
response_status = HTTP_200_OK
result = 'SUCCESS'
message = 'redirect'
redirect_url = reverse('agent:profile')
except AttributeError:
message = 'The email or password does not match'
errors.update({'password': message})
except Uparceluser.DoesNotExist:
message = 'The email or password does not match'
errors.update({'password': message})
return HttpResponse(json.dumps({
'data': data,
'status': result,
'error': error,
'errors': errors,
'redirect': redirect_url,
'message': message
}),
status = response_status
)
EDITED
The following code is what is displaying as depicted in the inserted image.
def user_lockout(request, credentials):
"""
Custom accout lockout message for django-axes.
change custom_account_lockout_message accordingly to what you need.
"""
response_data = {
"login": "Failed",
"error": settings.CUSTOM_AXES_SOFT_LOCKOUT_MESSAGE
}
return HttpResponse(JsonResponse(response_data))
CodePudding user response:
The error message in the browser screenshot isn't included in views.py
.
Are you sure that's the correct file?
Also, I'm not sure if {{ errors }}
can display a list, but it should be a string defined as errors = 'list of error messages'
instead of a list defined as errors = {}
.