-->
How To Approve or Unapproved Results in Flask Python example.

How To Approve or Unapproved Results in Flask Python example.

Back to top

Updated by Ashirafu Kibalama on March 31, 2024

"Managing Results: A Guide to Approving or Disapproving Entries in Flask Python"




#Only approving two posts: id 1 and 2:



In web development, managing user-generated content is crucial. Flask, a Python web framework, offers a toolkit for building web apps. 

With a practical example, we'll explore how to approve or unapproved content in Flask. By the end of this post, you'll know how to manage user-generated content in your Flask app.

"Here's an example of how to approve or unapproved results in Flask using Python."



7 Steps To Approve or Unapproved Results in Flask Python



Step 1) Modify your Class Model with  the status attribute, approve and un-approve method:


#before modification



#after modification



Additionally, two methods, approve() and unapprove(), are defined within the class. These methods provide functionality to change the approval status of an entry.


  • The approve() method sets the status to 'approved', indicating that an administrator or moderator has approved the entry.


  • The unapprove() method sets the status to 'unapproved', indicating that an administrator has disapproved or revoked approval of the entry.


These methods allow for dynamic management of the approval status of entries within the Flask application, providing flexibility and control over user-generated content.


#main.py

# OTHER CODES ABOVE


# CONFIGURE TABLE
# CREATE TABLE IN DB FOR POSTS
class Posts(db.Model):
__tablename__ = "posts"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
title: Mapped[str] = mapped_column(String(300), unique=True, nullable=False)
description: 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)
author: Mapped[str] = mapped_column(String(250), nullable=False)

status = mapped_column(String(20), default='pending') # pending, approved, unapproved

def approve(self):
self.status = 'approved'

def unapprove(self):
self.status = 'unapproved'

# OTHER CODES BELOW

Note:

At this moment, when you run your application, you will see this error:


"

OperationalError


Click Here To Learn To Fix the error: 





Step 2) Create a route to manage all posts within the application. ie. manage_posts().






Step 3) Create a template for access control implementation or user-specific information display, such as "manage_all_posts.html".


#manage_all_posts.html

{% include "header.html" %}


<!-- Page Header-->


<!-- Main Content-->


<div class="container">
<table class="table table-hover">
<thead>
<tr>
<th scope="col">Id</th>
<th scope="col">Blog Post</th>
<th scope="col">Blog Author</th>
<th scope="col">Edit</th>
<th scope="col">Action</th>
<th scope="col">Status</th>
<th scope="col">Delete</th>
</tr>
</thead>
<!-- Post preview-->

{% for post in all_posts %}

<tbody>
<tr>
<th scope="row">{{ post.id }}</th>
<td>
<a href="{{ url_for('show_post', post_id=post.id) }}">
{{ post.title }} || {{post.date}}
</a>
</td>
<td>{{ post.author}}</td>
<td>
<a href="#" class="btn btn-primary">EDIT</a>

</td>
<td>
{% if post.status == 'pending' %}
<a href="#" class="btn btn-success">Approve</a>
{% elif post.status == 'approved' %}
<a href="#" class="btn btn-warning">Unapprove</a>

{% else %}
<a href="#" class="btn btn-success">Approve</a>

{% endif %}


</td>
<td>{{ post.status }}</td>



<td>
<a href="#" class="btn btn-danger">DELETE</a>
</td>


</tr>

</tbody>
{% endfor %}

</table>

</div>


<!-- New Post -->

<!-- <div class="d-flex justify-content-end mb-4">-->
<div class="d-flex justify-content-around">
<a
class="btn btn-primary float-right"
href="#"
>Create New Post</a
>

<a
class="btn btn-primary float-right"
href="#"
>Approve All Posts</a
>
<a
class="btn btn-primary float-right"
href="#"
>Unapprove All Posts</a
>
</div>


{% include "footer.html" %}


Step 4) Create the approve_post() route:







The route is associated with the URL '/approve'. However, it's commented out with # @admin_only, indicating that it might be intended only for administrators. 


The @admin_only could potentially be a custom decorator that restricts access to users with administrative privileges.


Step 5) Create the unapprove_post() route:




Similar to the previous route, this route is also commented out with # @admin_only, suggesting it must be intended for administrators only.


Step 6) Modify your index template. ie. index.html.


<!--  Articles Starts     -->
{% for post in all_posts %}


<article class="blog-post">
<!-- Post preview-->
<div class="post-preview">
<a href="{{url_for('show_post', post_id=post.id)}}">
<h2 class="post-title">{{post.title}} </h2>
<h3 class="post-subtitle">{{post.subtitle}} </h3>
</a>
<p class="post-meta">
Posted by
<a href="#">{{post.author}}</a>
on {{post.date}}

</p>
</div>
<!-- Divider-->
<hr class="my-4" />


</article>



<!-- Articles Ends -->

{% endfor %}


#after modification by adding: {% if post.status == 'approved' %} #Other codes# {% endif %}



<!-- Articles Starts -->
{% for post in all_posts %}

{% if post.status == 'approved' %}
<article class="blog-post">
<!-- Post preview-->
<div class="post-preview">
<a href="{{url_for('show_post', post_id=post.id)}}">
<h2 class="post-title">{{post.title}} </h2>
<h3 class="post-subtitle">{{post.subtitle}} </h3>
</a>
<p class="post-meta">
Posted by
<a href="#">{{post.author}}</a>
on {{post.date}}

</p>
</div>
<!-- Divider-->
<hr class="my-4" />


</article>


{% endif %}
<!-- Articles Ends -->

{% endfor %}

Step 7) Modify your templates. ie. manage_all_posts.html.


#manage_all_posts.html
# modify this part:




#after modification






#results post with id 2 and 4 are approved:





This tutorial explored six essential steps to implement a robust system for approving or unapproving results in a Flask Python application. 


We enhanced our class model, created a dedicated route, crafted a template, devised two routes, and modified our templates. 


By following these steps, developers can integrate an approval system into their Flask Python applications, empowering administrators to manage user-generated content with ease, maintain quality standards, foster user engagement, and uphold the integrity of their platforms.


Please let us know if you have benefited from this post or if we missed anything. Your feedback is highly appreciated.


"Thank you for your message. I hope you have a great time coding!"


Other Posts: