Home > Net >  AssertionError: View function mapping is overwriting an existing endpoint function:
AssertionError: View function mapping is overwriting an existing endpoint function:

Time:10-31

I am creating an app with python and flask. I am getting the following error:

Traceback (most recent call last):
  File "C:\Users\Albert\PycharmProjects\Carro\views_trips.py", line 10, in <module>
    def index():
  File "C:\Users\Albert\PycharmProjects\Carro\venv\lib\site-packages\flask\scaffold.py", line 439, in decorator
    self.add_url_rule(rule, endpoint, f, **options)
  File "C:\Users\Albert\PycharmProjects\Carro\venv\lib\site-packages\flask\scaffold.py", line 57, in wrapper_func
    return f(self, *args, **kwargs)
  File "C:\Users\Albert\PycharmProjects\Carro\venv\lib\site-packages\flask\app.py", line 1090, in add_url_rule
    raise AssertionError(
AssertionError: View function mapping is overwriting an existing endpoint function: index

I have only one route.

from flask import render_template, request, redirect, session, flash, url_for, send_from_directory
from app import app
from models import Trips, Users, Cars, db
import time
from helpers import *

from flask_bcrypt import check_password_hash

@app.route('/')
def index():
    nickname = 'Bertimaz'
    trip = Trips.query.filter_by(user_nickname=nickname).order_by(Trips.initialTime.desc()).first()
    user = Users.query.filter_by(nickname='Bertimaz')  # não ta achando usuario
    print(user.name)
    car = Cars.query.filter_by(plate=trip.car_plate)

    return render_template('home.html', titulo='Viagens', trip=trip, user=user, car=car)

It was able to run it before I started implementing my SQL alchemy models and I tried changing the index function name

CodePudding user response:

Instead of running the file with the routes instead of the correct script below:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_wtf.csrf import CSRFProtect
from flask_bcrypt import Bcrypt

app = Flask(__name__)
app.config.from_pyfile('config.py')

db = SQLAlchemy(app)
   
from views_trips import *
from views_user import *

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

CodePudding user response:

In your init.py, import the Falsk app and assign the value to that app.

from flask import **app**, Flask
from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy

from app.config import Config

**app = Flask(__name__)
app.config.from_object(Config)**

# DB
db = SQLAlchemy(app)
migrate = Migrate(app, db)

After you have these above entries, you can import the app as

from app import app
  • Related