Introduction
In this part 3 of a multi-piece series, we continue building a messaging system. In this piece we build a way to display forum messages with Python and Postgres. In this piece, we use the Django and Psycopg2 frameworks for render, request, and a few database-related functions. In this piece, our database function we use is to execute two different “SELECT” SQL commands to view both topics and messages in separate dynamic HTML web pages. In the next piece in this multi-piece series, we will set up posting messages.
Prerequisites
See parts 1 and 2, where we developed the underlying Postgres tables, a registration form, and the SQL and Python application needed for user registration: Use Django for Forum Registration with Python and Postgres: https://oceanmedia.net/use-django-for-a-forum-part-1-registration
Django forum messages lesson overview
In piece 1 we created a registration screen. In piece 2, we built a dynamic HTML login form where the user typed in email address and password to sign in to the application and give us the id_user value to use in this piece 3 to view topics and messages.
Because you learned how the render and request functions work in piece 1, we will quickly go over those functions in this piece below.
Django request input
Syntax of Python Request Input
Example of Django Request
NOTE: t_value_sent is encapsulated in quotes for good reason.
Example of Python Request Input
Example of Python Render
Django HTML templates
We are going to build two pages here, one being a page to display topics and another to display message detail when a user clicks on the topic for that message.
Here’s a reminder of what tbl_posts looks like:
Now for the html: Name this first file “topics.html”. NOTE: We are leaving out the basic encapsulating html tags that you know by now how to include.
Now for message.html:
Now that we have examined the more complex pieces of the sign in portion of our overall project, let’s put it all together into a comprehensive Python application.
Full Source Code in Python
Conclusion
In this piece 3 of a 4-piece forum-building multi-piece series, we continued building the messaging system. In this section we built a way to display forum messages with Python and Postgres. We used the Django and Psycopg2 frameworks for render, request, and other database functions. In this piece, our database function we used to execute two different “SELECT” SQL commands was execute() to view topics and message content in separate server-generated HTML pages. In the next piece in this multi-piece series, we will build a feature where the user can post messages to the forum.
Recent Comments