title: Getting specific post category in Flask
body: <p>I must get post if my category == academic (SelectField choice). But it doesn't work (posts are not <strong>shown</strong>). How can I get post data or form data for this?</p>
  <p>I've tried</p>
  <pre><code>@categories.route(&quot;/category/academic&quot;)
  def academic():
      page = request.args.get('page', 1, type=int)
      posts = Post.query.order_by(
          Post.date_posted.desc()).filter(
          Post.category == &quot;Academic&quot;).paginate(page=page, per_page=5)
      return render_template('academic.html', posts=posts)
  </code></pre>
  <p>and</p>
  <pre><code>@categories.route(&quot;/category/academic&quot;)
  def academic():
      form = PostForm()
      page = request.args.get('page', 1, type=int)
      post = Post(title=form.title.data,
                  content=form.content.data,
                  category=form.category.data)
      posts = Post.query.order_by(
          Post.date_posted.desc()).order_by(
          post.category == &quot;Academic&quot;).paginate(page=page, per_page=5)
      return render_template('academic.html', posts=posts)
  </code></pre>
  <p>and</p>
  <pre><code>@categories.route(&quot;/category/academic&quot;)
  def academic():
      page = request.args.get('page', 1, type=int)
      posts = Post.query.order_by(
          Post.date_posted.desc()).filter(
          **PostForm**.category == &quot;Academic&quot;).paginate(page=page, per_page=5)
      return render_template('academic.html', posts=posts)
  </code></pre>
  <h2>Post.category</h2>
  <pre><code>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)
      category = db.Column(db.String(50))
      user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
  </code></pre>
  <h2>PostForm.category</h2>
  <pre><code>class PostForm(FlaskForm):
      title = StringField('Title', validators=[DataRequired()])
      content = TextAreaField('Content', validators=[DataRequired()])
      category = SelectField('Category', validators=[DataRequired()], choices=[(
          'Academic', 'Academic'), ('Art', 'Art')])
      submit = SubmitField('Post')
  </code></pre>
  <h2>Template (category.html)## This template for posts - Showing the category.html - All of template</h2>
  <pre><code>{% extends &quot;layout.html&quot; %} {% block content %} {% for post in posts.items %}
  &lt;article class=&quot;media content-section&quot;&gt;
    &lt;img
      class=&quot;rounded-circle article-img&quot;
      src=&quot;{{ url_for('static', filename='profile_pics/' + post.author.image_file) }}&quot;
    /&gt;
    &lt;div class=&quot;media-body&quot;&gt;
      &lt;div class=&quot;article-metadata&quot;&gt;
        &lt;a
          class=&quot;mr-2&quot;
          href=&quot;{{ url_for('users.user_posts', username=post.author.username) }}&quot;
          &gt;{{ post.author.username }}&lt;/a
        &gt;
        &lt;small class=&quot;text-muted&quot;&gt;{{
          post.date_posted.strftime(&quot;%Y-%m-%d&quot;)
        }}&lt;/small&gt;
        &lt;small class=&quot;article-category&quot;&gt;
          &lt;mark
            &gt;&lt;a
              class=&quot;mr-2&quot;
              href=&quot;{{ url_for('categories.category_route', category=post.category) }}&quot;
              &gt;{{ post.category }}&lt;/a
            &gt;&lt;/mark
          &gt;
        &lt;/small&gt;
      &lt;/div&gt;
      &lt;h2&gt;
        &lt;a
          class=&quot;article-title&quot;
          href=&quot;{{ url_for('posts.post', post_id=post.id) }}&quot;
          &gt;{{ post.title }}&lt;/a
        &gt;
      &lt;/h2&gt;
      &lt;p class=&quot;article-content&quot;&gt;{{ post.content }}&lt;/p&gt;
    &lt;/div&gt;
  &lt;/article&gt;
  {% endfor %} {% for page_num in posts.iter_pages(left_edge=1, right_edge=1,
  left_current=1, right_current=2) %} {% if page_num %} {% if posts.page ==
  page_num %}
  &lt;a class=&quot;btn btn-info mb-4&quot; href=&quot;{{ url_for('main.home', page=page_num) }}&quot;&gt;{{
    page_num
  }}&lt;/a&gt;
  {% else %}
  &lt;a
    class=&quot;btn btn-outline-info mb-4&quot;
    href=&quot;{{ url_for('main.home', page=page_num) }}&quot;
    &gt;{{ page_num }}&lt;/a
  &gt;
  {% endif %} {% else %} ... {% endif %} {% endfor %} {% endblock content %}
  </code></pre>
  <p>Doesn't work because posts are not <strong>shown</strong>.</p>