单击烧瓶中的按钮,检索数据库文件更新表单

时间:2019-03-20 20:49:52

标签: html python-3.x flask

我试图制作一个html页面和flask路由,当我按下一个按钮时将打开数据库更新表单,但是到目前为止,我仅收到一条错误消息,指出该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/>
 &nbsp;&nbsp;<a href='/python_forum'>Back Home</a>
 </body>
 </html>

pythonforum:

<!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文件中获取一行并对其进行编辑。我想制作一个将在数据库文件中添加新行的表格。我可以弄清楚html表单,但是我不能弄清楚如何制作一条允许用户在按下按钮时转到表单的路线。

1 个答案:

答案 0 :(得分:0)

要添加新路线以在表中添加行,您可以编辑代码或添加其他路线,如下所示:

@app.route('/addform', methods=['GET', 'POST'])
def addform():
  if request.method == 'GET':
      conn = sqlite3.connect("/home/Ethankbdca/mysite/python.db")
      c = conn.cursor()
      sql = "SELECT rank, title, genre, actors, description FROM IMDB Order By rank"
      c.execute(sql)
      r, t, g, a, de = "", "", "", "", ""
      for row in c.fetchall():
          if int(addpythoncode) == int(row[0]):
              r, t, g, a, de = row[0], row[1], row[2], row[3], row[4]
      conn.close()
      return render_template('addpython.html', addpythoncode=addpythoncode, r=r, t=t, g=g, a=a, de=de)
  elif request.method == 'POST':
      conn = sqlite3.connect("/home/Ethankbdca/mysite/python.db")
      sql = "Insert into IMDB (title, director, actors, description) values(" + \
          request.form['genre'] + "', "
      sql = sql + request.form['title'] + ", "
      sql = sql + request.form['director'] + ", "
      sql = sql + request.form['actors'] + ", "
      sql = sql + request.form['description'] + ") "
      conn.execute(sql)
      conn.commit()
      return render_template('pythonforum.html')

代码的elif部分负责向表中添加新行。您没有为数据库提供任何结构,因此我假设该结构基于更新查询。

您可以将此函数添加到python代码中,并确保存在相应的模板表单文件(它们通常与更新表单相同)。更改您的html代码,使其定位到/addform位置。

作为旁注,直接使用sqlite并获取表单参数并构建查询而不进行任何处理不是一个好习惯,因为这可能会导致sql注入。尝试改用sqlalchemy。