In this part 1 of a 4-part series of tutorials on building a basic message board (forum) system, we’re beginning with the Forum Registration with Python, Django, and Postgres as the primary tools. We’ll utilize the Django and Psycopg2 frameworks for opening a browser window, filling that window with content, requesting user input, processing that user input, and various database-related functions. In this part 1, our database-related function we use is to execute an “INSERT INTO” SQL command on our PostgreSQL database.
We will start out by creating two tables in Postgres, tbl_users and tbl_posts. Next, we will write up an HTML page with a form on it for the user to enter their email address (as username) and password. Then we’ll build an application in Python to use the Render and Request functions in the Django library to send the user to the HTML page and get the user’s submitted data. Finally, we’ll add a hashed version of their password to our database.
Before you begin learning how to use the Django framework, you’ll want to first understand the basics of Python use. Fortunately, there are a gazillion resources on the web for this, including articles and free video tutorials. Here’s an invaluable resource: https://www.python.org.
There is more than one IDE out there a programmer can use to write and test Python applications, including the two most popular, PyCharm and VS Code. Having used both, this programmer prefers VS Code. Bonus: It’s free. Get it here: https://code.visualstudio.com
Depending how new you are to Python and Django, you can try looking at the following overview or skip down to where we build a simple application. First, be sure not to skip installing the Django library for your Python code to include (import).
Postgres tables for forum
Create a table for managing users
NOTE: In the above SQL for creating your users table, we went a little bit “overkill” but recommend – even though we won’t be using many of these columns in the current simple forum system we’re building – you will most likely want those fields later on if you choose to enhance your message board system with more features.
Create table for storing posts
NOTES: As with the users table, we have included some columns in tbl_posts that won’t be used in our simple messaging system but you’ll probably need to have later if you expand your application with more features. One such column is id_parent. If you want messages to be able to refer to other messages – multi-threading – then you will want this field.
Django HTML template
Below is a minimal representation of HTML for a registration screen for our simple forum system. Name this file “forumRegistration.html”:
In the form above we mentioned Django’s Request function. Now we learn how to use it.
Django request from HTML
The first function we’ll examine is the “request” function, which is used to pull data that was submitted either by form “post” or by a querystring “get”.
Django Request Syntax
– field_in_querystring: This determines the name of the field being submitted; how it was named in a querystring or where method=retrieve in the HTML form. GETs are tacked on to the URL.
– field_in_form_post: This is the field name being submitted via the HTML form where “method” is set to “post” like you see in the form tag of the HTML we wrote above.
– default value if empty: If no value was submitted by the user for that field because they left the input text field empty, this is the value that will be received in the value_from_form_post piece of the equation we showed you in the syntax above. For strings, we might put an empty string here, like “”. For numeric, we might put a zero in this spot.
Let’s examine how it works:
Django Request Examples
An example of how we might use Django’s Request function can be used in the “real world”, like for example, in creating a forum system or message board:
– t_email_addr: This pulls (requests) the value the user entered into the HTML input tag called “t_email_addr” and places that value in the Python variable we also named “t_email_addr”.
Python Django’s Render function sends an HTML page to the user’s browser. Render can be filled with dynamic content by sending parameters with the function, including, especially, the document or HTML to be displayed.
Django Render Example
Note: Render needs the Django library, so you want to add the two import lines you see in the example code below.
– t_msg_from_server: The value we placed in “t_msg_from_server” is sent to forumRegistration.html, the dynamic html page we developed above, that receives and displays t_msg_from_server. Note: instead of using “t_msg_from_server”, you can name this variable anything you want and even include more parameters within the render function.
We recommend you include hashing the user’s password before storing in the database. We’ll include this in the source code at the bottom of this article.
Psycopg2 insert into Postgres
Here above we created a parameterized query with SQL. Now let’s examine the full Python source code for a Forum-type messaging system.
Full Source Code in Python
In this first of a multi-tutorial document series for creating a message board, we developed Forum Registration with Python and Postgres. We used the Django and Psycopg2 libraries for request, render, and various database-related functions.