Demo Application

Set up the Reddit application and look at its implementation.

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:

  1. Line 1: We import praw. Then we make a function called getreddit() to create an instance of Reddit in other functions.
  2. Line 14: We make a function to load a list of posts for the given subreddit.
  3. Line 39: We make a function that returns all the subreddits the user has subscribed to.
  4. Lines 43–44: We filter out display_names that have the prefix of u_, as they are users and not subreddits.
  5. Line 47: The next function votes on a post. Both the vote direction and post ID must be provided.
  6. Line 56: We write a function that comments on a post given its ID and the text of the comment.
  7. Line 62: We make the function that returns posts matching the query.
  8. Line 72: we get and return the image URL of a post.
  9. 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 1200+ tech skills courses.