0

I'm trying to create a web application database with 2 types of users (Admin and Patient).

I am also new to Python and Flask.

I'm done with the part about the Admin and now I'm doing the Patient part.

I'm trying to access the user_records.html from my navigation bar which is based on the patient_id. (If you are wondering what is patient id, it is shown below)

While doing so, I encountered an undefined error variable. I tried to debug but had no luck, now I want to ask for some help if you can find and tell me what is causing the error.

Jinja Undefined Error:

UndefinedError
jinja2.exceptions.UndefinedError: 'patients' is undefined

Here are some of my codes.

base.html (includes navbar)

<a class="nav-item nav-link" id="home2" href="/">Home</a>
<a class="nav-item nav-link" id="profile" href="/user/user_profile">Profile</a>
<a class="nav-item nav-link" id="Records" href="/user/user_records/{{patients.id}}">Records</a>
<a class="nav-item nav-link" id="logout" href="/logout">Logout</a>

views.py

@views.route('/user/user_records/<int:patient_id>', methods=['GET', 'POST'])
@login_required
def user_records(patient_id):
patient_id = Views.query.get_or_404(patient_id=patient_id)
if request.method == 'POST':

    name = request.form.get('name')
    note = request.form.get('note')
    doctor = request.form.get('doctor')
    validated = request.form.get('validated')
    date = request.form.get('date')
    tdate = datetime.datetime.strptime(date, '%Y-%m-%d').date()
    file = request.files['file']

    flash('Added successfully', 'success')
    new_view = Views(patient_id=patient_id, name=name, note=note, doctor=doctor, validated=validated, date=tdate, filename=file.filename, data=file.read())
    db.session.add(new_view)
    db.session.commit()
    
view_list = Views.query.filter_by(patient_id=patient_id)
return render_template("user_records.html", user=current_user, view_list=view_list)

user_records.html

<table class="table table-hover table dark">
                        <tr>
                            <th>Date</th>
                            <th>Name</th>
                            <th>Note Type</th>
                            <th>Doctor</th>
                            <th>Validated</th>
                            <th>Actions</th>
                        </tr>
                        {% for rec in rec_list %}
                        <tr>
                            <td>{{ rec.date }}</td>
                            <td>{{ rec.name }}</td>
                            <td>{{ rec.note }}</td>
                            <td>{{ rec.doctor }}</td>
                            <td>{{ rec.validated }}</td>
                            <td width="150">
                                <a href=""><i class="bi bi-eye" style="color: black; font-size:20px;"></i></a>&emsp;*emphasized text*
                                <a href="" value="delete" onclick="return confirm('Are you sure you want to delete?')"><i style="color: black; font-size:20px;" class="bi bi-trash3"></i></a>&emsp;
                                <a href="" value="download"><i style="color: black; font-size:20px;" class="bi bi-download"></i></a>&emsp;
    
                            </td>
                        </tr>
                        {% endfor %}
    
                    </table>

models.py (here is the part where we get the patient_id)

class Patients(db.Model):
     id = db.Column(db.Integer, primary_key=True)
     lname = db.Column(db.String(150))
     fname = db.Column(db.String(150))
     mname = db.Column(db.String(150))
     gender = db.Column(db.String(150))
     age = db.Column(db.String(150))
     phone = db.Column(db.String(150))
     email = db.Column(db.String(150))
     date = db.Column(db.DateTime(timezone=True))
     bday = db.Column(db.DateTime(timezone=True))
     mstatus = db.Column(db.String(150)) 
     address = db.Column(db.String(150))
     phid = db.Column(db.String(150))
     views = db.relationship('Views', backref='patients')

class Views(db.Model):
     id = db.Column(db.Integer, primary_key=True)
     name = db.Column(db.String(150))
     note = db.Column(db.String(150))
     doctor = db.Column(db.String(150))
     validated = db.Column(db.String(150))
     date = db.Column(db.DateTime(timezone=True))
     data = db.Column(db.LargeBinary)
     filename = db.Column(db.String(50))
     patient_id = db.Column(db.Integer, db.ForeignKey('patients.id'))

I hope you can help me to debug the error :) !

Hydroxy21
  • 17
  • 6

1 Answers1

0

You use the variable patients.id in your base template here:

<a class="nav-item nav-link" id="Records" href="/user/user_records/{{patients.id}}">Records</a>

I do not see anywhere in the code where your pass the template an object called patients. Did you mean user.id?

I also see that you refer to rec_list in user_records.html. Did you mean to refer to the view_list object you passed?

Patrick Gorman
  • 132
  • 1
  • 8
  • Thanks for the view_list, already updated it. About the patients.id, I'm trying to access the user_records that are based on the patients.id, I tried it by doing the code at the models.py in the class Views. `patient_id = db.Column(db.Integer, db.ForeignKey('patients.id'))` – Hydroxy21 Dec 29 '22 at 16:09
  • That is a relational column in a database. You're pretty much linking the ```Patients``` object in the database to have a list of associated ```Views``` in the db. This is in no way connected to your UI with the ```{{patients.id}}``` field in it. Jinja is not given a ```patients``` object in render template so it has no way of rending ```patients.id``` and instead throws an error. – Patrick Gorman Dec 29 '22 at 16:25
  • How can I fix it then? What advice can you give to me? sorry just a newbie – Hydroxy21 Dec 29 '22 at 16:39
  • Try adding ```{% if patients is defined %}Records{% endif}``` to your menu so that it only trys to render the patients link if it's defined. https://stackoverflow.com/questions/3842690/in-jinja2-how-do-you-test-if-a-variable-is-undefined – Patrick Gorman Dec 29 '22 at 16:49