我提前道歉,我确信这是一个愚蠢的问题。我想在周末尝试自学烧瓶,但是我遇到了第一道障碍。这是我的计划的基本概念:
这是我的主要代码:
import os
import sqlite3
from flask import Flask, request, session, g, redirect, url_for, abort, \
render_template, flash
app = Flask(__name__)
app.config.from_object(__name__)
# Load default config and override config from an environment variable
app.config.update(dict(
DATABASE=os.path.join(app.root_path, 'plugin.db'),
))
app.config.from_envvar('PLUGINDB_SETTINGS', silent=True)
def connect_db():
"""Connects to the specific database."""
rv = sqlite3.connect(app.config['DATABASE'])
rv.row_factory = sqlite3.Row
return rv
def init_db():
with app.app_context():
db = get_db()
with app.open_resource('schema.sql', mode='r') as f:
db.cursor().executescript(f.read())
db.commit()
@app.cli.command('initdb')
def initdb_command():
"""Initializes the database."""
init_db()
print 'Initialized the database.'
def get_db():
"""Opens a new database connection if there is none yet for
the current application context
"""
if not hasattr(g, 'sqlite_db'):
g.sqlite_db = connect_db()
return g.sqlite_db
@app.teardown_appcontext
def close_db(error):
"""Closes the database again at the end of the request."""
if hasattr(g, 'sqlite_db'):
g.sqlite_db.close()
@app.route("/",methods=["GET","POST"])
@app.route("/index",methods=["GET","POST"])
def index():
db = get_db()
cur = db.execute('select * from t0109161144 where pluginID is 19506')
plugins = cur.fetchall()
return render_template("index.html")
@app.route('/query', methods=["GET","POST"])
def script_id():
db = get_db()
script_id = request.form['script_id']
plugin_info = dependencies(script_id)
return redirect(url_for('index'))
def dependencies(script_id):
db = get_db()
dependencies = db.execute("select * from t0109161144 where pluginID is (script_id) values (?)', (script_id)
return dependencies
在我测试一些不同的东西的过程中,我的脚本变得非常拥挤。这是我当前的index.html:
{% extends "layout.html" %}
{% block body %}
<form action="{{ url_for('index') }}" method=post class=index>
<dl>
<dt>Plugin ID:
<dd><input type=text size=30 name=script_id>
<dd><input type=submit value=Query>
</dl>
</form>
<ul class=plugins>
{% for plugin in plugins %}
<li><h2>{{ plugin.pluginID }}</h2>{{ plugin.pluginname|safe }}
{% else %}
<li><em>Unbelievable. No entries here so far</em>
{% endfor %}
</ul>
{% endblock %}
我想做的是:
我尝试对已知的好值进行硬编码,但这甚至没有出现在index.html上:
cur = db.execute('select * from t0109161144 where pluginID is 19506')
在这里发帖之后,我可以看到我肯定有不必要的代码,但我只是希望应用程序的这一部分工作,然后我将继续清理代码。任何指针或建议将不胜感激,谢谢!
答案 0 :(得分:1)
您没有将plugins
传递给您的模板。 index
的最后一行应为
return render_template("index.html", plugins=plugins)