【发布时间】:2019-03-20 20:49:51
【问题描述】:
我正在尝试创建一个 html 页面和烧瓶路由,当我按下按钮时将打开一个数据库更新表单,但到目前为止,我只收到一个错误,说 url 不存在或服务器超载。这是我所拥有的:
路线:
@app.route('/python_forum')
def python_forum():
conn = sqlite3.connect("/home/Ethankbdca/mysite/python.db")
c = conn.cursor()
sql = "SELECT rank, codename, name, actors, description FROM pythonpage Order By rank"
c.execute(sql)
pythonforum = []
for row in c.fetchall():
tup = (row[0],row[1],row[2],row[3],row[4])
pythonforum.append(tup)
conn.close()
return render_template('pythonforum.html', pythonforum=pythonforum)
@app.route('/addform', methods=['GET', 'POST'])
def addform():
if request.method == 'GET':
'''conn = sqlite3.connect("/home/Ethankbdca/mysite/python.db")
f = conn.cursor()
sql = "SELECT rank, codename, name, actors, description FROM pythonpage Order By rank"
f.execute(sql)
r, c, n, a, d = "", "", "", "", ""
for row in f.fetchall():
if int(addpythoncode) == int(row[0]):
r, c, n, a, d = row[0], row[1], row[2], row[3], row[4]
conn.close()'''
return render_template('addpython.html')
elif request.method == 'POST':
conn = sqlite3.connect("/home/Ethankbdca/mysite/python.db")
sql = "Insert into pythonpage ('codename', 'name', 'description') values('" + \
request.form['name'] + "', "
sql = sql + "'" + request.form['codename'] + "', "
sql = sql + "'" + request.form['description'] + "') "
conn.execute(sql)
conn.commit()
return render_template('pythonforum.html')
@app.route('/pythoncode')
def pythoncode():
conn = sqlite3.connect("/home/Ethankbdca/mysite/python.db")
c = conn.cursor()
sql = "SELECT description FROM pythonpage Order By description"
c.execute(sql)
pycodeforum = []
for row in c.fetchall():
tup = (row[0])
pycodeforum.append(tup)
conn.close()
return render_template('updatepython.html', pycodeforum=pycodeforum)
html代码形式“addpythoncode”:
<!DOCTYPE html>
<html>
<head>
<link data-require="bootstrap@4.1.3" data-semver="4.1.3" rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" />
<script data-require="bootstrap@4.1.3" data-semver="4.1.3" src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body>
<h1 align='center'>Add Python Code</h1>
<table width="90%">
<form method="POST">
<tr>
<td>Code name</td>
<td><input type=text name="codename" placeholder="name your code" value='{{c}}' size=40 /></td>
</tr>
<tr>
<td>Name</td>
<td><input type=text name=name placeholder="enter your name" value='{{n}}' size=50 /></td>
</tr>
<tr>
<td>Description</td>
<td><textarea name='description' placeholder="enter your code" rows=5 cols=50>{{d}}</textarea></td>
</tr>
<tr>
<tr>
<td><input type=hidden name=rank value={{r}} /></td>
<td><input type=submit value='submit' class='btn btn-primary' /></td>
</tr>
</form>
</table><br/>
<a href='/python_forum'>Back Home</a>
</body>
</html>
python论坛:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css"/>
<script src="script.js"></script>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
padding: 15px;
text-align: left;
border-bottom: 3px solid #ddd;
}
</style>
<style>
.button {
background-color: #1E90FF; /* Blue */
border: none;
color: white;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
left: 87%;
position: fixed;
top: 80px;
}
.button1 {padding: 10px 24px;}
.button1 {border-radius: 8px;}
</style>
<style type="text/css">
a.nounderline {text-decoration: none; }
</style>
<style type="text/css">
a.nounderline {text-decoration: none; }
</style>
</head>
<body>
<h1 align="center">Python Forum</h1>
<br/>
<button type="submit" style="float: right;" class="button button1"><a href="/addform"</>Add code</button>
<table width="90%">
<br/>
{% for x in pythonforum: %}
<tr>
<td width=20%><a href='/pythoncode' >{{ x[1] }}</a></td>
<td width=20%> {{ x[2] }} </td>
</tr>
{% endfor %}
</table><br/>
</body>
</html>
这样做是试图从已经存在的 .db 文件中取出一行并对其进行编辑。我想制作一个将新行添加到 db 文件的表单。我可以弄清楚 html 表单,但我无法弄清楚如何制作一条允许用户在按下按钮时转到表单的路线。
【问题讨论】:
-
请从您的调试控制台提供更多信息。
-
问题是我基本上编写了一个代码来更新 .db 中的当前条目,而不是创建一个新条目。是否有可能为我提供一种方法来做到这一点?
-
您的代码似乎有不同的问题。首先尝试解决这个问题。更新查询似乎还可以。如果您从终端运行代码(例如使用 flask 命令运行它),请查看重新加载页面时显示的错误消息。
-
所以这是试图从已经存在的 .db 文件中取出一行并 eidt 。我想制作一个表格,将新行添加到 db 文件中。
-
逻辑与update类似,但使用不同的sql命令。如果您在编写 sql 查询时遇到问题,请至少提供 db 结构(用于初始化 db 的代码)。
标签: html python-3.x flask