'verifyForm'对象没有属性'reception'



我有一个页面,它允许用户输入接收号码,所以当用户输入接收号码时,它应该与数据库验证接收号码是否存在,但我有这个错误:'verifyForm' object has no attribute 'reception'。如何解决这个错误?更新了回溯错误

views.py

@login_required()
def verifydetails(request):
if request.method == 'POST':
#Customername = request.GET['Customername']
form = AddForm(request.POST or None)
if form.reception == Photo.reception:
messages.success(request, 'Both Reception and customer name match')
return redirect('AddMCO')
else:
messages.success(request, 'Both Reception and customer do not match')
return redirect('verifydetails')

else:
form = AddForm()
return render(request, 'verifydetails.html', {'form': form, })

forms.py

class verifyForm(forms.Form):
reception = forms.CharField(label='',
widget=forms.TextInput(
attrs={"class": 'form-control', 'placeholder': 'Enter Reception number'}))
class meta:
model = Photo
fields = ('reception')

verifydetails.html

<!DOCTYPE html>
<html>
<head>
<script>
$(function () {
$("#datetimepicker1").datetimepicker();
});
</script>
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<title>SCS verify details</title>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<!-- CSS only -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js" integrity="sha384-wHAiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js" integrity="sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k" crossorigin="anonymous"></script>
<!-- Font Awesome -->
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
<!-- Moment.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment.min.js" integrity="sha256-VBLiveTKyUZMEzJd6z2mhfxIqz3ZATCuVMawPZGzIfA=" crossorigin="anonymous"></script>
<!-- Tempus Dominus Bootstrap 4 -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tempusdominus-bootstrap-4/5.1.2/css/tempusdominus-bootstrap-4.min.css" integrity="sha256-XPTBwC3SBoWHSmKasAk01c08M6sIA5gF5+sRxqak2Qs=" crossorigin="anonymous" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/tempusdominus-bootstrap-4/5.1.2/js/tempusdominus-bootstrap-4.min.js" integrity="sha256-z0oKYg6xiLq3yJGsp/LsY9XykbweQlHl42jHv2XTBz4=" crossorigin="anonymous"></script>

</head>
{% if messages %}
{% for message in messages %}
<div class="alert alert-danger" role="alert">
<button type = "button" class="close" data-dismiss = "alert">x</button>
{{ message }}
</div>
{% endfor %}
{% endif %}
<body class="m-5">
<div class="container">
<div class="row justify-content-center">
<div class="col-md-4">
<h3>Verify Details</h3>
<a href="{% url 'customer' %}" class="btn btn-dark my-3">Go Back</a>
<div class="card" style="width: 400px">

<form  method="post">
{% csrf_token %}
<div class="form-group m-3">
<label>Reception:</label>
<br>
{{ form.reception}}
</div>

<button type='submit' class="btn btn-primary m-3">Submit</button>
</form>
</div>
</div>
</div>
</div>
</body>
</html>

我的代码中的回溯错误是这样的:

Environment:

Request Method: POST
Request URL: http://127.0.0.1:8000/verifydetails/
Django Version: 2.2.20
Python Version: 3.8.0
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'account.apps.AccountConfig',
'crispy_forms']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django_session_timeout.middleware.SessionTimeoutMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']

Traceback:
File "C:UsersTAYAppDataLocalProgramsPythonPython38-32libsite-packagesdjangocorehandlersexception.py" in inner
34.             response = get_response(request)
File "C:UsersTAYAppDataLocalProgramsPythonPython38-32libsite-packagesdjangocorehandlersbase.py" in _get_response
115.                 response = self.process_exception_by_middleware(e, request)
File "C:UsersTAYAppDataLocalProgramsPythonPython38-32libsite-packagesdjangocorehandlersbase.py" in _get_response
113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:UsersTAYAppDataLocalProgramsPythonPython38-32libsite-packagesdjangocontribauthdecorators.py" in _wrapped_view
21.                 return view_func(request, *args, **kwargs)
File "E:Role_based_login_system-masteraccountviews.py" in verifydetails
496.                 if form.reception == Photo.reception:
Exception Type: AttributeError at /verifydetails/
Exception Value: 'verifyForm' object has no attribute 'reception'

form = AddForm(request.POST or None)这一行表示,如果没有找到对象,form也可以是None


在检查表单是否有效或None之前,您试图运行这行if form.reception == Photo.reception:


要解决这个问题,可以这样做:

if form:
if form.reception == Photo.reception:
........
else(optional):
print("the object doesn't exist")

form.reception更改为form.cleaned_data["reception"]

相关内容

  • 没有找到相关文章

最新更新