Skip to content

Instantly share code, notes, and snippets.

@TheMuellenator
Forked from angelabauer/main.py
Last active November 20, 2024 16:16
Show Gist options
  • Save TheMuellenator/b4e21f77c04afcac85a7ec2a262ea0d7 to your computer and use it in GitHub Desktop.
Save TheMuellenator/b4e21f77c04afcac85a7ec2a262ea0d7 to your computer and use it in GitHub Desktop.
Day 69 L532 Challenge 1 - Creating Relational Databases (SQL Alchemy 3.1)
## MORE CODE ABOVE
class User(UserMixin, db.Model):
__tablename__ = "users"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
email: Mapped[str] = mapped_column(String(100), unique=True)
password: Mapped[str] = mapped_column(String(100))
name: Mapped[str] = mapped_column(String(100))
#This will act like a List of BlogPost objects attached to each User.
#The "author" refers to the author property in the BlogPost class.
posts = relationship("BlogPost", back_populates="author")
class BlogPost(db.Model):
__tablename__ = "blog_posts"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
# Create Foreign Key, "users.id" the users refers to the tablename of User.
author_id: Mapped[int] = mapped_column(Integer, db.ForeignKey("users.id"))
# Create reference to the User object. The "posts" refers to the posts property in the User class.
author = relationship("User", back_populates="posts")
title: Mapped[str] = mapped_column(String(250), unique=True, nullable=False)
subtitle: Mapped[str] = mapped_column(String(250), nullable=False)
date: Mapped[str] = mapped_column(String(250), nullable=False)
body: Mapped[str] = mapped_column(Text, nullable=False)
img_url: Mapped[str] = mapped_column(String(250), nullable=False)
## MORE CODE BELOW
@by-Soulhunt
Copy link

Ok, got it to work.

First comment out the login manager code. We don't have any users in this new database that's why we are getting 404 status code.

# login_manager = LoginManager()
# login_manager.init_app(app)
# @login_manager.user_loader
# def load_user(user_id):
#     return db.get_or_404(Users, user_id)

Next we have to find all the "current_user" code portions in our templates, mostly in index and header html files and comment those out too. e.g.

{# {% if current_user.id == 1 %} #}
      <div class="d-flex justify-content-end mb-4">
        <a
          class="btn btn-primary float-right"
          href="{{url_for('add_new_post')}}"
          >Create New Post</a
        >
      </div>
{# {% endif %} #}

After that create your admin user like before and then uncomment the code :)

Thx, it works. It takes a lot of time to comment all current_user and other jinja variables, but it done.

@depena89
Copy link

image After days of Troubleshooting this one worked for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment