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> *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> 
<a href="" value="download"><i style="color: black; font-size:20px;" class="bi bi-download"></i></a> 
</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 :) !