Home > OS >  'verifyForm' object has no attribute 'reception'
'verifyForm' object has no attribute 'reception'

Time:10-01

I have a page where it allows the user to enter the reception number, so when the user enter the reception number, it should verify with the database if the reception number exist, but i am having this error: 'verifyForm' object has no attribute 'reception'. How do I solve this error? Updated with traceback error

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/[email protected]/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>

traceback error in my code is this:

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:\Users\TAY\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\exception.py" in inner
  34.             response = get_response(request)

File "C:\Users\TAY\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py" in _get_response
  115.                 response = self.process_exception_by_middleware(e, request)

File "C:\Users\TAY\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py" in _get_response
  113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\TAY\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
  21.                 return view_func(request, *args, **kwargs)

File "E:\Role_based_login_system-master\account\views.py" in verifydetails
  496.                 if form.reception == Photo.reception:

Exception Type: AttributeError at /verifydetails/
Exception Value: 'verifyForm' object has no attribute 'reception'

CodePudding user response:

form = AddForm(request.POST or None) This line says that form can also be None if object is not found.


You're trying to run this line if form.reception == Photo.reception: before checking if form is valid or None.


To solve this problem, do something like:

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

CodePudding user response:

Change form.reception to form.cleaned_data["reception"]

  • Related