2017-03-12 37 views
0

我是新来的Python和我目前正在创建使用瓶和MySQL一个页面,让信息(姓,名和电子邮件)提交到数据库和我的index.html显示。如何根据所选条目路由到Flask中的MySQL ID?

我遇到的挑战之一是让每个条目具有编辑按钮,让用户访问URL(在这种情况下,“/ friends/id/edit”)将显示编辑页面该特定用户。我如何以允许它转到正确的URL并在数据库中编辑正确信息的方式编写它?

我server.py文件:

@app.route('/', methods=["GET"]) 
def index(): 
    query = "SELECT * FROM friends" 
    friends = mysql.query_db(query) 
    return render_template('index.html', all_friends=friends) 

@app.route('/friends', methods=["POST"]) 
def create(): 
     if len(request.form['email']) < 1: 
      flash("Email cannot be blank.", "error") 
      return redirect('/') 
     if not EMAIL_REGEX.match(request.form['email']): 
      flash("Invalid email address", "error") 
      return redirect('/') 
     else: 
      query = "INSERT INTO friends (first_name, last_name, email, created_at) VALUES (:first_name, :last_name, :email, NOW())" 
      data = { 
        'first_name': request.form['first_name'], 
        'last_name': request.form['last_name'], 
        'email': request.form['email'] 
        } 

      mysql.query_db(query, data)  
      query= "SELECT * FROM friends" 
      friend = mysql.query_db(query) 
      return render_template('index.html', all_friends=friend) 

@app.route('/friends/<id>/edit', methods=["GET"]) 
def edit(id): 
     return render_template('/edit.html') 

的index.html:

<!DOCTYPE html> 
<html> 
    <head> 
    <meta charset="utf-8"> 
    <title>Full Friends</title> 
    </head> 
    <body> 
    <h1>Full Friends</h1> 
    {% for friend in all_friends: %} 
     <p>ID: {{ friend['id'] }} First Name: {{ friend['first_name'] }} Last Name: {{ friend['last_name'] }} Email: {{ friend['email'] }} Timestamp: {{ friend['timestamp'] }} <form action="/friends/<id>/edit" method=["GET"]><input id="edit" type="submit" value="Edit"></form> <form action="/friends/<id>/delete"><input id="delete" type="submit" method="POST" value="Delete"></form></p> 
    {% endfor %} 

    <div id="add_contact"> 
     {% with messages = get_flashed_messages(with_categories=true) %} 
     {% if messages %} 
      <ul class=flashes> 
      {% for category, message in messages %} 
      <li class="{{ category }}">{{ message }}</li> 
      {% endfor %} 
      </ul> 
     {% endif %} 
     {% endwith %} 

     <form action="/friends" method="POST"> 
     <p class="titles">First Name:</p><input type="text" name="first_name"> 
     <p class="titles">Last Name:</p><input type="text" name="last_name"> 
     <p class="titles">Email:</p><input type="text" name="email"><br> 
     <input type="submit" value="Add Friend"> 
     </form> 
    </div> 
    </body> 
</html> 

回答

0

我相信你正在寻找建立在你的模板,你可以实现像下方的“修改”链接。你不需要一个'表单'。你可以使用css来设置链接的样式。

{% for friend in all_friends: %} 
    <p> 
    <a href="/friends/{{friend['id']}}/edit">Edit</a> 
    </p> 
{% endfor %} 
+0

谢谢!这比我想象的要简单得多。 – Ziyal

0

只要你不关心安全性。你可以做这样的事情。

server.py文件:

@app.route('/friends/<id>/edit', methods=["GET"]) 
def edit(id): 
    return render_template('/edit.html', id=id) 

@app.route('/friends/<id>/edit', methods=["POST"]) 
def edit(id): 
    # Do anything with the POST data 
    # and modify database 
    return render_template('/edit.html', id=id) 

edit.html文件:

<html> 
<head> 
    <meta charset="utf-8"> 
    <title>Full Friends</title> 
</head> 
<body> 
    <span> friend id: {{ id }} </span> 
    <form action="/friends/{{ id }}/edit" method="POST"> 
    <p class="titles">First Name:</p><input type="text" name="first_name"> 
    <p class="titles">Last Name:</p><input type="text" name="last_name"> 
    <p class="titles">Email:</p><input type="text" name="email"><br> 
    <input type="submit" value="Modify Friend"> 
    </form> 
</body> 
</html> 

这里的想法是,你只有一个,你与数据库中的数据填充模板,你可以区分用户在他们的ID后发送请求。

[编辑] 我误解你的问题。您还必须在index.html中用/friends/{{ friend['id'] }}/edit代替/friends/<id>/edit。这样你就可以为每个用户提供独特的链接。您可以使用ID来搜索数据库并使用用户信息填充模板。

相关问题