Demo Application
Set up the Reddit application and look at its implementation.
We'll cover the following
Now that we've understood Reddit's API, we can use it in an actual application.
The layout of our application
We use two main templates in our application. One is the index.html
with bootstrap version 4 and the navbar. The other is the page.html
that extends the index.html
, and adds posts to the navbar. The posts can be from any subreddit the user has chosen in the navbar. The file handles all the interactivity. To keep views.py
clear and concise, we separate the PRAW to the redditapi.py
file and then import the functions from that file.
Explanation of PRAW in the project
Explanation of the code present in the redditapi.py
is as follows:
- Line 1: We import
praw
. Then we make a function calledgetreddit()
to create an instance ofReddit
in other functions. - Line 14: We make a function to load a list of posts for the given subreddit.
- Line 39: We make a function that returns all the subreddits the user has subscribed to.
- Lines 43–44: We filter out
display_names
that have the prefix ofu_
, as they are users and not subreddits. - Line 47: The next function votes on a post. Both the vote direction and post ID must be provided.
- Line 56: We write a function that comments on a post given its ID and the text of the comment.
- Line 62: We make the function that returns posts matching the query.
- Line 72: we get and return the image URL of a post.
- We use the functions explained in the
views.py
to populate data and make actions.
Click “Run” to run the application.
Get hands-on with 1400+ tech skills courses.