【发布时间】:2019-10-18 09:12:39
【问题描述】:
我在显示另一个表中的值时遇到了一些问题(使用下面的代码会更容易理解):
contact.py
def index():
db = get_db()
contacts = db.execute(
'SELECT *'
' FROM contact INNER JOIN branche ON contact.branche_id = branche.branche_id'
' ORDER BY contact.nom ASC'
).fetchall()
return render_template('contact/index.html', contacts=contacts)
templates/contact/index.html
<table class="table table-hover">
<thead class="thead-light">
<th>Nom</th>
<th>Prénom</th>
<th>Adresse Mail</th>
<th>Code Postal</th>
<th>Ville</th>
<th>Téléphone Portable</th>
<th>Branche</th>
</thead>
<tbody>
<div class="col-md-4">
<div class="form-group">
<div runat="server" id="div_columns" >
{% for contact in contacts %}
<tr>
<td>{{ contact['nom'] }}</td>
<td>{{ contact['prenom'] }}</td>
<td>{{ contact['telportable'] }}</td>
<td>{{ contact['adressemail'] }}</td>
<td>{{ contact['codepostal'] }}</td>
<td>{{ contact['ville'] }}</td>
<td>{{ contact['telportable'] }}</td>
<td>{{ branche['branche_nom'] }}</td>
<td>
<a class="btn"><i class="fas fa-pencil-alt"></i></a>
<a class="btn text-danger "><i class="fa fa-trash fa-lg"></i></a>
</td>
</tr>
{% endfor %}
<hr>
</div>
</div>
</div>
</tbody>
</table>
schema.sql
DROP TABLE IF EXISTS contact;
DROP TABLE IF EXISTS branche;
CREATE TABLE contact (
id INTEGER PRIMARY KEY AUTOINCREMENT,
nom TEXT NOT NULL,
prenom TEXT NOT NULL,
adressemail TEXT NOT NULL,
adressepostale TEXT NOT NULL,
codepostal INTEGER NOT NULL,
ville TEXT NOT NULL,
telfixe INTEGER NOT NULL,
telportable INTEGER NOT NULL,
branche_id INTEGER NOT NULL,
FOREIGN KEY (branche_id) REFERENCES branche (branche_id)
);
CREATE TABLE branche (
branche_id INTEGER PRIMARY KEY AUTOINCREMENT,
branche_nom TEXT UNIQUE NOT NULL
);
我要做的就是打印联系人信息(姓名、电话号码……),contact 表中没有一个信息,它是branche_nom 里面branche 表,那么我的错误在哪里?我在终端计算机或浏览器终端内没有任何错误
P.S : 当我禁用 branche_nom 的显示时,存储在 DB 中的所有行都会被打印出来,我只是没有想要使用另一个表的东西
编辑 1:加载 branche 表
contact.py
def index():
db = get_db()
contacts = db.execute(
'SELECT contact.nom, contact.prenom, contact.adressemail, contact.codepostal, contact.ville, contact.telportable, branche.branche_nom'
' FROM contact INNER JOIN branche ON contact.branche_id = branche.branche_id'
' ORDER BY contact.nom ASC'
).fetchall()
branches = db.execute(
'SELECT branche_nom'
' FROM branche INNER JOIN contact ON contact.branche_id = branche.branche_id'
).fetchall()
return render_template('blog/index.html', contacts=contacts, branches=branches)
Branche List 的截图:
EDIT2:db.py
import sqlite3
import click
from flask import current_app, g
from flask.cli import with_appcontext
def init_app(app):
app.teardown_appcontext(close_db)
app.cli.add_command(init_db_command)
def init_db():
db = get_db()
with current_app.open_resource('schema.sql') as d:
db.executescript(d.read().decode('utf8'))
@click.command('init-db')
@with_appcontext
def init_db_command():
"""Clear the existing data and create new tables."""
init_db()
click.echo('Initialized the database.')
def get_db():
if 'db' not in g:
g.db = sqlite3.connect(
current_app.config['DATABASE'],
detect_types=sqlite3.PARSE_DECLTYPES
)
g.db.row_factory = sqlite3.Row
return g.db
def close_db(e=None):
db = g.pop('db', None)
if db is not None:
db.close()
【问题讨论】:
-
您根本没有将
branche变量传递给您的模板,也没有在查询中选择任何branche列。 -
@AKX 我尝试添加它,但没有任何变化(目前)