找不到页面 python 烧瓶
page not found python flask
我正在开始编程 python 并且我正在使用 flask 框架创建 Web 应用程序,但它显示找不到页面,404。
您好,我在访问删除("excluir") 驱动程序功能和返回列表("lista.html") 页面时遇到问题。每当我单击 table 中的 Delete("Excluir motorista") link 时,都会出现找不到错误页面。
这是一个app.py
#!/usr/local/bin/python
# -*- coding: UTF-8 -*-
#IMPORTAR FRAMEWORK FLASK
from flask import Flask, render_template, request, url_for, redirect
from flask_sqlalchemy import SQLAlchemy
#CONFIGURAÇÃO E CRIACAO DAS TABELAS DO BACO DE DADOS
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite3'
db = SQLAlchemy(app)
class MotoristaDB(db.Model):
__tablename__ = 'motorista'
_idMotorista = db.Column(db.Integer, autoincrement=True, primary_key=True)
nomeMotorista = db.Column(db.String)
dtNascMotorista = db.Column(db.String)
cpfMotorista = db.Column(db.Integer)
modCarro = db.Column(db.String)
status = db.Column(db.String)
sexoMotorista = db.Column(db.String)
def __init__ (self, nomeMotorista, dtNascMotorista, cpfMotorista, modCarro, status, sexoMotorista):
self.nomeMotorista = nomeMotorista
self.dtNascMotorista = dtNascMotorista
self.cpfMotorista = cpfMotorista
self.modCarro = modCarro
self.status = status
self.sexoMotorista = sexoMotorista
class PassageiroDB(db.Model):
__tablename__ = 'passageiro'
_idPassageiro = db.Column(db.Integer, autoincrement = True, primary_key = True)
nomePassageiro = db.Column(db.String)
dtNascPassageiro = db.Column(db.String)
cpfPassageiro = db.Column(db.Integer)
sexoPassageiro = db.Column(db.String)
def __init__(self, nomePassageiro, dtNascPassageiro, cpfPassageiro, sexoPassageiro):
self.nomePassageiro = nomePassageiro
self.dtNascPassageiro = dtNascPassageiro
self.cpfPassageiro = cpfPassageiro
self.sexoPassageiro = sexoPassageiro
db.create_all()
@app.route('/index')
def index():
return render_template('index.html')
##CADASTRAMENTO DE MOTORISTAS
@app.route('/cadastrarMotorista')
def cadastrarMotorista():
return render_template('cadastroMotorista.html')
@app.route('/cadastroMotorista', methods=['GET', 'POST'])
def cadastroMotorista():
if request.method == 'POST':
nomeMotorista = request.form.get('nomeMotorista')
dtNascMotorista = request.form.get('dtNascMotorista')
cpfMotorista = request.form.get('cpfMotorista')
modCarro = request.form.get('modCarro')
status = request.form.get('status')
sexoMotorista = request.form.get('sexoMotorista')
if nomeMotorista and dtNascMotorista and cpfMotorista and modCarro and (status == 'ativo' or status == 'inativo' or status == 'Ativo' or status == 'Inativo') and sexoMotorista:
motorista = MotoristaDB(nomeMotorista, dtNascMotorista, cpfMotorista, modCarro, status, sexoMotorista)
db.session.add(motorista)
db.session.commit()
return redirect(url_for('index'))
##CADASTRAMENTO DE PASSAGEIROS
@app.route('/cadastrarPassageiro')
def cadastrarPassageiro():
return render_template('cadastroPassageiro.html')
@app.route('/cadastroPassageiro', methods=['GET', 'POST'])
def cadastroPassageiro():
if request.method == 'POST':
nomePassageiro = request.form.get('nomePassageiro')
dtNascPassageiro = request.form.get('dtNascPassageiro')
cpfPassageiro = request.form.get('cpfPassageiro')
sexoPassageiro = request.form.get('sexoPassageiro')
if nomePassageiro and dtNascPassageiro and cpfPassageiro and sexoPassageiro:
passageiro = PassageiroDB(nomePassageiro, dtNascPassageiro, cpfPassageiro, sexoPassageiro)
db.session.add(passageiro)
db.session.commit()
return redirect(url_for('index'))
##LISTAGEM DE MOTORISTA, PASSAGEIROS E CORRIDAS FEITAS
@app.route('/lista')
def lista():
motoristas = MotoristaDB.query.all()
passageiros = PassageiroDB.query.all()
return render_template('lista.html', motoristas = motoristas, passageiros = passageiros)
##ATUALIUÇÃO DO STATUS DO MOTORISTA
@app.route('/excluir/<int:idMotorista>')
def excluir(idMotorista):
motorista = MotoristaDB.query.filter_by(_idMotorista=idMotorista).first()
db.session.delete(motorista)
db.session.commit()
motoristas = MotoristaDB.query.all();
return render_template('lista.html', motoristas = motoristas)
if __name__ == '__main__':
app.run(debug=True)
并且是 lista.html
<!DOCTYPE html>
<html>
<head>
<title>Corridas Compartilhada</title>
</head>
<body>
<h1>Corridas Compartilhadas</h1>
<hr/>
<table border="1">
<tr>
<td>Nome</td>
<td>Data de Nascimento</td>
<td>CPF</td>
<td>Modelo do Carro</td>
<td>Status</td>
<td>Sexo</td>
<td>Excluir</td>
</tr>
{% for motorista in motoristas %}
<tr>
<td>{{ motorista.nomeMotorista }}</td>
<td>{{ motorista.dtNascMotorista }}</td>
<td>{{ motorista.cpfMotorista }}</td>
<td>{{ motorista.modCarro }}</td>
<td>{{ motorista.status }}</td>
<td>{{ motorista.sexoMotorista }}</td>
<td><a href="/excluir/{{ motorista._id }}">Excluir Motorista</a></td>
</tr>
{% endfor %}
</table>
<table border="1">
<tr>
<td>Nome</td>
<td>Data de Nascimento</td>
<td>CPF</td>
<td>Sexo</td>
</tr>
{% for passageiro in passageiros %}
<tr>
<td>{{ passageiro.nomePassageiro }}</td>
<td>{{ passageiro.dtNascPassageiro }}</td>
<td>{{ passageiro.cpfPassageiro }}</td>
<td>{{ passageiro.sexoPassageiro }}</td>
</tr>
{% endfor %}
</table>
</body>
</html>
问题是访问 delete("excluir") 函数。
OBS:我是巴西人,抱歉英文有误嘿嘿:)
我认为对你来说简单的解决方案是使用 redirect
from flask import url_for, redirect
@app.route('/excluir/<int:idMotorista>')
def excluir(idMotorista):
motorista = MotoristaDB.query.filter_by(_idMotorista=idMotorista).first()
db.session.delete(motorista)
db.session.commit()
return redirect(url_for('lista'))
我正在开始编程 python 并且我正在使用 flask 框架创建 Web 应用程序,但它显示找不到页面,404。
您好,我在访问删除("excluir") 驱动程序功能和返回列表("lista.html") 页面时遇到问题。每当我单击 table 中的 Delete("Excluir motorista") link 时,都会出现找不到错误页面。
这是一个app.py
#!/usr/local/bin/python
# -*- coding: UTF-8 -*-
#IMPORTAR FRAMEWORK FLASK
from flask import Flask, render_template, request, url_for, redirect
from flask_sqlalchemy import SQLAlchemy
#CONFIGURAÇÃO E CRIACAO DAS TABELAS DO BACO DE DADOS
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite3'
db = SQLAlchemy(app)
class MotoristaDB(db.Model):
__tablename__ = 'motorista'
_idMotorista = db.Column(db.Integer, autoincrement=True, primary_key=True)
nomeMotorista = db.Column(db.String)
dtNascMotorista = db.Column(db.String)
cpfMotorista = db.Column(db.Integer)
modCarro = db.Column(db.String)
status = db.Column(db.String)
sexoMotorista = db.Column(db.String)
def __init__ (self, nomeMotorista, dtNascMotorista, cpfMotorista, modCarro, status, sexoMotorista):
self.nomeMotorista = nomeMotorista
self.dtNascMotorista = dtNascMotorista
self.cpfMotorista = cpfMotorista
self.modCarro = modCarro
self.status = status
self.sexoMotorista = sexoMotorista
class PassageiroDB(db.Model):
__tablename__ = 'passageiro'
_idPassageiro = db.Column(db.Integer, autoincrement = True, primary_key = True)
nomePassageiro = db.Column(db.String)
dtNascPassageiro = db.Column(db.String)
cpfPassageiro = db.Column(db.Integer)
sexoPassageiro = db.Column(db.String)
def __init__(self, nomePassageiro, dtNascPassageiro, cpfPassageiro, sexoPassageiro):
self.nomePassageiro = nomePassageiro
self.dtNascPassageiro = dtNascPassageiro
self.cpfPassageiro = cpfPassageiro
self.sexoPassageiro = sexoPassageiro
db.create_all()
@app.route('/index')
def index():
return render_template('index.html')
##CADASTRAMENTO DE MOTORISTAS
@app.route('/cadastrarMotorista')
def cadastrarMotorista():
return render_template('cadastroMotorista.html')
@app.route('/cadastroMotorista', methods=['GET', 'POST'])
def cadastroMotorista():
if request.method == 'POST':
nomeMotorista = request.form.get('nomeMotorista')
dtNascMotorista = request.form.get('dtNascMotorista')
cpfMotorista = request.form.get('cpfMotorista')
modCarro = request.form.get('modCarro')
status = request.form.get('status')
sexoMotorista = request.form.get('sexoMotorista')
if nomeMotorista and dtNascMotorista and cpfMotorista and modCarro and (status == 'ativo' or status == 'inativo' or status == 'Ativo' or status == 'Inativo') and sexoMotorista:
motorista = MotoristaDB(nomeMotorista, dtNascMotorista, cpfMotorista, modCarro, status, sexoMotorista)
db.session.add(motorista)
db.session.commit()
return redirect(url_for('index'))
##CADASTRAMENTO DE PASSAGEIROS
@app.route('/cadastrarPassageiro')
def cadastrarPassageiro():
return render_template('cadastroPassageiro.html')
@app.route('/cadastroPassageiro', methods=['GET', 'POST'])
def cadastroPassageiro():
if request.method == 'POST':
nomePassageiro = request.form.get('nomePassageiro')
dtNascPassageiro = request.form.get('dtNascPassageiro')
cpfPassageiro = request.form.get('cpfPassageiro')
sexoPassageiro = request.form.get('sexoPassageiro')
if nomePassageiro and dtNascPassageiro and cpfPassageiro and sexoPassageiro:
passageiro = PassageiroDB(nomePassageiro, dtNascPassageiro, cpfPassageiro, sexoPassageiro)
db.session.add(passageiro)
db.session.commit()
return redirect(url_for('index'))
##LISTAGEM DE MOTORISTA, PASSAGEIROS E CORRIDAS FEITAS
@app.route('/lista')
def lista():
motoristas = MotoristaDB.query.all()
passageiros = PassageiroDB.query.all()
return render_template('lista.html', motoristas = motoristas, passageiros = passageiros)
##ATUALIUÇÃO DO STATUS DO MOTORISTA
@app.route('/excluir/<int:idMotorista>')
def excluir(idMotorista):
motorista = MotoristaDB.query.filter_by(_idMotorista=idMotorista).first()
db.session.delete(motorista)
db.session.commit()
motoristas = MotoristaDB.query.all();
return render_template('lista.html', motoristas = motoristas)
if __name__ == '__main__':
app.run(debug=True)
并且是 lista.html
<!DOCTYPE html>
<html>
<head>
<title>Corridas Compartilhada</title>
</head>
<body>
<h1>Corridas Compartilhadas</h1>
<hr/>
<table border="1">
<tr>
<td>Nome</td>
<td>Data de Nascimento</td>
<td>CPF</td>
<td>Modelo do Carro</td>
<td>Status</td>
<td>Sexo</td>
<td>Excluir</td>
</tr>
{% for motorista in motoristas %}
<tr>
<td>{{ motorista.nomeMotorista }}</td>
<td>{{ motorista.dtNascMotorista }}</td>
<td>{{ motorista.cpfMotorista }}</td>
<td>{{ motorista.modCarro }}</td>
<td>{{ motorista.status }}</td>
<td>{{ motorista.sexoMotorista }}</td>
<td><a href="/excluir/{{ motorista._id }}">Excluir Motorista</a></td>
</tr>
{% endfor %}
</table>
<table border="1">
<tr>
<td>Nome</td>
<td>Data de Nascimento</td>
<td>CPF</td>
<td>Sexo</td>
</tr>
{% for passageiro in passageiros %}
<tr>
<td>{{ passageiro.nomePassageiro }}</td>
<td>{{ passageiro.dtNascPassageiro }}</td>
<td>{{ passageiro.cpfPassageiro }}</td>
<td>{{ passageiro.sexoPassageiro }}</td>
</tr>
{% endfor %}
</table>
</body>
</html>
问题是访问 delete("excluir") 函数。
OBS:我是巴西人,抱歉英文有误嘿嘿:)
我认为对你来说简单的解决方案是使用 redirect
from flask import url_for, redirect
@app.route('/excluir/<int:idMotorista>')
def excluir(idMotorista):
motorista = MotoristaDB.query.filter_by(_idMotorista=idMotorista).first()
db.session.delete(motorista)
db.session.commit()
return redirect(url_for('lista'))