Django Tutorial - Creating Django user profile #12

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there welcome everyone to part 12 of the web development in python with django we all know that if a page has registration and login tabs it must have a user profile page so in this part we'll crack on our user profile so that our users could edit their description name surname or other details you want to include also it's important that these profiles could be accessed by other users for example so that anyone could be able to check the profile of the author of any published post on our page we start by creating an url for our profile we created our user model so that users can't change a username when registered and it's unique which means that best idea is to access user profiles by using it so we go to our jungle project users urls right have open this this file script files and insert a few lines with a path to a new profile function in our views that will be creating so right now i have a few old code here so i'll remove this old code because we i don't need this and i'll create a new one that i mentioned about profile so this will be called a profile and we'll access this by by user username username and this path will call a profile function and we'll of course name it as a profile that's pretty easy and from our previous tutorial we know that we definitely gonna need to create a new form for our users so we could easily access and modify our profile information so now we go to our pro django project uss forms and create a new user update form and we go to the bottom and here i'm creating a new object that i'll call i'm calling user date for and this will inherit from forms dot model form this is pretty pretty simple and we'll use a basic name surname email and description fields from our user models that i already have here and find and this is my custom one if you were following my previous tutorial if you didn't please check this out before doing all other stuff because it might not work for you from this point i'm already here and i need to create that i will use uh email and it will be equal to forms and email it's an email field we already know that and there is a class that we call meta and this will be equal to model and this model will be equal to get user model function and here is the field that we're gonna pass when we call for this object so here is some kind of basic fields from our original user model so here is a first name first name and then there is a last name then there is an email also kind of default one field and this one is a description this is the custom one if you remember we created this one here and of course we created an email and we made it unique and that's fine and yeah also we can give it a status but status is also some kind of field that user might not change because the status is given to a user when it registers and this might only be changed by administrator for example it might be a regular user it might be administrator it might be some kind of subscriber like a moderator or so on so this this is the reason i made this status additional feel so that's cool and this is done we don't need this anymore it's only a few lines of code to achieve our goal here now right now we are creating totally new page for our users where they might check their profile and change description and etc this means that we'll need to create a new html template same as before i already made one the templates and if i'll navigate there there is a profile template of course i'm not going to explain this detail but but i already made this to save us time on this video because this template to create or 41 line might take a little more time than we expect because html is uh well it takes times and it's pretty simple to write as you might notice from this template we are checking if the user username is equal to the username we do this here that we have in our form the purpose is to check whether this template is accessed by the owner or the profile or not so that we could preview and update the profile on the same link don't hesitate to modify this template if you have more fields in your user model that you want to display you might have this uh if you're building on this django tutorial while going from video to video and now when i finish this template we're close to testing how it works the last thing to do is to create our profile function in our views to do so we are we go there and create a basic profile function along with a few additional imports so i go to my views and i'm pretty sure that there will be some new imports as you saw there we used uh yet user modules so of course i need to import this from authentication tab and i add this here and of course i'll need uh some kind something from new from forms and i have a new object here so i also need to import it you said date four okay that's fine we go to bottom fit and here we type a profile and it will be equal to request and we access always a username or the profile no other choice and as always we check if the request method is equal to post okay sorry there's a mistake that's my mistake as always and for now i'll skip this because we want to check if this this part of code works at all so i need to get the user from our database and to do that i use this called get user model function we call it and here i call objects and we filter it by using our name and we check if the username of this user actually we search for our username in our user database or that is equal to this username that's damn simple and if we are able to find anything it's it's it's impossible to have a few usernames because as i said our user has an unique username so if we found a user in our database we construct a new form and it will be user update form that we created and instance will be equal to user and then we simply return uh an object to our new template we created so i i simply copy this for simplicity reasons not to type too many things here and here will be my user and now instead of login it will it will be for profile profile and the constant context will be equal to the same form that we got from our user mode and otherwise if we don't have any we we can find any user in our database with with the within the this link we simply return uh i use it to a home page well i think that's the best idea to do so okay and now for now we'll as i said we skip the post method and at first let's try how it works with a simple get method and right now in my mind it should already work so i'll run this in a debug mode to make sure everything works and if it's not to catch all the bugs we when i open this page and as you can see everything is fine so right now i know that we have a profile and my profile is a test user and as you can see it works like a charm and everything works as expected and we can enter the profile and we can see that that's a regular test it's email there also should be a description but because it's empty it doesn't type anything for us okay it works as expected and now i suppose we can make it to work or or accept our profile changes so first i would like to log into my test user oh yeah yes that's nice this is a recaptcha from previous tutorial oh yeah that's amazing and now i type a profile and there will be a test cool cool and as you can see it gave me some kind of description and simply just an information because i'm right now logged in but but if i'll try to add something here it will not work because we don't have a positive request here differently from the get method in a post method will feed request posts and request files to our user update form so i'll need to do that of course so instead of pass i'm grabbing the user of the that we have with which is trying to do this post request because of course uh not logged in user might not do a post request it's pretty impossible so at least i don't know if it's possible but i hope so it's not so forms will be equal to this one but instead we'll feed more information not only a user so here will be a request post and request files i'm passing this file because in the next tutorial we'll play around with images and so our user could have their own profile image otherwise will be pretty obvious if we're following my previous attack it's nothing new here that we haven't covered already so uh let's finish this so form and we always check if the form is valid let's try and if it's valid we typically save it user form will be equal to form.save and we need to type something as a feedback for our users in our front end so we type a message success on its messages that's right message success here's a request and here i'll simply type i'll simply type a user form dot username and here your profile has been updated that's right and now we simply uh return redirect i use it to um again to the same uh profile page where it will give up user form and username in case there is an error we of course would like to print some messages so i'll copy these lines and exactly as the same and well that's what we do and if form is not valid definitely there is some kind of wrong stuff happening here and we'll message this information to our front end and if there is uh not val form we try again to get user with a username if we found it we'll direct it and that's it otherwise we go to homepage okay let's test how it works and now we let's go to our profile i hit the refresh and let's try to update the description just just a test admin user so let's update this and that's cool yes your profile has been updated all right that this is this works and i'm really glad that we can train do this changes so quickly and easily and have you even thought it will be that simple before starting going through this tutorial that's why i love jungle when you're at least a little familiar with it everything is getting easier and more and straightforward so now we can view our and other profiles and we got the functionality we wanted but we still need simple access to these profiles because right now i can only access them by typing the exact url to it first when the user is logged in the account we want to display a button that links to this profile because we have only a logout here so why not adding some kind of profile button to do that we need to edit our navigation bar so we head to our code and we go to our main application main and we go to templates main includes and here's a navigation bar here we check if the user is authenticated so here is the best place where i can add something new so here i simply uh copy this for complete reasons and this is the navigation link but instead of linking to the logout button we link it to the profile and we feed our user.username to to this link and of course we need to close this uh new new stuff and here i need to add some kind of y same as here and here will be our profile now it will not profile but we'll make it as a user. username and there is a far easier so that's cool and let's save it and let's go back to our website and let's see how it works and as you can see this new profile button appeared here that's amazing and if we log into another user for example i don't know my name is by lessons if i have a pi lessons it will be pilot since i mean if i click it i can go to home page and then i go to my profile it's a simple button but to our own profile with the username as a parameter this does a huge impact and nice it works as expected as before but still user usually when someone creates a record or a post on a website usually somewhere it's mentioned in the author and date of publication but we know that our article and articles article series don't record this information at least we do not record the author who created a post so let's modify our article on article series model set so that every time we create a new post the offer would be automatically recorded let's head to our main application where we created here and uh i'll close this and we have a modules here now we need to import some kind of new stuff so i go back to my get user models not here where i imported this yeah this one i need to import this one here same here and we don't need this other stuff so remove we need only get user motor so right now i'm creating a new line here that will be up offer and this will be models and foreign key and this will link to our get user model and the default will be equal to one and on delete it will be equal to modulus dot set default that's right and i copy this the slide and i put this on my other model also into the articles we need to make configurations so we go python managed by make migrations and i do a migrate okay this i just i right now wrote the changes into my database so this will automatically assign the test admin user as an offer to article and article series because our test user has the id of one in the database because it's it was the first user that was created in our database the admin user of course but sometimes we want that at least the admin could change the author so we need to change what we display in our admin panel so we had our django main admin script and here i simply add another line awful awful and here near series we also add author nice let's say this and also should be still running that's nice uh let's go to our admin panel and let's check what we can see there right now and i go to my article for example and i open her husky and there is a offer appeared as a new option here and because i have two users one is testing this testing i can i could change the author of our publications but i didn't want to do this now so let's go to my home page nice now i'll record in the database that holds the offer and publication date we can display this information in the home page record and inside articles but first we do this change in our homepage template i go to my template uh where is my home page it's here templates main and there is a home home table name that's right here right after this link where here we start titles so above our title we want to modify this code a little so i create a new div that that class will be article metadata that's uh the that's kind of default bootstrap class and here i'm creating a link that will have a class that will be equal to m oh no no no no mr two then the link will be equal to it's a jungle way link of course it will be equal to url profile okay i need yeah url and we feed an object object author and of course we usually want to print this as a link that's cool and other thing we would like to add some kind of publication date of our alt article so i'll creating a new small class that will be equal well to text muted that's cool and here will be uh simply bracket and it will be object published this will give us a date when it was published but we would like to modify it a little so it will be more shown in uh this kind of format it will be month day and yeah of course and it is small so let's save it and right now if i go back to my home page as you can see it's amazing you can see right now it appeared here and this affected our homepage immediately and if i would click on this test it gives me a simple link to my test profile and if i go home uh it's home so right now if i press here it's also there on a deeper page and if i click again here well that's the place where we also would like to add this because if someone reads this article someone likes it so maybe someone likes to check who is that user who publishes description maybe in uh in a profile we can give more information what articles it published and so on so we could subscribe follow message and well we can implement many things that we could do with a user profile the overall view is getting better with each tutor so let's do a similar change inside our articles and to achieve our goal so i go here i simply copy this lines that i just created and i go to my article and i will do a very very similar change here just after this title i'm copying the lines i copied and this will be d flex it's like a flexible article and there will be um our true class you will link to the same profile object offer and we of course like to type when it was published so we add additional string here that it was published on this date and if i press save save ship and if i hit refresh as you can see it appeared here that's nice right now we have a user and when it was published that makes sense so if you go on your website and you might check this and you should immediately see these changes might you see how easy it was so i go home and that's it so let's log out now and let's go to home page and as you can see all the changes still the same and if i press to view the test profile i can't modify it and i see all the information so we can add more description here we can change the email we can add an image but this for next tutorial and great we quickly implemented a user profile into our django website now it's way easier to continue building on a current code so you can add more fields to a profile now each user will be available to add their own publication and etc even you can start building some communication between different users or comments section you may notice how each functionality can expand and help us to continue to grow our website i don't know how about you but i really am excited about that and that this is why i really like junk uh please like this video share it subscribe my video if you haven't yet this should really help me to grow in the future and even thought now our website looks way better than in the beginning but still we can make it way more attractive by adding some images to our publications if i go to home page there's uh we would like to see these cats for example what is cats maybe there will be a different name or title of this publication not mentioning exactly what it is so maybe it would be a good idea to see the image of it so the this is the goal of my next tutorial to check this out and how to do that and bring this for you guys so in the next tutorial i'll cover how to manage image uploads on our website thank you again for watching i hope this was useful for you and see you in the next part see you later bye
Info
Channel: Python Lessons
Views: 4,712
Rating: undefined out of 5
Keywords: django tutorial, django project, django custom model, django user profile, user dashboard, create user profile in django, edit user profile in django, manage users in django, software developer, python django tutorial, django tutorial for beginners, customize user profile, python django user profile, python django user registration, python django user authentication, django user profile page
Id: q5oO9XB6Vrs
Channel Id: undefined
Length: 25min 14sec (1514 seconds)
Published: Sun Jul 10 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.