Python-Flask SQLAlchemy应用程序未运行-“;这个网站可以’;“联系不到”;
所以我试着去学习,炼金术。 我从教程中复制了代码 它有一个缩进错误,所以我修正了它。此外,我添加了Python-Flask SQLAlchemy应用程序未运行-“;这个网站可以’;“联系不到”;,python,flask,sqlalchemy,flask-sqlalchemy,Python,Flask,Sqlalchemy,Flask Sqlalchemy,所以我试着去学习,炼金术。 我从教程中复制了代码 它有一个缩进错误,所以我修正了它。此外,我添加了run(use_reloader=False),因为它没有加载。但现在它显示: This site can’t be reached The webpage at http://127.0.0.1:6000/ might be temporarily down or it may have moved permanently to a new web address. ERR_UNSAFE_
run(use_reloader=False)
,因为它没有加载。但现在它显示:
This site can’t be reached The webpage at http://127.0.0.1:6000/ might be temporarily down or it may have moved permanently to a new web address.
ERR_UNSAFE_PORT
在浏览器中
完整的代码是
from flask import Flask, request, flash, url_for, redirect, render_template
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///students.sqlite3'
app.config['SECRET_KEY'] = "random string"
db = SQLAlchemy(app)
class students(db.Model):
id = db.Column('student_id', db.Integer, primary_key = True)
name = db.Column(db.String(100))
city = db.Column(db.String(50))
addr = db.Column(db.String(200))
pin = db.Column(db.String(10))
def __init__(self, name, city, addr,pin):
self.name = name
self.city = city
self.addr = addr
self.pin = pin
@app.route('/')
def show_all():
return render_template('show_all.html', students = students.query.all() )
@app.route('/new', methods = ['GET', 'POST'])
def new():
if request.method == 'POST':
if not request.form['name'] or not request.form['city'] or not request.form['addr']:
flash('Please enter all the fields', 'error')
else:
student = students(request.form['name'], request.form['city'],
request.form['addr'], request.form['pin'])
db.session.add(student)
db.session.commit()
flash('Record was successfully added')
return redirect(url_for('show_all'))
return render_template('new.html')
if __name__ == '__main__':
db.create_all()
app.run(port=6000, use_reloader=False, debug = True)
尝试访问
http://localhost:5000/
因为这是默认的端口flask在@BilalAliJafri上:在没有解释安全含义的情况下,不要建议使用0.0.0.0!他们正在连接到默认主机localhost,并且他们的Flask服务器不需要对其网络的其他部分可见。@BilalAliJafri:如果有人在公共网络上这样做,他们会面临可能的安全问题,包括让他们的机器被黑客攻击。Werkzeug WSGI服务器不是经过战斗考验的,在调试模式下运行时,有可能绕过简单的PIN保护,允许在您的机器上执行任意Pythno代码。因此,我删除了您的评论。请不要要求人们再次向这样的风险敞开心扉。运行Flask开发服务器时,请将其保持在本地。@MartijnPieters注释中,使用wsgiref
not Flask等。在未学习WSGI应用程序的情况下,不要使用任何应用程序,如Flask或dijango。@dsgdfg:app.run()
的全部目的是为您提供一个本地服务器进行开发。那很好。他们只从自己的机器上访问此端口。app.run(port=6000,…)
设置端口,它不再在默认端口上运行。