Creating Customer Profiles with Django Signals | Django (3.0) Crash Course Tutorials (pt 19)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys so in the last video I just introduced you guys to Django signals and how those can actually save us a lot of time and make our project more dynamic so if you haven't watched that video I highly recommend you check that out before you get to this one if you don't understand signals yes so what I'm gonna do now is actually just go ahead and remove this process here so when the user registers right now our form gets submitted so on a registration page and we also create a customer here so in the last video we use profile but now we're gonna revert back to customers so what we want to do now is actually take out this logic and allow our signals to handle of that and we also want to remove this group logic here too so we want a user to now have a group and a customer on create so what we're gonna do here is it within our accounts project let's go ahead and create a file called signals dot py so if you want to make sure I spelt that right and we're gonna go ahead and write all over logic to within here so within signals I'm gonna go ahead and copy and paste some imports because you should be familiar with these if you watch the last video but we're gonna import the post save method and we're also gonna import the user and from models we're gonna import the customer so from models import customer so now what we're gonna do is create a function called create customer profile so actually we'll just do define customer and we'll just do underscore profile so this will just be responsible for creating a new profile and I'm gonna pass in the sender the instance the create method and again we're not going to work on the update method this is just gonna be the create method I'm trying to simplify this one part and we're just gonna do star star kwargs so you want to make sure I spell that right and now let's just do this so if creative let's go ahead and write our logic so from here if we look at this we want to first get our group so automatically when a customer registers we're gonna get that customer group so we can remove that and user groups add so the user is going to be the instance so let's just do the instance dot group star add and now for the customer we need to create the customer so we can just go ahead and remove all that and I think so far so good here so let's go ahead and put this down here and I don't want to put that in twice I just happen to copy it because I was deleting it and let's make sure our indentation is right here so we'll go ahead and just back this up so customer objects dot create and the user is gonna be the instance and instance dot username so that should take care of it and we're gonna use that post saved connect method so I don't really care to use the receivers at this point posts save or yeah post save and this for some reason makes more sense to me than using those receivers connect and we'll do this on will actually pass into customer profile and let's go ahead and make the sender be the user so I'm gonna go ahead and review my views here so the users passed in and the signal is cold so this function will run and it should add our user to a group and create one so I'm doing this on the fly so let's go ahead and see how everything's working so let's go ahead and register a new user and we will call this user David I don't think I've used that and we'll just do David at gmail.com and give David a password let's go ahead and verify that password and I think I'm supposed to print something out I want to make sure I have that so let's go ahead and print customer was created or profile created so I want to be able to see this profile I can't spell here it's a profile created and let's go ahead and save that and now if I register this user there we go did that run or not let's see say that looks like for some reason oh it wasn't connected because I completely forgot about that apps config file so if we go to apps right here we need to go ahead and override that ready method so I'm actually just gonna go ahead and write that out here so define ready and we'll pass itself and we'll do import in this case we're doing accounts so our app name is accounts dot signals and based on what I talked about in the last video because of the way I set up my apps file here let's actually refer to the config file here so let's do accounts da apps dot and then whatever this config file was again I talked about this in the last video but for some reason this gives us a it just doesn't connect the signal so let's just go ahead and run that full import and if I go and check this out it looks like it's still good and there was a few things that I missed here in the signals up UI file which is importing this group model here so we're adding the user to the group right here so we need to reference that and this needs to be created actually because that will give us an error and we have our print method and let's go ahead and give that a test now with David one here let's actually go ahead and call this David one and create the email David one that Gmail and go ahead and confirm the password and there we go so profile was created perfect so that's exactly what we needed and if I log in as the super user in the admin panel we'll probably notice that profile I just want to make sure everything was created so if I go to customers there we go David one connected to that customer and also David was also connected to that group too I think so we need to make sure that that was done and that would be in our user so David has the user not the customer let's go back here she users if we check out David one here go down to users there we go David is now a customer so we went ahead and removed all that logic from our view now and sent that two signals so if I actually created another user from the admin panel I wouldn't have to rewrite that logic in because I have signals listening to that so the senders and receivers are communicating and that's the beauty of using signals so that's it for this video in the next video I'm actually gonna cover sending email so we'll actually create some kind of contact form and then we'll follow up with that one kind of like what we did here where I will create a password reset method where a user if they forgot their password can type in their email and the password will be sent to them or their link will be sent and they can go ahead and resend this so make sure you subscribe and thanks for following along with this series
Info
Channel: Dennis Ivy
Views: 33,730
Rating: undefined out of 5
Keywords: Programming, Software Developer, Dennis Ivy, Dennis Ivanov, Django, Python, Django Signals
Id: jYzTKcvO0Pk
Channel Id: undefined
Length: 7min 29sec (449 seconds)
Published: Mon Jan 13 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.