qasimalbaqali
qasimalbaqali

Reputation: 2131

Many-to-one Flask SqlAlchemy rendering

I'm trying to create categories for my posts. End result should be a user when trying to post, they choose a category, the post goes under that category so it can be indexed there. So for example they'd go on example.com/category/Flask and they'd find all the posts that have the Flask category. I added to the post model a category_id and category and then I created a Category model with an id and a name. Then created a form so I can add categories to the category table so users can start choosing categories for their posts. What has worked for me is that I can create categories, choose them when posting, and when I look at the database tables I can see that those posts have categories assigned to them. So what I want to do now is to create a view for /category/, and I have no idea how to do that.

class Post(db.Model):
    __tablename__ = 'posts'
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(80))
    body = db.Column(db.Text)
    timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
    author_id = db.Column(db.Integer, db.ForeignKey('users.id'))
    body_html = db.Column(db.Text)
    comments = db.relationship('Comment', backref='post', lazy='dynamic')
    category_id = db.Column(db.Integer, db.ForeignKey('categories.id'))
    category = db.relationship('Category',
                               backref=db.backref('posts', lazy='dynamic'))


    @staticmethod
    def on_changed_body(target, value, oldvalue, initiator):
        allowed_tags = ['a', 'abbr', 'acronym', 'b', 'blackquote', 'code', 'em', 'i', 'li', 'ol', 'pre', 'strong',
                        'ul', 'h1', 'h2', 'h3', 'p']
        target.body_html = bleach.linkify(bleach.clean(
            markdown(value, output_form='html'),
            tags=allowed_tags, strip=True))

db.event.listen(Post.body, 'set', Post.on_changed_body)


class Category(db.Model):
    __tablename__ = 'categories'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50))

    def __repr__(self):
        return '<Category {}>'.format(self.name)

I created a CategoryForm

class CategoryForm(Form):
    name = StringField('Category name', validators=[DataRequired()])
    submit = SubmitField('Submit')

Category.html

{% extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf %}
{% import "_macros.html" as macros %}

{% block title %}Flasky{% endblock %}

{% block page_content %}

{{ wtf.quick_form(form) }}

{% endblock %}

Category view

@main.route('/add_category', methods=['GET', 'POST'])
def add_category():
    form = CategoryForm()
    if form.validate_on_submit():
        category = Category(name=form.name.data)
        db.session.add(category)
        flash('A confirmation email jas been sent you.')
        return redirect(url_for('main.index'))
    return render_template('add_category.html', form=form)

and then after adding categories I edited the PostForm with

def enabled_categories():
    return Category.query.all()


class PostForm(Form):
    body = PageDownField("What's on your mind?", validators=[DataRequired()])
    category = QuerySelectField('Category', query_factory=enabled_categories,
                                allow_blank=True)
    submit = SubmitField('Submit')

And this is the route I have created.

@main.route('/category/<name>')
def view_posts_in_category(name):
    name = Category.query.filter_by(name=name)
    return render_template('category.html', name=name)

So now in the category.html 'category/Flask' how can I show all the posts under the Flask category? e.g {{ category.post }} ? What should I use?

Upvotes: 0

Views: 1489

Answers (1)

Miguel Grinberg
Miguel Grinberg

Reputation: 67509

You are almost there. What you need to do is run a query for the posts that belong to the selected category and add that to the template. It would be something like this:

@main.route('/category/<name>')
def view_posts_in_category(name):
    cat = Category.query.filter_by(name=name).first_or_404()
    return render_template('category.html', name=name, posts=cat.posts)

Upvotes: 2

Related Questions