按下提交时,Flask不会返回请求的页面

时间:2018-03-28 11:50:00

标签: python flask

我是新手,我正在为我的办公室创建一个小表单,它工作非常简单,它有一个登录按钮,当我输入id和密码并按提交时,它返回内部服务器错误,当我刷新页面再次它返回我想要的页面。 我还收到一个错误,即代码{{render_field(form.select)}}上的形式未定义任何人都可以建议做什么

以下是代码

from flask import Flask, render_template, request
from flask.ext.sqlalchemy import SQLAlchemy
from flask_sqlalchemy import SQLAlchemy
from db_setup import init_db, db_session
from forms import DataSearchForm
from flask import flash, render_template, request, redirect
from models import *

app = Flask(__name__)

app.config["SQLALCHEMY_DATABASE_URI"]="postgresql://postgres:****@**@localhost/****"
app.secret_key = "flask rocks!"
db = SQLAlchemy(app)


init_db()
class Data(db.Model):
    __tablename__="data"
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(120))
    password = db.Column(db.Integer)

    def __init__(self, email, password):
        self.email = email
        self.password = password

@app.route("/")
def index():
    return render_template("index.html")

@app.route("/rera", methods=["POST"])
def rera():
    if request.method=="POST":
        email = request.form["email_name"]
        password = request.form["password_name"]
        print(email, password)
        data = Data(email, password)
        db.session.add(data)
        db.session.commit()
        return render_template("rera.html")

@app.route('/rera', methods=['GET', 'POST'])
def rera2():
    search = DataSearchForm(request.form)
    if request.method == 'POST':
        return search_results(search)

    return render_template('rera.html', form=search)

@app.route('/results')
def search_results(search):
    results = []
    search_string = search.data['search']

    if search.data['search'] == '':
        qry = db_session.query(OTRS)
        results = qry.all()

    if not results:
        flash('No results found!')
        return redirect('/')
    else:
        # display results
        return render_template('results.html', results=results)


if __name__ == "__main__":
    app.debug:True
    app.run()

这些是我的HTML代码

<!DOCTYPE html>
<html lang="en">
<title> Login Form</title>
<head>
  <link href="../static/main.css" rel="stylesheet">
</head>
<body>
  <div class="container">
    <h1> Productivity Form and Rera Details</h1>
    <h2> Please Login</h2>
    <form action="{{ url_for('rera') }}" method="POST">
      <input title="Your email will be safe with us" placeholder="Enter Your email" type="email" name="email_name" pattern="[prince.bhatia]{0,100}+@[99acres]+\.[com]{2,3}$" required><br>
      <input title="Your password will be safe with us" placeholder="Enter Your password" type="password" name="password_name"required><br>
      <button type="submit"> Submit </button>
    </form>
  </div>
</body>
</html>

以下是rera.html(请求的HTML)

<!DOCTYPE html>
<html lang="en">
<title> Login Form</title>
<head>
  <link href="../static/main.css" rel="stylesheet">
</head>
<body>
  <div class="container">
    <h2>Rera Database</h2>
    {% with messages = get_flashed_messages() %}
  {% if messages %}
    <ul class=flashes>
    {% for message in messages %}
      <li>{{ message }}</li>
    {% endfor %}
    </ul>
  {% endif %}
{% endwith %}

{% from "_formhelpers.html" import render_field %}
<form method="POST">
  <dl>
    {{ render_field(form.select) }}
    <p>
    {{ render_field(form.search) }}
  </dl>
  <p><input type=submit value=Search>
</form>
  </div>
</body>


</html>

2 个答案:

答案 0 :(得分:1)

看看你的代码:

@app.route("/rera", methods=["POST"])
def rera():
    if request.method=="POST":
        email = request.form["email_name"]
        password = request.form["password_name"]
        print(email, password)
        data = Data(email, password)
        db.session.add(data)
        db.session.commit()
        return render_template("rera.html")

@app.route('/rera', methods=['GET', 'POST'])
def rera2():
    search = DataSearchForm(request.form)
    if request.method == 'POST':
        return search_results(search)

    return render_template('rera.html', form=search)

将两个函数绑定到同一路由(/rera)。

如何烧瓶&#34;猜测&#34;你打算执行哪一个?嗯,它不可能。 我目前无法验证这一点,但我的猜测是它将始终执行后者(即rera2)。

答案 1 :(得分:0)

可以修改rera,如下所示,删除rera2:

@app.route('/rera', methods=['GET', 'POST'])
def rera2():
    search = DataSearchForm(request.form)
    email = request.form["email_name"]
    password = request.form["password_name"]
    if request.method == 'POST':
        if search:
            return search_results(search)
        elif email and password:
            data = Data(email, password)
            db.session.add(data)
            db.session.commit()
            return render_template("rera.html")
        else:
            return render_template('rera.html', form=search)