THE BIG PROJECT — LAST PART*

Ok ,I know that new year is here and everyone is busy in planning how to start their 2K18 with a bang. But unfortunately, I don’t have much of a plan right now and hence I thought of ending what I started long long back (if I finish it tomorrow, it would be like I started a project in 2K17 and finished it in 2K18, then it would be too long). So, it is the final task regarding linking our flask with MySQL. We already configured and initialized our database and now we have to insert our data which is inside the forms to our database. So, let us also finish our 2K17 with a bang!!

We already saw these three lines of code earlier,

@a.route(‘/register’, methods=[‘GET’, ‘POST’])
def register():
form=RegisterForm(request.form)

When we had to just display our registration page, there was no need of checking the GET request. But, when we have to store our content we have to check whether the POST request is satisfied along with the validators. As our form contains all our data, we can get those data with the help of form.field_required.data. We have a password field in our database, hence that password should be encrypted for security. For that purpose, we have to import as follows,

from passlib.hash import sha256_crypt

Usually we would have written like — password=form.password.data, but this data would be in bits format and hence needs to be converted into string. So our command would become, password=str(form.password.data). Now we have to do encryption on this string as follows,

password=sha256_crypt.encrypt(str(form.password.data))

A cursor is required basically to run our MySQL commands in flask. For eg, we use insert into command in our MySQL to insert values. If we want to use this in flask, we need a cursor. We can create a cursor and insert all the above mentioned data in our database as following,

After executing this command, now we have to connect to our database and after the process is complete, we have to close the connection. This can be done as shown below:

mysql.connection.commit()
cur.close()

After the registration is done, we can display a message thanking the user for registration. We can do it in different manners by using flash, our returning the url for another page etc. We are here returning the url for our home page as return redirect(url_for(‘home’)), hence we need to create a home function in our base address as follows,

@a.route(‘/’)
def home():
return render_template(“thankyou.html”)

Thankyou.html consists of basic text displaying a thank you message. So our whole code for connecting our flask with the database would be as shown,

Our program would not work without a secret key which is a mandatory statement. Hence, we have to include a secret key as shown,

if __name__ == ‘__main__’:
a.secret_key=’secret123'
a.run()

You can use any key of your own, there is no validators for it. Now we are ready for the BIG PROJECT, we are registering with following details,

As soon as we submit, we are transferred to thankyou.html page with following message,

Now we can check our database for the data storage, you can visit the following link and see our database. I tried to post the image, but the font was too small, so I thought it would be better to share a link,

You can see the id as 21, because I have been storing and deleting the data continuously. You can create a new table and the count would begin from 1. So finally we are done with our so called THE BIG PROJECT. This was something which required knowledge in different applications, hence it was a very useful project, I would try to do more such projects. I will see you in 2K18, enjoy the new year with your family, friends and special ones, take some resolution and try to follow it and last but definitely not the least, keep coding…

Thank you for reading, if you liked the article please click on the clap button and show your support. You can clap more than once, just keep pressing the icon!!! Happy new year in advance, I wish that GOD fulfills all your wishes and you have a fantastic 2K18. Keep smiling and stay blessed.

VLSI Engineer with the urge for sharing knowledge.