I need help to fix my code running.

I need help to fix my code running.. I am writing the databases code to let the user enter City Name, City Rank in the box just like Username and Password.
But I’m getting this message when I run the code.
sqlalchemy.exc.OperationalError
“sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: cities [SQL: SELECT cities.id AS cities_id, cities.city_name AS cities_city_name, cities.city_rank AS cities_city_rank FROM cities]”
__init__.py
import flask
import os
from flask_sqlalchemy import SQLAlchemy
basedir = os.path.abspath(os.path.dirname(__file__))
#instance of the flask class
app_obj = flask.Flask(__name__)
app_obj.config.from_mapping(
SECRET_KEY = ‘it-dont-matter’,
SQLALCHEMY_DATABASE_URI = ‘sqlite:///’ os.path.join(basedir, ‘app.db’),
SQLALCHEMY_TRACK_MODIFICATIONS = False
)
db = SQLAlchemy(app_obj)
from app import routes, models
forms.py
from flask_wtf import FlaskForm
from wtforms import StringField, IntegerField, BooleanField, SubmitField
from wtforms.validators import DataRequired
class Cities(FlaskForm):
city_name = StringField(‘City Name’,validators=[DataRequired()])
city_rank = IntegerField(‘City Rank’,validators=[DataRequired()])
is_visited = BooleanField(‘Visited’)
submit = SubmitField(‘Submit’)
models.py
from app import db
from datetime import datetime
class Cities(db.Model):
#create column for database
#primary_key means it is unique
id = db.Column(db.Integer, primary_key=True)
city_name= db.Column(db.String(64), unique=True, index=True)
city_rank = db.Column(db.Integer())
posts = db.relationship(‘Post’, backref=’author’, lazy=’dynamic’)
def __repr__(self):
return f'{self.city_name} with rank {self.city_rank}’
class Post(db.Model):
#create column for database
#primary_key means it is unique
id = db.Column(db.Integer, primary_key=True)
body = db.Column(db.String(256))
timestamp = db.Column(db.DateTime, default=datetime.utcnow)
user_id = db.Column(db.Integer, db.ForeignKey(‘cities.id’))
def __repr__(self):
return f’ {self.id}: {self.body}>’
routes.py
from app import app_obj
from app.forms import Cities
from flask import render_template, flash, redirect, request
from app import db
from app.models import Cities, Post
@app_obj.route(“/login”, methods = [‘GET’,’POST’])
def login():
name = ‘Khuong’
title = ‘Top Cities’
top_cities = Cities.query.all()
form = Cities()
if request.method == ‘POST’:
if not request.form [‘city_name’] or not request.form [‘city_rank’]:
flask(‘Invalid city_name or city_rank’)
else:
top_cities = Cities(city_name = form.city_name.data, city_rank = form.city_rank.data)
db.sesion.add(top_cities)
db.session.commit()
flask(‘New city is added!’)
return redirect(‘/’)
return render_template(“home.html”, name=name, title=title, form=form)
run.py
#from library import app
from app import app_obj
app_obj.run(debug=True)
base.html

{% if title %}
{{title}}!
{% else %}
Home
{% endif %}

{% with messages = get_flashed_messages() %}
{% if messages %}

    {% for m in message %}

  • {{ m }}
  • {% endfor %}

{% endif %}
{% endwith %}
{% block content %}
{% endblock %}

home.html

{% if title %}
{{title}}!
{% else %}
Home
{% endif %}

{% with messages = get_flashed_messages() %}
{% if messages %}

    {% for m in message %}

  • {{ m }}
  • {% endfor %}

{% endif %}
{% endwith %}
{% block content %}
{% endblock %}

I need help to fix my code running.

This question has been answered by our writers. You can buy the answer below or order your 0% plagiarized answer