Beginner programmer here. Trying to create a flask app which users can post into. I am looking to create a "home page" where users can see their posts rather than every users posts.
Here is my code:
routes.py
#app.route("/home")
def home():
posts = Post.query.all()
return render_template('home.hmtl', posts=posts)
models.py
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(20), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
image_file = db.Column(db.String(20), nullable=False, default='default.jpg')
password = db.Column(db.String(60), nullable=False)
posts = db.relationship('Post', backref='author', lazy=True)
def __repr__(self):
return f"User('{self.username}', '{self.email}', '{self.image_file}')"
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100), nullable=False)
date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
content = db.Column(db.Text, nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
def __repr__(self):
return f"Post('{self.title}', '{self.date_posted}')"
Any help would be greatly appreciated!
First of all get, get the current user using session or jwt's.
Then you can now query your database to get posts by the current user only.
You can use current_user case
#app.route("/home", methods=['GET', 'POST'])
def home():
posts = current_user.posts
return render_template('home.html', posts=posts)
Related
I'm trying to set up a one-to-many relation between two tables (User and BlogPost, respectively).
I tried copying what the course(?) solution for this and still get foreign-key errors. What do I have wrong? Or, is this actually an issue with sqlalchemy (v. 1.3.19)?
I've even tried upgrading to the current version (1.4.31) with no success. I was getting run-time errors (flask page error reports) usually around the posts = BlogPost.query.all() line in home(). Now that I've upgraded the package, I'm getting a build-time error (AttributeError: can't set attribute) stemming from the db.create_all() line.
I've also tried deleting the db, again with no success.
Please leave a comment if this was answered somewhere else or if more info is needed.
Here's a snippet from Main.py:
db = SQLAlchemy(app)
class User(db.Model, UserMixin):
__tablename__ = "users"
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(250), nullable=False)
password = db.Column(db.String(250), nullable=False)
name = db.Column(db.String(100), nullable=False)
posts = relationship("BlogPost", back_populates="author")
class BlogPost(db.Model):
__tablename__ = "blog_posts"
id = db.Column(db.Integer, primary_key=True)
author_id = db.Column(db.Integer, db.ForeignKey("users.id"))
title = db.Column(db.String(250), unique=True, nullable=False)
subtitle = db.Column(db.String(250), nullable=False)
date = db.Column(db.String(250), nullable=False)
body = db.Column(db.Text, nullable=False)
img_url = db.Column(db.String(250), nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey("users.id"))
author = relationship("User", back_populates="posts")
db.create_all()
#app.route('/')
def home():
posts = BlogPost.query.all()
user = current_user
return render_template("index.html", all_posts=posts, user=user)
I still don't know what's wrong, but the following code works in a newer project. Like I said in one of the comments: I suspect that there's an issue with my current PyCharm project.
db = SQLAlchemy(app)
class User(UserMixin, db.Model):
__tablename__ = "users"
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(100), unique=True)
password = db.Column(db.String(100))
name = db.Column(db.String(100))
posts = relationship("BlogPost", back_populates="author")
class BlogPost(db.Model):
__tablename__ = "blog_posts"
id = db.Column(db.Integer, primary_key=True)
author_id = db.Column(db.Integer, db.ForeignKey("users.id"))
author = relationship("User", back_populates="posts")
title = db.Column(db.String(250), unique=True, nullable=False)
subtitle = db.Column(db.String(250), nullable=False)
date = db.Column(db.String(250), nullable=False)
body = db.Column(db.Text, nullable=False)
img_url = db.Column(db.String(250), nullable=False)
db.create_all()
#app.route('/')
def home():
posts = BlogPost.query.all()
return render_template("index.html", all_posts=posts)
Hello i would like to know how to get rid of this error. Here are my my tables
#login_menager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(20), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
password = db.Column(db.String(60),nullable=False)
posts = db.relationship('Post', backref='author', lazy=True)
user_comments = db.relationship('Comment', backref='author_comment', lazy=True)
def __repr__(self):
return f"User('{self.username}', '{self.email}')"
class Comment(db.Model):
id = db.Column(db.Integer, primary_key = True)
body = db.Column(db.Text)
timestamp = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
#post_id = db.Column(db.Integer, db.ForeignKey('post.id'), nullable=False)
comment_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
def __repr__(self):
return f"Comment('{self.body}')"
When i add these lines:
if comment_form.validate_on_submit():
reply = Comment(body=comment_form.comment_on_form.data, author_comment=current_user)
db.session.add(reply)
db.session.commit()
replies = Comment.query.filter_by(author_comment=current_user).order_by(Comment.timestamp.desc()).all()
return render_template('home.html', posts=posts, title='Home', form=form, comment_form=comment_form, replies=replies)
I get this error:
sqlalchemy.exc.ArgumentError: Mapped instance expected
for relationship comparison to object.
Classes, queries and other SQL elements are not accepted in this context;
for comparison with a subquery, use Comment.author_comment.has(**criteria).
With trackback:
https://raw.githubusercontent.com/RealdoBeja98/endi-s_project/main/README.md
I would appreciate any help. Thank you
So, I'm trying to create a blog that has users who can create posts, within any post there should be comments written by the users.
That's my code:
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(20), nullable=False, unique=True)
email = db.Column(db.String(120), nullable=False, unique=True)
password = db.Column(db.String(60), nullable=False)
image_file = db.Column(db.String(20), nullable=False, default='default.jpg')
posts = db.relationship('Post', backref='author', lazy=True)
comments = db.relationship('Comment', backref='commentauthor', lazy=True)
def __repr__(self):
return f"User('{self.username}','{self.email}','{self.image_file}')"
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100), nullable=False)
date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
content = db.Column(db.Text, nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
comments = db.relationship('Comment', backref='postcontainer', lazy=True)
def __repr__(self):
return f"Post('{self.title}', '{self.date_posted}')"
class Comment(db.Model):
id = db.Column(db.Integer, primary_key=True)
date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
content = db.Column(db.Text, nullable=False)
post_id = db.Column(db.Integer, db.ForeignKey('post.id'), nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
def __repr__(self):
return f"Comment('{self.content}', '{self.date_posted}')"
Also the code at my route:
#app.route("/", methods=['POST', 'GET'])
def home():
page = request.args.get('page', 1, type=int)
posts = Post.query.order_by(Post.date_posted.desc()).paginate(page=page, per_page=5)
cpost = CreatePost()
if cpost.validate_on_submit():
post = Post(title=cpost.title.data, content=cpost.content.data, author=current_user)
db.session.add(post)
db.session.commit()
return redirect(url_for('home'))
ccomment = CreateComment()
comments = Comment.query.all()
if ccomment.validate_on_submit():
comment = Comment(content=ccomment.content.data)
db.session.add(comment)
db.session.commit()
return redirect(url_for('home'))
return render_template('home.html', posts=posts, cpost=cpost, ccomment=ccomment, comments=comments)
I end up getting this error:
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such
table: comment [SQL: SELECT comment.id AS comment_id,
comment.date_posted AS comment_date_posted, comment.content AS
comment_content, comment.post_id AS comment_post_id, comment.user_id
AS comment_user_id FROM comment] (Background on this error at:
http://sqlalche.me/e/e3q8)
Traceback (most recent call last)
How can I fix this?
You need to create the comment table in your database, if you are using flask-sqlalchemy extension then you only need to call this function https://flask-sqlalchemy.palletsprojects.com/en/2.x/api/#flask_sqlalchemy.SQLAlchemy.create_all
https://flask-sqlalchemy.palletsprojects.com/en/2.x/quickstart/
Go to your python shell window and type:
from name import db
db.create_all()
name is the name of your project.
I want this to be able to filter post by the CURRENT USER and delete it. Someone told me to use .WHERE instead of .FILTER but neither of them are working and I get an Attribute Error. How can I get this working?
By the way I am able to delete ALL user post with just db.session.query(Post).delete() but I want it so it only deletes posts for CURRENT_USER.ID (the user that is currently logged into my session)
I added my current models:
#login_required
def delete_all_post():
if current_user.is_authenticated:
db.session.query(Post).delete().filter(Post.user_id == current_user.id)
db.session.commit()
flash('All of your posts has been deleted!', 'success')
return redirect(url_for('main.home'))```
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100), nullable=False)
date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
content = db.Column(db.Text, nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
def __repr__(self):
return f"Post('{self.title}', {self.date_posted}')"
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(16), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
image_file = db.Column(db.String(20), nullable=False, default='default.jpg')
password = db.Column(db.String(60), nullable=False)
posts = db.relationship('Post', backref='author', lazy=True)
delete() return True/ False which is 0 or 1 which is int so you cannot do filter over it
try:
db.session.query(Post).filter(Post.user_id == current_user.id).delete()
I am new to Flask and I have created a social media blog using Flask by following videos of Corey Schafer and Miguel Grinberg, I have incorporated additional functionalities such as Liking a post and Commenting on a post. All of the functionalities that I have incorporated are functioning properly but I cannot view the users who have liked a post.
here is my routes.py file that has the like and ViewLikers route
#app.route('/like/<int:post_id>/<action>')
#login_required
def like_action(post_id, action):
post = Post.query.filter_by(id=post_id).first_or_404()
if action == 'like':
current_user.like_post(post)
db.session.commit()
if action == 'unlike':
current_user.unlike_post(post)
db.session.commit()
return redirect(request.referrer)
#app.route('/like/<int:post_id>/viewLikes')
#login_required
def viewLikers(post_id):
post = Post.query.get_or_404(post_id)
return render_template('viewLikes.html', likers=post.get_likers())
Here is my models.py file
class PostLike(db.Model):
__tablename__ = 'PostLike'
id = db.Column(db.Integer, primary_key=True)
users_id = db.Column(db.Integer, db.ForeignKey('user.id'))
post_id = db.Column(db.Integer, db.ForeignKey('post.id'))
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(20), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
image_file = db.Column(db.String(20), nullable=False, default='default.jpg')
password = db.Column(db.String(60), nullable=False)
posts = db.relationship('Post', backref='author', lazy=True)
followed = db.relationship(
'User', secondary=followers,
primaryjoin=(followers.c.follower_id == id),
secondaryjoin=(followers.c.followed_id == id),
backref=db.backref('followers', lazy='dynamic'), lazy='dynamic')
liked = db.relationship(
'PostLike',
foreign_keys='PostLike.users_id',
backref='user', lazy='dynamic')
def like_post(self, post):
if not self.has_liked_post(post):
like = PostLike(users_id=self.id, post_id=post.id)
db.session.add(like)
def unlike_post(self, post):
if self.has_liked_post(post):
PostLike.query.filter_by(
users_id=self.id,
post_id=post.id).delete()
def has_liked_post(self, post):
return PostLike.query.filter(
PostLike.users_id == self.id,
PostLike.post_id == post.id).count() > 0
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100), unique=True, nullable=False)
date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
content = db.Column(db.Text, nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
likes = db.relationship('PostLike', backref='post', lazy='dynamic')
comments = db.relationship('Comment', backref='title', lazy='dynamic')
def get_likers(self):
return PostLike.query.filter_by(users_id =User.id, post_id=self.id)
But when I click on the viewLikes button in the template it gives me an error stating Function doesn't return any view
Simple error. In the last method you haven't returned the render_template. return render_template('')
Also call .first() on your post object