您好我对flask和python是新手,我尝试重定向我的脚本转到something.html,因为它是一个404我决定运行一个hello world并且没有工作。
未找到
在服务器上找不到请求的URL。如果您手动输入了URL,请检查拼写,然后重试。
from flask import Flask
from flask import request
from flask import url_for
from flask import redirect
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.debug = True
app.config['SQLALCHEMY_DATABASE_URI']= 'postgre://postgres:*****@localhost/web'
db = SQLAlchemy(app)
class Rent(db.Model):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(200) , unique=True)
sug1 = db.Column(db.String(200) , unique =True)
sug2 = db.Column(db.String(200) , unique = True)
sug3 = db.Column(db.String(200), unique = True)
def __init__ (self,email,sug1,sug2,sug3):
self.email = email
self.sug1 = sug1
self.sug2 = sug2
self.sug3 = sug3
def __repr__(self):
return '<User %>' % self.email
@app.route('/hello')
def index():
return "Hello"
@app.route('/resumo', methods=['POST'])
def pesquisa():
rent = Rent(request.form['email'],request.form['sug1'],request.form['sug2'],request.form['sug3'])
db.session.add(rent)
db.session.commit()
return redirect(url_for('index'))
if __name__ == "__main__":
app.run(port=5500)
答案 0 :(得分:0)
如果您的代码类似于显示代码缩进问题的代码,则代码应如下所示。
from flask import Flask
from flask import request
from flask import url_for
from flask import redirect
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.debug = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgre://postgres:*****@localhost/web'
db = SQLAlchemy(app)
class Rent(db.Model):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(200) , unique=True)
sug1 = db.Column(db.String(200) , unique =True)
sug2 = db.Column(db.String(200) , unique = True)
sug3 = db.Column(db.String(200), unique = True)
def __init__ (self,email,sug1,sug2,sug3):
self.email = email
self.sug1 = sug1
self.sug2 = sug2
self.sug3 = sug3
def __repr__(self):
return '<User %>' % self.email
@app.route('/hello')
def index():
return "Hello"
@app.route('/resumo', methods=['POST'])
def pesquisa():
rent = Rent(request.form['email'],request.form['sug1'],request.form['sug2'],request.form['sug3'])
db.session.add(rent)
db.session.commit()
return redirect(url_for('index'))
if __name__ == "__main__":
app.run(port=5500)