Flask Tutorial Web Development with Python 21 - Dynamic user Content

sentdex · Beginner ·🔧 Backend Engineering ·11y ago

Key Takeaways

This video tutorial demonstrates how to produce dynamic user content in a Flask web application using Ginger templating, session management, and conditional rendering. It covers how to store and access user information via sessions and use Flask logic in templates to add dynamic content.

Full Transcript

hello everybody and welcome to part 21 of our practical flat tutorial video series in this video we're going to be talking about uh changing our templates a little bit based on whether or not the user is logged in and then we're going to start the transition into the final major system of this website and that's the content management system so we've already kind of touched on the content uh content management system a little bit uh but I want to talk about how we're actually going to add content and then uh especially like how how might we add bulk content and how are we going to manage that content and then say you have something you want to slip into the middle of say a video series or post or whatever uh or you want to get rid of something how do you keep the flow and all that so that's kind of one of the more major reasons why people use a Content management system rather than just like static creating things is because generally you've got links that go to and from whatever the content happens to be you want to make sure everything stays really fluid so we'll be talking a little bit about that but first what we want to do is we want to have some handling for like say when the user logs in um this no longer should it have a log in here nor a sign up I mean why would you you know leave those links there you should get rid of them and you maybe replace login with a log out and there's really no reason to have sign up at all so that's kind of what we want to do uh first so how do we do that well uh first let's open up this is dictated by our header file so let's go ahead and open up header so that would be from flas apps template and then header wonder if I can zoom in too no I can't I bet there is a way to zoom in though no anyway um go into header uh let's pull down this header file so this is our current header file now what we want well first let's fix this I don't know where these like nasty spaces came in that's pretty disgusting huh Let's uh first let's like get rid of some of this poor spacing stuff going on here I'm not really sure why we've got that uh happening there is a pretty bad job if I do say so myself uh oops well this is corresponding to that div so let's my goodness that got ugly I don't know maybe we were coding it really quick and then just didn't really think much about it uh then we got this header five tag that needs to be cut off with a slash interesting so if you would not SL H5 like I had uh make sure you do that now we're ready in theory you don't really have to do it this way but anyway right now these are the links that have the support the login and the sign up up so what do we do uh to handle that well luckily uh you can have your flask logic where's my coffee you can have your flask logic in your Ginger template so this is an HTM obviously HTML file but flask uses Ginger templating and so what we're allow that's what allows us to add this like logic here and when you view the source of course you don't see the logic right when a user or a client or whatever you want to call them views The Source on their web browser they don't see this logic right so that's the magic of Ginger templating so what we can do is we can have Dynamic HTML content being served to our users by using some logic and what logic might we use for logged in or not we we can use the exact same logic that we actually use in our flask script script script and that logic is session and then that logged in attribute so uh it's a it's like a dictionary I'm not really I don't think it's actually a dictionary it's a I don't even know what data type it would be it's it's like a dictionary but it's like that the pandas data type uh it's more of like probably a I don't know an associative array or something anyway moving along uh we've got um here this would be as if the user was logged out but what about when they're logged in we would want some different stuff so what we can do is we can do the following we can ask if session do logged underscore in and basically that's just meaning if that exists cuz remember that doesn't really uh session. logged in let's go here to the init.py real quick and then we'll do session logged in we're just searching for that so here we have session. logged in equals true so when they log in this value in the session I'm going to call it a dictionary I don't think it is especially because we're using dot we can use dot logged in there but anyway this value here is equal to true so uh so that's that and oh one more thing I will just mention is normally uh a value like any variable that you want to reference you have to pass it right so let's see so like error remember we say error equals invalid credentials right if they type the wrong thing but if we try to reference error in login. HTML without actually passing ER error through in rendered template it's actually going to say that error doesn't exist or either either say it doesn't exist or it just won't give us an error so if you try to reference a variable that doesn't exist usually it's just not going to do anything unless you're doing logic with that variable then it's going to be like this variable doesn't exist and the whole thing comes crashing down so unless the question is if variable and then if the variable doesn't exist then that would render false anyway uh but with with session session where does session come from well session comes from flask so do we and here's our import for session right here so anything that comes from flask we actually don't have to import in uh the template file and interestingly enough even if you didn't import it in your init.py you can reference stuff from the flask module that is not imported here but that's totally fine so so let's say we weren't using URL 4 anywhere in our flask file here that would be fine we could actually not use it and only use it in our template and in most cases that will be totally fine you won't actually get an error and URL 4 will act as it is expected to anyway moving along so if session equals or if session logged in so basically that's just asking if that's true so if if the user is logged in what do we want to show well we kind of want um um basically the same thing but we'll make some changes so if the user's logged in we still want to entice them to support and donate to the cause and then we give them a log out okay and then we don't need to ask them to sign up they've already done that so we this is log out instead of log in here for the glyph icon we use log out and then we have uh a final log out here that's it we don't need anything else so then we'll add another little bit of logic and that's just else else we give them this stuff Support login sign up and then finally we end with end if and that's that so we can save that wait for over here cool do we need to restart Apache no it's the HTML file that we've edited even though there is some logic there you don't have to restart Apache for any HTML logic so let's bring this website up we are logged in let's go ahead and refresh and we'll see that now we just have the support and log out awesome and then we can go to other stuff maybe we can go to well this P this is going to give a 404 but that's okay and then we can log out awesome and now we're giving back the you know the other three things then we can you know either sign up or we're like oh we didn't mean to sign up we wanted to log in and then we can log in like so no and that's that you're now logged in stuff goes away so that's how we can have Dynamic content based on certain variables now if it's you can it can either be a variable in session logged in or it could be a totally different variable that you're making in your flask file uh it can be a completely it could be any variable obviously if it's your own custom nonf flask data type variable module whatever you have to pass it to the the template but otherwise you can always use this sort of logic to dictate custom content now the only thing I will just mention is that sometimes this gets hairy because you might have something um I'm trying to think there there was an example for me at one point where you know let's say we open up a div tag and in you open up the div tag and then within uh one if statement maybe you don't close the div tag after some other div tags and then in this one you open up but then you close a little later or something uh HTML can't read your logic so HTML is going to treat div tags um it's going to treat those div Tags I'm trying to think of a a quick way to display this so you have a div here and then normally to close off that div you would have one more div so let's say you had some stuff like this so div and then div this div corresponds to this div right like that you can see that with your eyes but also HTML when you highlight it in like notepad++ or most editors it's going to do that for you and the same thing you can collapse it right you can do this and it it treats this like a block but then if you come in here you might have something let's say like another div tag and you div around this div and then I don't know Just for kicks let's say you close off the div so this would close off this original div and then here you would open another div just like you had above but then now it's treating this like the close Okay and then you would close this div and then finally you could close the final div and then now it's acting like it's closing the div way up here okay so that's an example of how using this logic can sometimes throw HTML for a loop so in reality this div either either this div closes to this div or this div closes to this div and when we render this logic using Ginger templating to the browser that's going to be fine but in your HTML file it's going to play games like this with you and so you know obviously if you got rid of this content here and we look yeah sure enough this last here does indeed close off this div but because of the logic that we're using it doesn't so long story short uh when you start to use HTML logic and like later on you're like going through your code you're like what the hell is happening it doesn't close the div it's supposed to you're going to start deleting stuff and then you're going to remember oh yeah it's because I'm using logic and it's confusing notepad++ so anyway um let's get rid of all these divs having a little div party here div div div all right let's make sure we did everything right let's go ahead and save that and let's refresh one more time but yeah it can get really confusing especially when uh you're trying to debug why maybe your your buttons or something are like down here and you're like man what's going on you're like trying to figure out where the divs are and then you have to like visualize it yourself and that's why standards are really really really important uh with something like this because as you're looking through your code you should be able to immediately say okay this div corresponds to this div which it actually doesn't according to my uh tabbing here so good example Harrison anyway um that's it uh I I think that should cover it using logic and stuff basically we wrote this and covered uh session actually we covered quite a bit of stuff but the only code we really wrote was this so in the next video we're going to be talking about is the building up the content management system how am I actually doing it on pythonprogramming.net um so we'll be getting into that there will be no ACP I didn't really see any point personally to an ACP and I'll show you exactly why I like to code in HTML but you obviously want some sort of templating that's basically what an ACP will do for you anyway uh but I'll show you guys how I'm doing it just because this website's not fully complet complete but at this point you should be able to do quite a bit stay tuned for those whenever they happen to come out otherwise as always thank you for your uh support your subscriptions thanks for watching and until next time

Original Description

Welcome to part 21 of our Flask web development tutorial, in this video we discuss how to produce dynamic user content. We can do this by storing and accessing information via the user's specific session. sample code: http://pythonprogramming.net http://seaofbtc.com http://sentdex.com http://hkinsley.com https://twitter.com/sentdex Bitcoin donations: 1GV7srgR4NJx4vrk7avCmmVQQrqmv87ty6
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from sentdex · sentdex · 0 of 60

← Previous Next →
1 Matplotlib Python Tutorial Part 1: Basics and your first Graph!
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
2 Python Encryption Tutorial with PyCrypto
Python Encryption Tutorial with PyCrypto
sentdex
3 Python's Logging Function
Python's Logging Function
sentdex
4 wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
5 wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
6 wxPython Programming Tutorial 3: Menu Bar and Menu Button
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
7 wxPython Programming Tutorial 4: Panels
wxPython Programming Tutorial 4: Panels
sentdex
8 wxPython Programming Tutorial 5: User Input Saved To Variables
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
9 wxPython Programming Tutorial 6: Multiple Choice Input
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
10 wxPython Programming Tutorial 7: Adding Static Text and Colors
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
11 wxPython Programming Tutorial 8: Custom Button Images
wxPython Programming Tutorial 8: Custom Button Images
sentdex
12 wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
13 Basic PHP Tutorial 13: Multi-dimensional Array
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
14 Basic PHP Tutorial 15: Functions and Global Variables
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
15 Basic PHP Tutorial 12: Associative Array
Basic PHP Tutorial 12: Associative Array
sentdex
16 Basic PHP Tutorial 14: Foreach loop
Basic PHP Tutorial 14: Foreach loop
sentdex
17 Basic PHP Tutorial 16: Include and Require
Basic PHP Tutorial 16: Include and Require
sentdex
18 Basic PHP Tutorial 7: Assignment, comparison and Logical operators
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
19 Basic PHP Tutorial 4: Variables and Comments
Basic PHP Tutorial 4: Variables and Comments
sentdex
20 Basic PHP Tutorial 11: Arrays part 1, basic array
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
21 Basic PHP Tutorial 6: If else and else if conditionals cont'd
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
22 Basic PHP Tutorial 1: Intro to PHP
Basic PHP Tutorial 1: Intro to PHP
sentdex
23 Basic PHP Tutorial 3: HTML with PHP
Basic PHP Tutorial 3: HTML with PHP
sentdex
24 Basic PHP Tutorial 9: While Loop
Basic PHP Tutorial 9: While Loop
sentdex
25 Basic PHP Tutorial 10: Switch Statement
Basic PHP Tutorial 10: Switch Statement
sentdex
26 Basic PHP Tutorial 2: Print and Echo
Basic PHP Tutorial 2: Print and Echo
sentdex
27 Basic PHP Tutorial 5: If else and else if conditional statements
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
28 Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
29 Basic PHP Tutorial 17: User Input Form Example / String Manipulation
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
30 Basic PHP Tutorial 18: HTML Entities and forms cont'd
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
31 Basic PHP Tutorial 19: Finding words in strings
Basic PHP Tutorial 19: Finding words in strings
sentdex
32 Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
33 Basic PHP Programming Tutorial 22: Hashing part 2: salting
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
34 Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
35 Basic PHP Programming Tutorial 21: MD5 Hashing For Security
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
36 Basic PHP Programming Tutorial 24: String similarity
Basic PHP Programming Tutorial 24: String similarity
sentdex
37 Basic PHP Programming Tutorial 25: Time and Time stamps
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
38 Basic PHP Programming Tutorial 26: Die and Exit
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
39 Basic PHP Programming Tutorial 27: MySQL Databases Part 1
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
40 Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
41 Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
42 Basic PHP Programming Tutorial 30: MySQL database in Use
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
43 Django Tutorial Web Development with Python Part 1: Installing Django
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
44 Python Tutorial: File Deletion and Folder Deletion / directory deletion
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
45 Python Tutorial: How to Rename Files and Move Files with Python
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
46 3D Graphs in Matplotlib for Python: Basic 3D Line
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
47 3D Plotting in Matplotlib for Python: 3D Scatter Plot
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
48 3D Charts in Matplotlib for Python: Multiple datasets scatter plot
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
49 Sikuli Tutorial 1: Visually programming in python!
Sikuli Tutorial 1: Visually programming in python!
sentdex
50 Sikuli Tutorial 2: Program visually in python!
Sikuli Tutorial 2: Program visually in python!
sentdex
51 Sikuli Tutorial 3: Program visually in python!
Sikuli Tutorial 3: Program visually in python!
sentdex
52 3D Bar Charts in Python and Matplotlib
3D Bar Charts in Python and Matplotlib
sentdex
53 3D Plane wire frame Graph Chart in Python
3D Plane wire frame Graph Chart in Python
sentdex
54 Raspberry Pi Part 1 Introduction
Raspberry Pi Part 1 Introduction
sentdex
55 Raspberry Pi Part 8: First Download and Update! (Firmware)
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
56 Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
57 Raspberry Pi Part 11: Remote Desktop
Raspberry Pi Part 11: Remote Desktop
sentdex
58 Twitter Analysis: How to rank a user's influence
Twitter Analysis: How to rank a user's influence
sentdex
59 GPIO Tutorial for Pi Part 2 - Programming the GPIO
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
60 GPIO Tutorial for Raspberry Pi Part 1 - Setting up
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex

This video tutorial teaches how to produce dynamic user content in a Flask web application using Ginger templating and session management. It covers how to store and access user information via sessions and use Flask logic in templates to add dynamic content. The tutorial provides a step-by-step guide on how to implement dynamic user content in a Flask application.

Key Takeaways
  1. Open header file from Flask app template
  2. Remove unnecessary spacing and tags
  3. Use Flask logic in Ginger templates to add dynamic content
  4. Check session attribute to determine user login status
  5. Ask if session logged_in exists
  6. Display different content based on user login status
  7. Use dot notation to access session variables
  8. Pass variables to templates
💡 Using session variables and Flask logic in templates allows for dynamic user content and conditional rendering in a Flask web application.

Related AI Lessons

Up next
This Cop Was Held Accountable For His Brutality! #police #lawyer
Hampton Law
Watch →