Reputation: 9
I am passing the information to my template, but when i load the local server I cannot see anything when the expected output is a list of passwords that are being stored in my database. I put in a bunch of print statements to help debug the code, but it seems everything is being processed fine. The function that's processing the code is as follows:
@app.route('/dashboard')
def dashboard():
if 'user' not in session:
print("User not found!!")
return redirect(url_for('login'))
user_id = session['user']['id']
print(f"\nDEBUG: Logged-in user ID -> {user_id}") # Debugging
with sqlite3.connect('database.db') as conn:
cursor = conn.cursor()
cursor.execute('SELECT service, username, password FROM passwords WHERE user_id = ?', (user_id,))
rows = cursor.fetchall()
cursor.execute('SELECT COUNT(*) FROM passwords WHERE user_id = ?', (user_id,))
total_passwords = cursor.fetchone()[0]
cursor.execute("SELECT COUNT(*) FROM passwords WHERE user_id = ? AND strength = 'Strong'", (user_id,))
strong_count = cursor.fetchone()[0]
cursor.execute("SELECT COUNT(*) FROM passwords WHERE user_id = ? AND strength = 'weak'", (user_id,))
weak_count = cursor.fetchone()[0]
cursor.execute("SELECT COUNT(*) FROM bankcards WHERE user_id = ?", (user_id,))
total_cards = cursor.fetchone()[0]
cursor.execute("SELECT COUNT(*) FROM notes WHERE user_id = ?", (user_id,))
total_notes = cursor.fetchone()[0]
print("\nDEBUG: Retrieved passwords ->", rows) # Debugging
# Convert tuples into dictionaries for better template handling
passwords = [{'service': row[0], 'username': row[1], 'password': row[2]} for row in rows]
name = get_name(user_id)
# Check if passwords are passed to the template
response = render_template('dashboard.html',
user=session['user'],
passwords=passwords,
total_passwords=total_passwords,
strong_count=strong_count,
weak_count=weak_count,
total_cards=total_cards,
total_notes=total_notes,
name=name)
print("\nDEBUG: Rendering dashboard with passwords ->", passwords) # Debugging
return response
And this is the html code
<div class="card-body">
<div class="row row-cols-1 g-2">
{% if passwords %}
{% for entry in passwords %}
<div class="col">
<div class="card shadow-sm p-2 d-flex flex-row align-items-center">
<!-- Service Initial -->
<div class="rounded-circle bg-primary text-white d-flex justify-content-center align-items-center"
style="width: 40px; height: 40px;">
{{ entry.service[0]|upper }} <!-- First letter of the service -->
</div>
<!-- Service & Username -->
<div class="ms-3 flex-grow-1">
<h6 class="mb-0">{{ entry.service }}</h6> <!-- Service name -->
<small>{{ entry.username }}</small> <!-- Username -->
</div>
<!-- Password Field (Hidden by Default) -->
<div class="password-container d-flex align-items-center">
<input type="password" class="form-control form-control-sm me-2 password-field"
value="{{ entry.password }}" readonly style="width: 150px; border: none; background: none;">
<!-- Eye Toggle Button -->
<button class="btn btn-outline-secondary btn-sm toggle-password">
<i class="bi bi-eye"></i> <!-- Bootstrap Icons Eye -->
</button>
</div>
</div>
</div>
{% endfor %}
{% else %}
<p class="text-center">No saved passwords.</p>
{% endif %}
</div>
</div>
Upvotes: 0
Views: 62