Django views.py if statement not passing value to HTML File

Viewed 50

In my Django views.py page, I have some code, where the user enter an employee ID with an HTML form, that submits the value to views.py. Using that, I check whether the employee ID is in a database by iterating over each ID in the database, appending it to an empty list, and using an if statement. The parts where I receive the employee ID is working fine, and so is the ID getting appended to the list. However, when I use an if statement to check whether the employee ID that was given is a part of the list, isn't working. Despite this working on Jupyter Notebook when i tested the same.

Here's the code:
views.py:

from django.shortcuts import render
import pandas as pd


dat = pd.read_csv('/Users/Lenovo/Desktop/Cakewala DB/CW_Attendance/AttendanceApp/test_employee.csv')

# Create your views here.
def home(request):
    employee_id = request.POST.get('id')

    print(employee_id)

    id_list = []
    for id in dat["ID"]:
        id_list.append(id)

    print(id_list)

    if employee_id in id_list:
        print("yes")
        employee_row = dat.loc[dat["ID"] == employee_id]
        employee_name = employee_row.iat[0, 1]
        return render(request, 'attendance/index.html', {
            "employee_id": employee_id,
            "employee_name": employee_name
        })

    return render(request, 'attendance/index.html')

index.html:

<!DOCTYPE html>
<html>
    <head>
        <title>Attendance - Home</title>
    </head>
    <body>
        <h1>Cakewala Attendance</h1>
        <form method="POST">
            {% csrf_token %}
            <label for="id">Employee ID</label> <br>
            <input type="number" name="id" max="9999" min="1000">
            <input type="submit" id="submit-btn"> <br>
            <p id="name">{{ employee_id }}: {{ employee_name }}</p>
            <input type="checkbox">
        </form>
    </body>
</html>

test_employee.csv:

ID, NAME
1001, John Doe
1002, Jane Doe

Command Line Output when I pass employee ID as 1001:

Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
April 15, 2022 - 13:22:00
Django version 4.0.4, using settings 'CW_Attendance.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
1001
[1001, 1002]
[15/Apr/2022 13:33:17] "POST / HTTP/1.1" 200 598

I need to display the Employee ID and Name in the HTML file, any help would be greatly appreciated!

0 Answers
Related