Showing posts with label registration and logout. Show all posts
Showing posts with label registration and logout. Show all posts

Wednesday, December 18, 2019

Python Django : login, registration and logout

Python Django : login, registration and logout

we are going to explore how to handle user authentication related feature in this article.

Before going further you should know about django template language, form handling, and django ORM, if not go to the previous articles about them.

Our Aim to create three fully working functional page as shown below



Code for the signup and login pages is given below. Both pages contain looks similar except for one email field in signup page and URL for signup in the login page.

If msg is passed to these page in context then they both page show msg first.



Now look at the index page and urls.py below which is quite simple, a user is passed to index and show its username and email associated with the user.



Here for storing users details, we use Django built-in User model and hence first importing this. We also need to import authentication and login as auth_login, logout as auth_logout because we use the function name as login and logout hence it conflicts if we import normally.

One new thing is request.user.is_authenticated that simply returns boolean whether the user is login or not?


Here authenticate function takes three argument request, username and password and tries to search data in User daatabase. If match occurs the return the user else return None.



If a match occurs the auth_login simply store the user in session means now the user is logged In and redirects to the index which returns index page now.

In logout function, first, check that the user is logged in or not. If logged in the auth_logout remove the user from session means logout user. Note that auth_logout only take request as an argument, not user.


So, now only signUp page view is required, look at the below code


After basic checking, extract the data from the form which is sent by the user and check that username is exists or not in User database.

If the username is not present means it is unique then except block code is run and create a new user with create_user function and save to database with user.save(), then log in the user and redirects to index.

If the username exists in database then return signup page with error message

Arrays in Solidity Programming Language.

Arrays Solidity supports both generic and byte arrays. It supports both fixed size and dynamic arrays. It also supports multidimensional ...