In this post, I am going to start teasing apart Finance from CS50 problem set 7. Now, this is going to be another one that is too long for a single post. So, I will be separating it out into parts. This week we will tackle part 1, where we will implement register. This function will allow a user to register by setting a username and password. Let’s get started.

To start, I am going to cover how we implement register.html. You will notice that we are using the Jinja template language so that the page will look the same across the site. Get used to using the extends, block title, and block main, as you will use these for every html page we write in this problem set. Now, we want to create a form for the user to register. The form below is submitted using the post method.  Next, we create three inputs for the user to input their name, a password, and a password confirmation. The last item is a button that will submit the form, which will call our register function in our Python document.

Below is the implementation of the register function that will execute when the submit button is clicked on our form. The first thing we want to do is make sure that the user landed on the page via post method. If not, we will redirect them to the register.html page. The next three if statements are for input validation. The first makes sure that the user has entered something into the username field. The second makes sure that a user has typed in their password twice. And the third makes sure that both passwords match.

Now that we have validated that the user has typed something in all the fields, the next step is to hash the password. This will make sure that no one, including us, will know the users password. The next statement enters the username and password into our database of registered users. Remember that we want to use the :username and :hash notation to avoid a SQL injection attack. Since the username field in the database in UNIQUE, if the username already exists nothing will be stored in result. If, however, the username doesn’t exist, the primary key (or id) will be stored in the result. Thus, we can set the current user_id to the result, and redirect the user to the home page (index).

And that is all there is to registering a user. Please comment below with any questions, and feel free to click the buttons below to share on your favorite social media site. As always…Happy Programming!!

14 comments

  1. Hello, once again. Sorry to disturb. I have checked my code I have used some of your codes. I still have that error message. I didn’t create any table named ‘transaction’ I don’t know how to move from here. I would appreciate your help.

    Like

    1. Don’t be sorry. I enjoy helping people grasp the material. So, if you did not create a table named transaction, this is why you are getting the error. Basically, you are trying to query data from a table that doesn’t exist yet. So, we just need to create the table. I show how to add this table to the database in this tutorial. Hope this helps!

      Like

  2. Hi Jason, I’ve found this website incredibly useful for CS50, so thanks! I was having some trouble with my code and tried to run what you have posted here to see if I could debug and compare to figure out what’s not working. Unfortunately even running your code I’m not able to get past the log in page and have a similar problem to the first comment on this page. I have both the ‘users’ and ‘transactions’ tables in my database as well. Thanks.

    Like

  3. Hi! I’m working on this problem set right now, and have some trouble trying to fix a bug I have. It’s about the /login, which is weird, since I haven’t done anything with it yet. But maybe you could help? 🙂

    I get this from chech50:

    😦 logging in as registered user succceeds
    application raised an exception (see log for details)

    Log
    sending POST request to /login
    exception raised in application: TemplateSyntaxError: Expected an expression, got ‘end of print statement’

    Do you know what this is about?

    Really appreciate your help! And love your blog – very intuitive and great descriptions for beginners!

    Like

    1. Thanks! I appreciate the feedback. Well, if you haven’t done anything with /login route yet, that is probably where the error is coming from. Check50 is trying to see if the login route works and it is not passing their tests. Have you had the time to write the login route since this comment?

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s