我想列出应该使用python flask显示的链接列表

时间:2018-08-21 09:39:13

标签: html python-3.x flask

这是我的代码。它的工作正常,但我希望删除按钮与编辑按钮在同一行。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>List of Links</title>
</head>
<body>
    <h1>List of Links</h1>
    <div>Add a new link: 
        <label action="/add" method="POST">
            <input type="text" name="link">
            <input type="submit">
        </label>
    </div>
    <div>
        <ul>
            {% for link in links %}
            <li style="font-size: 15pt">{{ link.text }}</li>
            <form action="/edit" method="POST">
            <input type="hidden" name = "old_text" value={{ link.text }}>
            <input type="text" name = "new_text" value="">
            <input type="submit" value="Edit">
            </form>
            <form action="/delete" method="POST">
            <input type="hidden" name = "text" value={{ link.text }}>
            <input type="submit" value="Delete">
            </form>
            {% endfor %}
        </ul>
    </div>
</body>
</html>

因此,我尝试使用标签来编写代码,因为在表单标签中我们无法对齐按钮。但是现在删除和编辑按钮不起作用。 另外,对于“添加新链接:”部分,我将其标记为表格,但是“提交”按钮不起作用。当我将所有内容保留在form标签中时,所有按钮都可以正常工作,但是在table和label标签中,它们将停止工作。

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
    <title>List of Links</title>
</head>
<body>
    <h1>List of Links</h1>
    <div>Add a new link: 
        <table action="/add" method="POST">
            <input type="text" name="link">
            <input type="submit">
        </table>
    </div>
    <div>
        <ul>
            {% for link in links %}
            <li style="font-size: 15pt">{{ link.text }}</li>
            <label action="/edit" method="POST">
            <input type="hidden" name = "old_text" value={{ link.text }}></label>
            <label><input type="text" name = "new_text" value="">
            <input type="submit" value="Edit">
            </label>
            <label action="/delete" method="POST">
            <input type="hidden" name = "text" value={{ link.text }}>
            <input type="submit" value="Delete">
            </label>
            {% endfor %}
        </ul>
    </div>
</body>
</html>

下面是上述html代码的python代码。

from flask import Flask, render_template, request, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
import time

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://///home/heisenberg98/Desktop/DMCA Flask/links.db'
db = SQLAlchemy(app)

class Link(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    text = db.Column(db.String(2083))
    complete = db.Column(db.Boolean)

db.create_all()

@app.route('/')
def index():
    links = Link.query.all()
    return render_template('index.html', links=links)

@app.route('/add', methods=['POST'])
def add():
    link = Link(text=request.form['link'], complete=False)
    db.session.add(link)
    db.session.commit()
    return redirect(url_for('index'))

@app.route('/delete', methods=['POST'])
def delete():
    tt=request.form['text']
    t_ = Link.query.filter_by(text=tt).first()
    db.session.delete(t_)
    db.session.commit()
    return redirect(url_for('index'))

@app.route('/edit', methods=['GET', 'POST'])
def edit():
    old_text=request.form['old_text']
    new_text=request.form['new_text']
    qry = Link.query.filter_by(text=old_text).first()
    qry.text = new_text
    db.session.commit()
    return redirect(url_for('index'))

if __name__ == '__main__':
    app.run("0.0.0.0",port=5001,debug=True)

0 个答案:

没有答案