Как получить сообщение об успехе для объекта модели в django
Я пытаюсь создать аутентификацию в моем проекте после входа в систему. Я создал свои модели. Я хочу, чтобы пользователь получал сообщение об ошибке, если он не ввел правильный пин в модели, и об успехе, если он ввел правильный пин. Я получаю только сообщение об ошибке, но не получаю сообщение об успехе
class Data(models.Model):
name = models.CharField(max_length=100)
body = models.CharField(max_length=1000000)
first_name = models.CharField(max_length=200)
last_name = models.CharField(max_length=200)
other_name = models.CharField(max_length=200)
email = models.EmailField()
profile_image = models.ImageField(blank=True)
city = models.CharField(max_length= 100)
title = models.TextField(null=True, blank=True)
middlename = models.TextField(null=True, blank=True)
adreess = models.TextField(null=True, blank=True)
country = models.TextField(null=True, blank=True)
state = models.TextField(blank=True, verbose_name="Biography (Age, Language, Location...)")
pin = models.IntegerField()
available = models.TextField(null=True, blank=True)
checkings = models.TextField(null=True, blank=True)
savings = models.TextField(null=True, blank=True)
phone_number = models.TextField(null=True, blank=True)
first_his = models.TextField(null=True, blank=True)
second_history = models.TextField(null=True, blank=True)
third_history = models.TextField(null=True, blank=True)
last_history = models.TextField(null=True, blank=True)
newstate = models.TextField(null=True, blank=True)
def __str__(self):
return self.first_name
Это мое мнение
def checkview(request):
pin = request.POST['pin']
if Data.objects.filter(name=pin).exists():
messages.success(request, "You have registered successfully")
else:
messages.error(request, 'Account Suspended, Your Request To Transfer funds Has Been Declined. Contact Support for Help')
return redirect('account')
Это мой Htm файл
<div class="pagetitle">
<h1>Pin Code Required to Complete Transfer Request</h1>
<style>
h8{
color: red;
}
</style>
{% for message in messages %}
<h8> {{message}}</h8>
{% endfor %}
<form action = "checkview" method="POST" autocomplete="on">
{% csrf_token %}
<!--Second name-->
<div class="box">
<label for="pin" class="fl fontLabel"> </label>
<div class="fl iconBox"><i class="fa fa-user" aria-hidden="true"></i></div>
<div class="fr">
<input type="text" name="pin" maxlength="4"
class="textBox">
</div>
<div class="clr"></div>
{% if error %}
<span style="color:red">{{error}}</span>
{% endif %}
</div>
<!--Second name-->
<!---Submit Button------>
<input type="Submit" value="Transfer">
<!---Submit Button----->
</form>
Ваше представление должно возвращать ответ в случае, когда пин существует:
if Data.objects.filter(name=pin).exists():
messages.success(request, "You have registered successfully")
# your view should return a response here also
else:
messages.error(request, 'Account Suspended, Your Request To Transfer funds Has Been Declined. Contact Support for Help')
return redirect('account')
Я думаю, что вы ничего не возвращаете при успехе. в вашем случае:
def checkview(request):
pin = request.POST.get('pin') or ''
if Data.objects.filter(name=pin).exists():
messages.success(request, "You have registered successfully")
else:
messages.error(request, 'Account Suspended, Your Request To Transfer funds Has Been Declined. Contact Support for Help')
return redirect('account')
По-моему, в строке:
допущена опечатка.if Data.objects.filter(name=pin).exists():
что должно быть 'pin=pin', а не 'name=pin'
if Data.objects.filter(pin=pin).exists():