为什么没有特定帖子的特定网址?

时间:2019-05-17 17:42:23

标签: python flask

例如,帖子的网址是/ post / username / id / post-about-about 但是,当我从用户名中删除字母或大约时,或者将ID更改为现有ID时,帖子保持不变:(

  • / post / username / id / post-about-about
  • / post / userna /(另一个现有ID)/ post-about-abo
  • / post / use /(另一个现有ID)/ post-about-abou

------->都一样

我正在使用flask和mysql

@app.route("/post/<string:username>/<string:id>/post-about-<string:about>", methods=['GET', 'POST'])
def post(id, about, username):
    form = commentform(request.form)
    cur = mysql.connection.cursor()
    r = cur.execute("SELECT * FROM posts WHERE ID=%s", [id])
    d = cur.execute("SELECT * FROM posts WHERE about=%s", [about])
    k = cur.execute("SELECT * FROM posts WHERE username=%s", [username])
    post = cur.fetchone()

    if r==0 and k==0 and d==0:
        flash("no post found with that id")
        return redirect(url_for('index'))
    if 'logged_in' in session :
        if (request.method == 'POST' and form.validate()):
            comment = form.comment.data
            cur = mysql.connection.cursor()
            cur.execute("INSERT INTO comments (text, username, post_id) VALUES (%s, %s, %s)", (comment, session['username'], id))
            mysql.connection.commit()
            cur.close()
            flash("Comment successful", 'success')
            return redirect(url_for('index'))
    cur.execute("SELECT * FROM comments WHERE post_id=%s", [id])
    commentaria = cur.fetchall()
    cur.close()

    return render_template('post.html' , post = post, form=form, commentaria=commentaria)

1 个答案:

答案 0 :(得分:0)

从URL的结构来看,我认为您想使用这种查询:

k = cur.execute("SELECT * FROM posts WHERE username=%s AND ID = %s AND about = %s", (username, id, about))
post = cur.fetchone()