Introduction
In this part 2 of a multi-section series of lessons, we continue building a messaging system. In this piece we build a forum login with Python and Postgres. In this section, we use the Django and Psycopg2 frameworks for render, request, and some other database-related functions. In this piece, our database function we use is to execute a “SELECT” SQL command to compare the data the user submitted for login to the data in a record in our database. In the next piece in this multi-piece series, we will create an HTML page for the user to see a list of message topics and another page to see the content of any message the user clicks on. After that, piece 4 will be posting messages to the forum.
Prerequisites
See the first lesson in this series where we created the PostgreSQL tables, a registration form, and the Python and SQL code needed for user registration: Use Django for Forum Registration with Python and Postgres: https://oceanmedia.net/use-django-for-a-forum-part-1-registration
Python Postgres Forum Project overview
In piece 1 we created a registration screen. In this piece 2, we will start out by creating a dynamic HTML login form where the user could type in their email address and password. Then we’ll write a Python application using the Render and Request functions for the purpose of getting their login information to TRY to log them in.
Since you learned how the render and request functions work in piece 1, we will only quickly go over those functions here.
Django request from form
Syntax of Django Request
Example of Django Request
NOTE: t_value_sent is encapsulated in quotes for good reason.
Example of Django Render
Django HTML template
Below is the HTML for a login screen. This will display nearly identical on all web browsers, including Chrome, Edge, Safari, and Firefox. Name this file “login.html”:
Request with Django
Next, we’ll use Django’s render function to show the user our dynamic HTML we created above, followed by requesting the data submitted by the user.
SELECT with Python
Analysis: Here we created a parameterized query with SQL for increasing safety. We’re looking for a match in our database for any rows equal to BOTH email and password using the WHERE clause.
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 tutorial document, we continued a multi-piece series of lessons to build a forum with Python and Postgres. In this piece, we used the Django and Psycopg2 libraries for render, request, and other database-related functions. In this piece, our database function we use is to execute a “SELECT” SQL command to compare the data submitted by the user for login (sign in) to our database. In the next piece of this series, we will build an HTML page for the user to view a list of message topics and another page to see the detail of a message the user clicks on.
Recent Comments