2017-09-15 122 views
0

我开始计划Python和我使用的烧瓶框架来创建Web应用程序,但它给找不到网页,404找不到网页蟒蛇烧瓶

您好,我有访问问题删除(“excluir”)驱动程序功能并返回列表(“lista.html”)页面。每当我点击表格中的删除(“Excluir motorista”)链接,就会发现找不到错误页面。

这是一个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> 

的问题是访问删除( “excluir”)功能。

OBS:我是巴西人,对不起,在英文错误嘿嘿:)

回答

1

我想简单的为您解决情况下,使用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')) 
+2

我知道这是不是问题的一部分,但验证'id'和'motorista'实际存在总是个好主意,否则你会在'db.session.delete(motorista)'上产生一个错误。 –