[Python] RuntimeError: The current Flask app is not registered with this 'SQLAlchemy'...

Discussão em 'Python' iniciado por Stack, Março 19, 2025.

  1. Stack

    Stack Membro Participativo

    ERROR : RuntimeError: The current Flask app is not registered with this 'SQLAlchemy' instance. Did you forget to call 'init_app', or did you create multiple 'SQLAlchemy' instances?

    i'll be honest, i have no idea what im doing. Heres what i have

    base.html


    <!DOCTYPE html>
    <html lang="es">
    <head>
    <title>Superhéroes</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
    </head>
    <body class="container mt-4">
    {% block content %}{% endblock %}
    </body>
    </html>

    index.html

    {% extends "base.html" %}
    {% block content %}
    <h1>Lista de Superhéroes</h1>
    <a href="{{ url_for('add') }}" class="btn btn-success mb-3">Agregar Superhéroe</a>
    <table class="table">
    <tr>
    <th>Alias</th><th>Nombre Real</th><th>Acciones</th>
    </tr>
    {% for superheroe in superheroes %}
    <tr>
    <td>{{ superheroe.alias }}</td>
    <td>{{ superheroe.nombre_real }}</td>
    <td>
    <a href="{{ url_for('edit', id=superheroe.id) }}" class="btn btn-warning">Editar</a>
    <a href="{{ url_for('delete', id=superheroe.id) }}" class="btn btn-danger">Eliminar</a>
    </td>
    </tr>
    {% endfor %}
    </table>
    {% endblock %}


    add.html

    {% extends "base.html" %}
    {% block content %}
    <h1>Registrar Superhéroe</h1>
    <form method="post">
    {{ form.hidden_tag() }}
    <p>{{ form.nombre_real.label }} {{ form.nombre_real() }}</p>
    <p>{{ form.alias.label }} {{ form.alias() }}</p>
    <p>{{ form.foto.label }} {{ form.foto() }}</p>
    <p>{{ form.info.label }} {{ form.info() }}</p>
    <p>{{ form.submit() }}</p>
    </form>
    {% endblock %}


    edit.html

    {% extends "base.html" %}
    {% block content %}
    <h1>Editar Superhéroe</h1>
    <form method="post">
    {{ form.hidden_tag() }}
    <p>{{ form.nombre_real.label }} {{ form.nombre_real() }}</p>
    <p>{{ form.alias.label }} {{ form.alias() }}</p>
    <p>{{ form.foto.label }} {{ form.foto() }}</p>
    <p>{{ form.info.label }} {{ form.info() }}</p>
    <p>{{ form.submit() }}</p>
    </form>
    {% endblock %}


    app.py

    from flask import Flask, render_template, redirect, url_for, request
    from flask_sqlalchemy import SQLAlchemy
    from flask_migrate import Migrate

    app = Flask(__name__)
    app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
    app.config['SECRET_KEY'] = 'supersecretkey'

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

    from models import Superheroe

    @app.route('/')
    def index():
    superheroes = Superheroe.query.all()
    return render_template('index.html', superheroes=superheroes)

    if __name__ == '__main__':
    with app.app_context():
    db.create_all()
    app.run(debug=True)


    models.py

    from flask_sqlalchemy import SQLAlchemy

    db = SQLAlchemy()

    class Superheroe(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    nombre_real = db.Column(db.String(100), nullable=False)
    alias = db.Column(db.String(100), nullable=False)
    foto = db.Column(db.String(255), nullable=False)
    info = db.Column(db.Text, nullable=False)


    forms.py

    from flask_wtf import FlaskForm
    from wtforms import StringField, TextAreaField, SubmitField
    from wtforms.validators import DataRequired, URL

    class SuperheroeForm(FlaskForm):
    nombre_real = StringField('Nombre Real', validators=[DataRequired()])
    alias = StringField('Alias', validators=[DataRequired()])
    foto = StringField('URL de la Foto', validators=[URL()])
    info = TextAreaField('Información Adicional')
    submit = SubmitField('Guardar')


    I'm so sorry for posting the whole thing, i would really appreciate help

    Continue reading...

Compartilhe esta Página