Forums

Redirecting to the root /

Hi! When doing a POST request in the code below I thought it would redirect to the url: username.pythonanywhere.com/dbtest ....but i won't . . I thought url_for was expecting the funcion name (dbtesting) and it would generate the following url: username.pythonanywhere.com/dbtest .... ...I must be doing somthong wrong because the code redirects to the root.... username.pythonanywhere.com/ Any help much appreciated! All the best! /Erik

@app.route("/dbtest", methods=["GET", "POST"])
def dbtesting():
    if request.method == "GET":
        return render_template("main_page.html", comments=Comment.query.all())

    comment = Comment(content=request.form["contents"])
    db.session.add(comment)
    db.session.commit()
    return redirect(url_for('dbtesting'))

Fixed it! Nothing wrong with the code above... I had simply forgotten to set the form action="/dbtest" in the HTML..... my Bad :) Thanks anyway! All the best/EE