Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Web3 backend and smart contract development for Python developers part 8: Django backend (logging and logout functionality)

Posted on Oct 3 Now we will move to Django backend part. First step as always is to activate proper Python virtual enviroment. From ./musical_nft_thumbnails folder $source env/bin/activateWhat we need at this point is landing page. Login, logout, sign-up functionality as well as reset password. Ones user sign-up and login into our platform we will redirect him/her to his personal page. There he will be able to see his information, NFTs he own and buy new NFT with crypto or credit card. Create new Django app authenticationIn this newly created app we will manage sign-in process, login, logout and reset password. Add to musical_nft/settings.py authentication to list of installed apps:Now in global urls.py inside musical_nft/settings.py addNow all path we have inside authentication app will be pluged to this project urls.pyIn authentication app folder create another urls.py and pass following codeIf you didn't create super user lets do it nowNow open browser http:127.0.0.1:8000/admin while local Django server running and type superuser credentials (always make sure your postgres server is up and running $sudo service postgresql start). Ones you are logged in go to user and add new user (confirm password)At this point we need authentication view class and few html page to be rendered. Have on mind when you create Django apps that it is always 3 step process: defining urls (in app), views (in our case class based view. It can easly be function based view if you like to experiment with alternative implementations of this code) and templates (html page to be rendered with few added Django elements). Inside views.py file of authentication app add following code:Only for test purpose lets make simple home.html file with message to be displayed in browser. For this to happen create in ./muscial_nft_thumbnails/tempaltes/home.htmlThis peculiar curly brackets above are part Django syntax we need to use with our templatesThen in settings.py add define root template folderIn this project we will use bootstrap for styling. Go on their documentation page and pick up from introduction second option number two, Include Bootstrap’s CSS and JS. and pass that code in newly created base.html inside project root ./templates folder (or you can simply copy from here)Now add navbar to our base.hmtl page. We can do this by simple going to bootstrap site documentation and search for navbar in components. Choose one you like the most. For our demostration purpose first will be ok. Here is edited code that you need to pass to navbar.html file inside ./templates folder in project root directoy.And then just go to previously created base.html and add one new line bellow first body tag {% include "navbar.html"%}From root directory $py manage.py runserverGo to browser to http://127.0.0.1:8000 and check if you get proper message displayedNow if you go back to you browser you should see somethin like thisOnes we have this we can move and add some basic sign-in, login, logout and reset password functionality. For login and logout we will use Django authentication system. Here is general overview on how we plan to plug all things together related to authetication First we will further develop 3 basic templates: navbar, base and home (base includes navbar and home extends base as flow shows). We should have on mind that home will have two possbilities: 1) if user is logged we will show to him his personal page; 2) If user is not logged we will offer him login forms (you can check this in home.html). navbar.html (modified bootstrap navbar)Now base.html to includes navbar.htmlHere I want to draw your attention on last {% block content %} {% endblock content %} This is place where base.html injects any other template that inheriti from him with extendscommand (in our case home.html) Then few lines above you have a bit strange syntax with {% if messages %} {% for message in messages%} Simply this is the way we pass to our template success or error message. You will find more details about this in view.py.As we see home.html will extend base.html and have inside itself Django specific if/else statemnt. This if/else statment will help us check if user is logged in, then we will offer to him his persoanl page. In case he is not logged in we will show login form. Here is code example:Now we have all templates and we can finally move to authentication app folder where we need to update our urls.py with following codeAs you can see we added new logout path (login is part of normal home path and HomeView class) which will be handled with LogoutUser class inside authenticaiotn view.pyAs you may guess last piece of puzzle we need now is view handler which will glue all this elements together. Here is example of possbile code:In this moment run your Django server locally (be sure again your postgres server is up and runnig) and then go to http://127.0.0.1:8000 If you are logged out as admin, you should see something like thisNow type admin or newly created user credentials and you should see something like this =>Look at upper left corner of navbar logout button. This button will hit our logout/ path and then LogoutUser class handler (from views.py) which will then simply logout current user and redirect him to home page. And on this point finally we can see very nice moment in home.html. This loop inside home.html will see that user is not logged in any more and he will show him login format. And basically with this nice detail we have everything in one place: login, logout and user profile all nicely connected thorough this template loop.Now when we have login and logout let's move to develop sign-up and reset password functionality before we start to work on our web3 integration.Code for this you can find on githubTemplates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well Confirm For further actions, you may consider blocking this person and/or reporting abuse Retiago Drago - Sep 17 Scofield Idehen - Sep 24 Odumosu Matthew - Sep 7 Charlton Asino - Sep 7 Once suspended, ilija will not be able to comment or publish posts until their suspension is removed. Once unsuspended, ilija will be able to comment and publish posts again. Once unpublished, all posts by ilija will become hidden and only accessible to themselves. If ilija is not suspended, they can still re-publish their posts from their dashboard. Note: Once unpublished, this post will become invisible to the public and only accessible to ilija. They can still re-publish the post if they are not suspended. Thanks for keeping DEV Community safe. Here is what you can do to flag ilija: ilija consistently posts content that violates DEV Community's code of conduct because it is harassing, offensive or spammy. Unflagging ilija will restore default visibility to their posts. DEV Community — A constructive and inclusive social network for software developers. With you every step of your journey. Built on Forem — the open source software that powers DEV and other inclusive communities.Made with love and Ruby on Rails. DEV Community © 2016 - 2023. We're a place where coders share, stay up-to-date and grow their careers.



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

Web3 backend and smart contract development for Python developers part 8: Django backend (logging and logout functionality)

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×