Try DJANGO TUTORIAL Series (v1.11) // Learn Django Version 1.11

fSo the reason that we code is to solve problems right that's why I believe that we code it we find a problem that we want to solve and we use technology and software to solve that problem And

Now one of those problems happens to be building web occasions that is how do we build web occasions in a fast and effective way now I believe Django is the best way to do that Django is written in Python which means it's a lot more approachable just because it's written in Python for beginners but then it's also powerful enough for the experts I mean a site like Instagram still uses jangle has their back and like all of the things that Instagram uses has Django at its core that's amazing right so it's definitely got that scale but at the same time since it's written in Python it's a lot more approachable than a lot of other frameworks because Python is a lot more approachable than a lot of other programming languages of course that's my opinion but if you're watching this you probably maybe already have a sense of that right so the reason we learn Jane you know is to really solve these software problems that exist on the Internet right so if we need an internet presence we can use Django as that back in technology to really solve a lot of these problems and they might be small ones such as hey I'm really picky so I want to share you know certain food items with my friends so they know what I'm picky about right and then my friends can share that with me so if I ever pick up food for them I'll know exactly what things they like at different locations and that's actually what we're going to be building but there's other bigger problems right like how do we do e-commerce how to actually build a business online and then further than that you know how do we do matchmaking how do we actually build them All of these things start to come up as possibilities of what you absolutely can do with Django of course you might be able to do it with other frameworks as well but like I already said Jane Doe is one of the most fundamentally easy to approach that's what I believe and that's why I have created so much content around Django now this series is called trying J No one point eleven this is really meant for those of you who haven't really worked with Django before even if you've worked with older versions of Django you're not really that comfortable this is a really good series for you because it will bring you back to the basics back to the core of what Django is and teach you some of the things that are new in the version one point eleven but for those of you who have followed us for a long time the changes are minor most changes in Django versions are not huge especially for beginners a lot of them are big like if you're more advanced user you know what those changes are and you probably don't agree with me that they're not that big but by and large we have been doing this a long time the core of what change is is not that much different so get ready and gets to the get started with trying one point eleven money was just an I'm going to be taking you step by step through this entire series and yes we have all of the reference code the things that we do all online for your use as well everything about that will be mentioned in future sections but just keep in mind that this is a high level high energy you know we want to get after we want to build something real and then get it on line and get our projects and our solutions out there faster and better using code thanks so much for watching So there's a few things that you want your Web cation to do One of them being understanding your L

So when you go to your L It understands that so if I click on Random User or if I click on a register it actually understands what is going on there and so this is an example of that it understands the your L's in it displays some information and it also displays information that saved in a database fright such as this user this is my user and you can actually check this out I actually have more stuff on here these are things that I actually like at this actual restaurant and these are items in there that I that I like picked out on my own Now if I click on one of these items I actually see a search specifically for those items now you'll see it in action when you go on there but it actually does a little bit of a search so that's kind of cool so I can actually search like pizza right now it's actually showing nothing but you know deadline it might actually show something that's what's kind of cool about this this site members thing the nice thing is we actually do registration so we show you how to do your own customer's attrition complete with email activation and everything much like what you see here and also authentication that is actually built into Django now so you can just use their stuff but we do our own registration process and we really just go through all of these things with you it doesn't look that fancy that's not really the point the point is how effective it actually is and where you can take a lot of this information and where you can go with it OK so the other part of this is to know that all of this code is available on our GET THIS IS THE your to it or a shortcut your real to it if you go to join see if the dot com slash get hub And that is another short cut your will but the other one actually takes you to this you're all right here are the tri Django one point eleven now one of the things that we do as We are creating this project we write something called code you're probably already know this but that's essentially what we're doing or writing code in on this good hub this actually shows the version history of everything we've done so right at the beginning this is the code that is represented at that time right so at the end of that video that's the code that it looks like and you'll see that throughout the way and obviously if you have questions on the code itself like while you're writing it you always are going to want to jump into this get hungry positon there's something we always do now it's really important to note that using get hub is optional you don't actually have to do this you can literally just watch what we do in the video to get the same result but get hubs really useful to compare your code against what we actually do in the video and the reason it's not like under the video or some like that is because there's just a good amount of code and since we're building something real this is how you would actually do it in the real world so if you were actually building a real project that you wanted to share with people and stuff like that you would probably use something like get it so that is the entire walkthrough of this project I do recommend that you check it out now to get a calm is a real Your L and Yes it is a real H

P's site too I can't say for sure how long it's going to be real but it definitely is now and probably if you're watching this it's going to be real too and will probably do some upgrades and all that and of course all of those upgrades will be added to the repo as we see fit so stay tuned with that thanks so much for watching and please let us know if you any questions or comments below and also consider subscribing to our You Tube channel joints if he dot com slash You Tube So now that we've kind of seen where the project's going to end up it's time that we actually get started with that project now there's Several things that your going to need to do prior to really going to that next video or even continuing on with this video Luckily we have a lot of sort of references for you so you can get those things going on your own without necessarily watching a video and there are a few things that we need to discuss Now if you've already worked with us before you can actually just jump in to create a blank Jango project right here and you can just go through that that's all we're really going to be doing and I will say that we are installing Django version one point eleven which is right here so that's for those of you who've already worked with us once you've finished that you can actually go to the next video because this is for the people that who haven't worked with us so who haven't done a general project with us yet and aren't really familiar with our process so a big thing here is we will reference a lot of different sort of things throughout the video that you might have to pas the video go do those things and come back this is a really good example of that now this UR

L Right here is a redirect to this guy this guy is made as your referenced to setting up your project now we're actually going to do this on the video so you will be able to see it but if you're one of those you know I don't need to watch the video for everything types you can possibly do now jump in and take a look at this guide in particular and go ahead and just go through it so a couple things that are required of you to actually go through this series one of them being that you already have windows your installation done so if you're on Windows or Linux make sure that you go there and do those things another thing to note is we actually have a little link to our tech this is the technology that we use a lot so if you want to look over This we will be updating it so give my idea of these are the things that we're doing consistently and constantly in our projects so here's another for you to check out and then finally we have a youtube channel so if you're not subscribed to You Tube already make sure you go here to subscribe just click that subscribe button press a little ding to make sure that you actually get the notifications fright you want to make sure you do all of that otherwise you won't see when we post new stuff And for this project we're going to be doing is we're going to be using Django project Django of course is a web framework written in Python so this is what's going to be handling all of our web development we're going to be working with a ton so this is something that I highly recommend that you jump into learning now especially with the coming wave of Python powered AI Yes Django is powered by Python so Python dot org is where you're going to go for that and we're going to be using Python version three Python two is kind of on its way out so Python version three is definitely the one you're going to want to do and that is three point six and beyond if you use a version different version of Python three should be OK as long as you're using something around Python three point four and above anything below that is going to be well it might have some challenges and if you use in Python two point seven most of the things will probably work so if they don't work it's likely related to Python two point seven and also a different version of Django So if you actually divert from using jingle one point eleven do that at your own peril I do never recommend using a different version of Django in the video so always stick with any video we're doing always stick with the version of Django that we use in that video even if it's an older Version of Django especially if you're a beginner if you're a beginner you're trying to learn how jingle works and Django version one point eight is works almost identically to version one point eleven of course there's exceptions to that but as a beginner you won't notice them I promise you I've been doing I've been doing this for years now over six years and it's like something that you'll notice there's there's not huge changes in that and that's true of Python as well there are some minor changes that are really good and those are the things we'll actually talk about very early on in the series next we're going to be using get bootstrapped dot com This is our front end web framework this is going to make our site look good it's not like angular or something like that it is just really so much of it is about H

TML and CS

S So if you're not familiar with those concepts I do recommend that you check them out more but we will do everything step by step so you'll see exactly how to implement this in your project even if you don't really know HTML

and CSS but Again you're going to want to get familiar with those things because they are fundamental for web development and those specifically are for web design finally we're going to be using sublime text as our text editor this is where we write our code and all of this code will be available online for you to see we'll talk about that later but basically you're going to using text editor similar to like you would use Microsoft Word to write a document your view sublime text to write your code it just makes it easier now if you go back to our tech post you'll see that there are absolutely other options for text editors so don't worry if you aren't using sublime text you can use whichever version you like we just use one takes because we like it a lot OK So without with all that stuff out of the way let's actually get our project started and we're going to be going off of getting or creating a blank jungle project here now again this is a reference is And you can use all the time going forward and we'll do our best to update and upgrade it based on your comments on this page itself so going forward this is what we will reference fairly often in starting a new jingle project now of course if you've learned and have are very comfortable with another method of creating the junior project you're welcome to use that method although I recommend that you do this method it's by far in my opinion the easiest way to actually starting the jing a project and keeping everything organized because once you actually do a lot of different code projects keeping things organized becomes so so critical OK so let's go ahead and get this started again I'm assuming that you already have your computer set up whether you have a windows mac or Linux I'm assuming that you already have those set up otherwise you could if this is just really really not going to work so I'm going to go ahead and open up my terminal and open a new window in my terminal here and I'm just really following along with what's going on here so I'm going to just close down that

A little bit so we can see that OK and close down my terminal a little bit to rights in our side by side just going along with this so do cd into my local user so I've got my local user here this is where I am and if I a list everything else I already actually have a folder called dev so I'm a just change into dev and if I list things out in here I have a bunch of projects that are already in here right but I don't have my newest project which is try Django one point eleven but what I see here is I have see if the home and see if you home so I'm basically saying let's make these directories but as you see with this I already have these directories see if he homes here and I do not have tried doing Just go ahead and do make sure try Django and I'm going to do one one one or one dash one one and that's because it's trudging version one point eleven let me go ahead and hit enter and I'm in a change in to try Django one dash eleven am going to create our virtual environment in here so virtual Ian V Desk P Python three Enter Now this is very important again we're using Python version three so Dutch P Python three allows us to create a virtual environment with Python version three and that trailing period puts this virtual environment inside of that folder we just created or the directory just created so I hit enter and it's now creating this new virtual environment for me again we're not really going off of script here this is really following along with this guide so we're going to go ahead and do source Ben slash activate And there we go we've got our activation going here and this is now a virtual environment so if I actually do Pip freeze I'll see that I don't actually have any installed but if I open a new window with command in a new pip freeze I'll see that well I don't even have PIP as a command on my life old computer right now obviously yours probably won't have that it will actually probably say something but in my case I just don't have PIP installed on my regular computer so I do have my virtual environment stalled and Pip freeze is not showing anything installed so now we're going to go ahead install Genco so Pip install Django equals he goes to one point eleven point two which that is the latest version of Django if we go to the Django project we see it's one point eleven point two and one of the great things about using Django version one point eleven is it's called an L

TS release so if we scroll down to their release schedule we see the different releases that they have so LTS Meaning a long term supported so it's going to be supported Intel twenty twenty twenty seventeen Right now is recording this so that me is going to report supported for three years were other versions of Django are going like defunct So they're not going to be supported any more that doesn't mean they won't work it just means that they won't get new security upgrades or bugs fixes that that's it I mean they'll still work you can still actually install these things even one point three not that a hood recommend using those in production but as you're learning yeah go ahead use old ones all you want OK so going back to the guide we are now going to press Enter uninstalling jingle one point eleven point to a new installation process it's actually showing us progress in a different way than we've seen before that's just new it's a one point eleven we've got J

One point eleven is now installed so if I list everything out of still only have one virtual Vironment stuff don't worry if your virtual Marmont looks a little bit different but you will want to make sure it's activated showing this right here so now I want to do is a make an S R C Directory or source so make S

RC and Change into SRC

And now that we're in here this is where our general projects going to live soldiers stooging a dish admin Start project and we're going to call it try Django one eleven again and put that trailing period again because we want to put it in the general project inside of this source see folder I press enter notice that the jangle project it you can actually name it this you can't put a desk in there right it's important for you guys to see errors happen because they happen all the time and they're pretty normal and a lot of a lot of times they'll actually tell you what the error is in this case it's just saying it's not a valid project name to pick a different name that's valid So what I'm do is Start a project and I'm going to call this movie Because that's actually going to be the name of our project movie picky as in very picky because what we're going to be building has to do with that name as you probably have already seen in the walkthrough so I'll call it movie picky and then put that little period there hit enter and there we go we now have our genome project as we can see if we list everything else OK And of course if you're a Windows DI

R Is how you list everything else so we've got our project going and it's actually now created so Step four I'm actually going to use sublime text for I'm not going to actually go through it on here but I will reference it still so let's jump into sublime text and I'm going go ahead and go to project and we'll go to save project as an oh go to my recently created project instead of users inside of of inside of we called it try doing a one point eleven and this I'll call it move picky And we're going to go to project and fold a project Of Treasure one point eleven open OK so now we have our projects set up inside of sublime text so it's going to our source folder into movie picking this movie picky folder is the main configuration folder and you know that by setting stopped by now if we called our project something different that is what's going to be here it's wherever settings up high is that's the main configuration folder and the root of the general project is where managed upaya is so if I close down movie picky I see managed up here that is exactly the root of the project and the main project configuration so settings up high is where we're going to work a lot but going back into the guide let's go ahead and break this down a little bit

Going back into the guide we see that we actually Create a new settings module so this settings file we want to turn it into its own folder and then turn it into a python module so we can have a local settings and a live settings local being our own test development stuff live being what we put on the actual server they are much different all right so let's go ahead and do them I'm going to create a new folder in here and it's going to be called settings so just like settings stop Hi Here I'm calling it settings and inside the settings we're going to put an init file here so in it too underscores i N I t two underscores the P Y and Of course we're saving it right inside that settings file there so go ahead and save that I can actually close that out but now Python treats these to the same church Python treats the Settings folder and the settings dapat I file the exact same so it's going to go off of one of those so what we want to do is with our old one we're going to call this old settings up high so old underscore settings dapat I There we go and do note that Python uses underscores a lot and sort of a dash there you go use an underscore OK so old settings up by Disney is literally the old settings for a project so we need to do is make sure our settings are still inside of our project so let's go ahead and make a new file inside of the settings

Module has been and will call it based up high and I'm going to copy all of those old settings over save that and There we go so now we've got our settings module working with one exception are in it up guy has to have this being called so I'm going to do from base imports all OK so now they're identical so the old settings is I didn't To the new settings so in it up high is importing all of the things from based on PI So this is roughly the exact same now there's a lot of different ways you go about testing this but in this case just come take my word for they are exactly the same except I need to fix one thing here and that's this base dear so this based or is related to the base of the Django project where minutes of pious but if you know S Path you know that this is going inside of the configuration folder because we moved it one level down so I'm going to go ahead and copy this pasted over And that we go now they're identical literally identical to what it came with originally to now what it is again this is about separating our environments making sure our environments are working correctly OK So inside of the guide we also had one for production and local some to go hang copy these and bring it into the init file here and production and local are two other python modules as top secret so instead of settings I'll do production I and then I'll do a local

OK and I'm literally going to eventually copy everything over to local in production but for now I'm just leave them blank to illustrate the per the point that they're not necessary right now and also based up high is really good for us to make sure that that is actually working in and of itself so in it is still working it's still going to try and import all of those things but there's actually nothing in them so we really ought to worry about a thing all right so we can scroll down here now and notice it saying copy Local Settings pull blah so we're not actually going off of that but we will install some of the other recommended installations for going live I'm Going to go ahead and come in here and do Pip install just a few of these things for the post Krzyzewski Well database which is what will end up using We're not using the My SQL Database we're going to be using the postscripts S

QL Database live once we go live which you'll see what I mean when we get there and then an install these a few other these things let those do the installations and then we're going to create a requirements file so requirements file means everything that is related to this project in particular Ok so we've got the most of the install done some to go ahead in copy the pip freeze go back into our terminal place that in here it's creating a new file there noticed this file is inside of where minutes up high is so we go to requirements the TXT

These are fairly common files that you use or actually installation versions of this project so that's what the requirements Is you'll see this on a lot of Python projects is not necessarily just Django and this is the reason we use virtual environments right here is Notice that these are the only things that are installed inside of this virtual environment and you can actually look at the library as to where they are installed by going into the Python version or see me live Python version site packages and you can actually see this is the code that's actually installed so if I click on this this is showing me the Django code so do you tills this is the J O code right there it's actually on and installed on your virtual Marmont it is on your local computer so that's something also that's really cool about requirements TXT OK So we now have our general project set up so let's actually do something with our genome project I'm going to use command K

To clear the out and do Python and choppy why migrate hit enter And I did do this without verifying but I am in where managed up Why is so I can actually run this command OK so now I have actually set up my database so migrate runs and sets up your database so DVD

Sequel lite three is another thing that was installed this is something that Django used by default OK cool so we've got our database Let's go ahead and create a user here we're going to create our super user so create super user And I'm going to use the username C F E M A leave a blank email address and as I type my password is left blank and when using local you can use kind of whatever password and I'm a go ahead and run the server And were to go ahead and check out this server here inside of our URL

I run the server and it says congrats your power page is working that's awesome that's what we want to see and notice that this loaded a little bit so if I refresh in here it's actually loading here so all it's doing is emulating a production server and it's as starting development server so when you see development server that's a local server production server is when we go live but we have now created our project and it's now ready to move on except I did skip a few things and that was copying our settings to production in local So let's actually do that I'm going to just go into my based up by copy the entire thing copy and paste the entire thing into local and production so instead of production I'm going to change debug to being false that is one of the key things here is changing debug to mean false and you also might want to change your secret key you can just kind of use different words and or different letters in there and that actually will change it to really get a brand new secret key just create a new project and change out your production secret key especially we're going to talk about that later but now we have three different settings files one is called based up by one As local one is production local is for my local computer right so if I actually sent this code somewhere else I might have another local file and therefore would need to update my in it And then base stop by will always be there so I can make sure that base is kind of always local development settings and then production up by actually overrides all of my base settings and that's in it again we'll see all this stuff and much more action going forward especially when we go live which is something we absolutely will do so thanks for watching this one I realize we went through kind of a lot and didn't cover things in specific detail like what's going on in the settings like what does what do all of these things mean now all of that will become more clear as we go forward working in this project but also working in any of our projects we talk about all of these things too so don't worry if you don't know everything that's going on the settings or really fully understand all of things that we did now if any of this was confusing at all what I recommend is just quite literally deleting the virtual environment delete it from from your entire computer and start all over again and since you have this guide you could do it five ten one hundred times I've probably started projects and virtual Marmont well over a thousand times I'm not even exaggerating it's probably well over a thousand because it's something that I do every time I start a new project I give it's own virtual Marmont So every Thomas or GNU project whether it's for classes or it's for actual real live projects that I'm working on you do this same process over and over and thus there is this guide there for you that should be there as your number one reference to always starting a blank project All right so let's keep going One of the cool things about writing code is it leaves a trail right so there's this sort of history that happens as you create your projects and if you use get which is something we do you can actually see every piece of that history if you use it correctly so I wanted to bring this to your attention because we actually have a good hub repository It's right here on this UR

L and What you're going to be able to do here is actually review the code that we've created so you can actually go to this Ural or simply join see if you dot com slash good hub and what that's going to do is it's going to take you to our main profile page where all of our Republican words are so if you click our positive Henri's you can see literally all of the repository as we have so the one we're going to right now of course is trying a one point eleven but if you go down to let's say a student quick search for a try Jane you know one point ten and I'm to go ahead and click on that one I'm using this as reference because if we scroll down we can actually see the code history here so on this lecture code you have all this history exactly how that project looks at the end of the video so the video is called Make changes the models you click on it and this is what it's going to look like at the end you click on the SRC Folder all of those things in there that's exactly what it is so good it's actually very very useful and it's an important thing to understand that you can go here to compare your code against what we do so you can make sure that you get it right since there's so much code right in some cases there's not so much code and in those cases we make actual guides for them so one of those guys being set up get and a good

So actually understanding how to release your project on to get hub and a little bit more understanding of get this is actually a guide that you can use and it's right here to understand those concepts much like we did a guide for creating a blank junior project we have that just like we've already done and this allows us to see the code in action or at least the commands that we actually have to write but the actual code the things that were created will be on good hub and that's an important piece of your own reference as you're going forward all right so let's keep going Now that we've started our general project Let's actually talk about what Jane Doe is for or more specifically what it does in general as it relates to other websites So typically when you go to a website and say for instance I go to Yelp dot com What's happening here is I'm sending a request to a computer on Yelp dot com I mean that's a simple vacation of it but basically I'm saying hey yelp dot com Send me something back and then the software on Yelp dot com sins back some code that my browser reads and understands it right and that's how we get these Web sites working and that's true for pretty much every single web site it's very rare that it's not the case that there's some computer sending something back now the times that it's not the case is if that website just simply doesn't exist right if you just go to a website that does not exist then you get something like this like in my case I've got my Time Warner Cable or my internet provider saying hey that doesn't exist so we're going to give you some other page so we can sell some as to you right so in in the case of having an actual website the software on the websites computers also known as servers send back the data needed to show that Web site and smart websites that is the more advanced websites like what you can do is it actually remembers who you are right so when I went to Yelp I didn't actually sign in to Yelp I haven't signed in to Yelp for a long time but it still remembered who I was and actually remembers some of my preferences and remembers a lot of things that it might not even tell me it remembers so all of that remembering all of that smartness that is all handled by their backend system now in your case it's probably not Django I believe that Pinterest uses Django So if you did Petrus that would be another example when I click on here this is all Django powered now maybe not every aspect of it but for the most part it's shingle powered and that means it's doing something very similar go to Pinterest dot com and the Django site understands that I'm a Pinterest dot com And since been back some content that is related to just dot com and the fact that I'm not logged in it's also related to that our website couldn't Farnsworth dot com does the same thing in this case I am logged in but it sends back a bunch of information that's related directly to me all the things that I recently viewed or well did not view Are coming from go so it actually remembers all of those things and if I click on a project it will remember that hey this is the your for that project so we need to send back the data specifically for that your L So that gives us a lot of information as to how our projects actually work so let's go ahead and close these out and really quickly jump in if someone takes a kind of over or overview or outline all of this stuff so number one handles your L's right so any your Elsa like we said you know dot com or even coding for entrepreneurs

Dot Com such projects thirty days of Python I believe that was the your L or At certain days Python So it handles how this works right so without any extension or any longer version of the your L It knows how to handle those two things right so it returns responses returns responses and then it remembers things I mean that's pretty much how it works right so what we need to do is design our app store our projects to work like this and what does is it actually breaks things down based off of functionality so let's say for instance we have our accounts so like our user accounts that is called an app in Jindo Yes it's called an app so it's called the jangle app

Accounts is one thing so let's say for instance we wanted something like videos this would be another chain go up and if we wanted something like e-mail sign ups or newsletter our marketing or you could do analytics or you could also do like menu esteems Right so a lot of these things are all considered apps and we are going to explore these apps because what the apps do is actually handle these things with the help of Django but they handle all of these things right here so that's really how Jango works and it does this sort of bread and butter of handling Urals and doing a response related to that URL and Then it remembers things so you know we can have a better experience on that site and then it does this through apps and these apps are self-contained like modules that are inside of a jingle project so not to be confused with apps like on your mobile phone or your smartphone

It's not to be confused with that so let's go back into are doing a project and actually start our first app now what I did mention it was a menu listings so we're going to be creating a app that or excuse me we're creating a project that is all about like listing out your own pickiness at any given restaurants so what I want to do is actually create go into my projects so it's changing to dev CD and to try Django one eleven I need to activate the virtual environment Source been such activates And also don't worry if you don't have some of these things that's just things that we did off the video for reference source code that's all or forget how of life we mention in the last portion OK so now we have activated our virtual environment and we're going to change into SRC and We're going to create one of those apps so we're going to create one of these things that handles this stuff so let's go ahead and just run it will do Python a minute up here why start at and in my case I'm going to restaurants because I am going to have a general listing for pressure on some I go ahead and hit enter and now free go back into our general project we see this thing called restaurants and inside of this app we have several things here migrations in IT admin apps models test the use now the key thing here is is actually going to be views so this is where we create those handlers for our your L

So that's that very first thing that we're doing so I need to create a way to actually handle that and that's what we'll do now so let's just create a function based view so this is going to be a function based view it's not super important right now but it is something that I did want to mention and I'll just call it a home and remember how it handled that request right so when I go to a website it goes and handles a request so they Means I can pass in a request as an argument and I can return it some sort of response right so return some sort of response now this is called a view this is a function based few versus a class of a few but this is a function of a few so I need to be able to return some sort of response here and Django has a lot of built in things to allow this to happen and that's where this render by default Jango to shortcuts shortcuts import render is already imported here so I can return to render and render takes three arguments one of them being the request another being a template and the final one being some context for that template in terms of a dictionary will leave that context out for now but we do want to have some sort of template here and I'm going to use home dot HTML

As our eventual template before I even jump into the that's kind of getting ahead of ourselves a little bit I'm going to go ahead and comment this out and I'm import another way to have a response so from HTTP Import H

TTP Response And all we're going to do here is return

A sheet of response and we'll just say hello to as a string OK that's it we've got our first few in our first app some to go ahead and jump in to my you or else because I remember if we look back at this it says it handles your L's so I need to put this view into a your L So if we're going to stop by I'm going to import that view so we'll do from restaurants Doug views import home OK So as you notice as I'm typing it's going to give me some some things as like a predictive sort of text and it also gives me the definitions so the There are some shortcuts that my text editor allows me to do that's what those things are OK so I'm not going to really explain the Ural patterns just yet instead I want to just get this working so I'm going to put a dollar sign here and then put home here so I've got my Ural patterns and I've got one for home of course a lot of this references are already there and we will go into it more in the future OK so now I actually have a you're all set up with a view to handle that you're ill so again going back to this view we look at it it takes in the requests and returns a response very very simple we save that refreshing here what do you know hello is coming through and now it's no longer empty it's no longer saying hey the power page it's just saying hello That is the basis of it but there's really not much more to it in Genco than just that now the things that get a little bit more complicated is remembering things and then however remembers those things and then also how we can return different sort of responses so in the next one we're going to actually return a different kind of response based off of what we started talking about here so stick with us So we figured out how to handle you or else with a view we did a very very basic version of it so basically Django understands the your L

This is the absolute basic home page you're well again we'll talk about this a little bit more later but it understands your L and It sends it to the appropriate view because we did the imports we did all of these things that are necessary to make that view work so how do we send back something different right so right now or just say hello but how do I send back more robust HTML

Now what is HTML Exactly if we go to get shot dot com We will actually be using the

Quite a bit but what I want to do actually is view the page source so it under View developer View Source pretty much every major browser has some sort of development ability to do this so if I open this up I see that there is this dock type of HTML So when you use the web it comes to the browser as this as H

TML This is the language the browser reads the browser comes through and reads this code and turns it into this seriously that's literally what it does I'm going to show you that in just a moment by using their code we will cover actually implementing this but I want to show you that hey this is the basic HT

ML Code so we want to do instead on hello we want to actually return that HTM

L Code instead of just some plain text so let's go ahead and do that I'm going to jump back into view and I'll just say HTML

Underscore equals two I may use triple quotes here because it's a long string and we're going to use some of the very basic HTML Stuff so I'll actually just come in here and pasting these things so paste and head and close off the H

TML Tag and then open up a body tag close off the body and I'll just say H one has the world close off at H one tag and then I'll do paragraph text this is HT

ML Coming through Close off that P Tag all right and now instead of a low we're going to return this H

TML Again it's a it's a python string so it actually is a python string here that we are returning so we're going to go ahead and save that with quickly with command S Of course you could also go to file save and really come back into our project make sure our service is still running if you forgot about that you just do Python it up he wire on server

That's it and if your virtual Marmont wasn't activated hopefully you sauce reactivated in that last one OK so I'm going to go ahead and refresh in here and what you know some HTML Actually comes through so this is an H

TML String this is actual HT

ML Code and that's what we're turning So this is pretty cool and what I want to do is actually show you something that's new to Python version three point six And that's something called F Strings this is a cool little way to do string substitution So what I'm going to do here is I'm going to put an F In front of this and then I'll just change this to being HTML Vol i

And I'll just come above it and say Schimmel bar equals two F Strings we'll save that and we refresh in here and they're real this is F Strings coming through just another way to do string substitution inside of Python three point six very cool but this is also showing us generally how we can combine these two things and make them even more robust right so like right now this code is super super simple once we get it to look a little bit more like this hoof that starts to get very very complicated and we really don't want to have that inside of this view and that's why Django has something built in to take it out of that view that's where I wrote this home daddy shemale thing that is related to actual HTM

L Code or HTML

Documentation and then understanding how we can do this string some fusion will help us with that second part of adding in some context there so in the next one will actually add in a template into using this view that's what we'll be doing instead of just using this kind of our own built HTML Stuff if you want

Questions of what we did here let us know otherwise let's keep going Now it's time to turn our HTML

String into an actual templates an HTML Document itself so what I want to do is inside of my main folder where many top he why is going to make a new folder in here called templates

And inside of template some to make a new file on here and call it base that HTML Explain based on each mail soon but let's just do that notice it is a dot a shemale So that means it's an H

TML Document itself so going back into our view I want to go ahead and copy our each to mail that we put that we already have done and put in here notice that once a copy and paste it it does syntax editing for me right so this is actual HT

ML Syntax so therefore my text editors can actually edit this or validate whether or not it's doing correctly so for example if I get rid of this P Tag and write out just a slash it automatically does the PT

For me where when I did the actual view need to not do that right so I've got this beach base HTML Page ready so it's time to actually be able to use this particular template but before I do that I'm going to go ahead and reveal this in Finder or essentially open this up so based on the smell if I open it up this is what happens right so it's opening that file itself and it's rendering as we would expect any sort of each to my document so a static website you could actually just bring just regular H

TML Onto a static web site and it will work so just keep in mind HT

ML Pages are like the fundamental website so this is its own website right here it's not a very good ones on a very complicated one but it is one so you could actually use a service to put this on a server just Space Studies shemale and then it would be a webs But Jane you does it better it does it in many ways and it will do it in a much more powerful way so since we've got this template folder now I'm to go into my main configuration into base study shemale and I'm just going to go ahead and scroll down All the way to the templates

Item here so this template settings is how our templates are being rendered where Django can actually take templates and render him into something we'll see what that means in just a moment but the main thing here is Ders we want to change this or actually add to this where this new template directory is and that's less that path join And is going to be base dirt and templates Now the reason I know this is the way it is number one is from experience doing it so many times but number two based areas were managed upaya is right so the templates folder is on the same place as managed up high and then if I scroll down I can also look at this database file it's roughly the exact same thing and database that sequelae three is in that same place now learning more about O S Take some stuff on python that's more Python related stuff so if you are curious about this just let us know in the comments below but otherwise this is actually the path to where the templates folder is much like saying something like your path to templates on your own computer right it's much like that or it's exactly that but it's OS

's independent so this works on MAC Linux and Windows versus how other things work OK so now we've got this Templates folder to actually exists somewhere now inside of our project so going back into our views I can go ahead and I'm just going to comment out this entire thing when we go to Some agreement pasted below And we're going to delete everything except for I'm going to go ahead and cut out this return to render stuff OK So again we're still have our home view here the old one I commented out for this reason to make sure that our code is different we use this function called render which is built in to shortcuts it takes in the request as one of the arguments it takes in a template name remember we now have templates and it takes in some context to return a response now of course we don't actually have home done HT

ML As a template but we do have a base study smell so let's do that to see base study shemale And let's go ahead and take a look at our homepage now to refresh its template does not exist OK so going back into base now this has to do with how our settings are set up so if I go in to end it know this it's importing a few other settings files I'm actually common those out for now and just go off of based on will save the other ones later but for now just going back to base studies so we refreshed here and there we go we now see that each of our documents rendering much like when we actually opened it up on it's own The main thing here though is the file did not change like it did before so if we go back into that view before this HT

ML Var turned into what are there this string was right it did some string substitution So bringing this back in I'll just call this home underscore old This document actually put F String in here now let's see that again some and it changes back to being home and I'll call this home new will save it we refresh in here and notice that it does replace this portion that's how strings work it uses curly brackets and replaces it just fine so let's

Let's go back keep it at home old save it and go back into based on each e-mail and add another set of cruelly brackets around this this is now called a context variable so what that means is I can go into my views and use this dictionary here a python dictionary and put it in as context Variable And we refresh in here that still showing the old one so we save this I want to save based on a shemale to refresh and now it's showing this is a context variable coming through OK so let's change the context variable to something else to true And this is true coming through right so it's actually going off of whatever we named here this is replacing this much like F Strings but it's not quite the same because there is some something called logic that actually goes through with the templates that F

Strings don't have We'll see what that means in just a moment but the main thing here is this is what does it takes in a request it takes in some template and it takes in some extra context there and then it produces a better result right so what if I actually did something like import random And I did a random image or so all just say no equals random Rand ints between zero and let's just say some huge number OK so I'm going to just add this in as a context variable so the key is numb and that's the actual context very old self and then the value is whatever value we set so we're going to use Nom here and then back into my base I'll put another paragraph tag here and we'll just say random number is double curly brackets save it refresh Not coming through so let's make sure we save our View Refresh in there and now it's giving us this random number so this is something that is unique to how Django actually works now this random number portion with the melt would actually work these would work in both places that is if I went like this And said Gnome and just copied this paragraph and called this gnome this is a random number Those two would be roughly the same and still work

But again the key difference here is that we are not doing the best thing possible given what we can do with context like this true value here that's something that will show you in the next one but this is how you render Django templates and this is how you just render standard HTML Of course there is absolutely a way to just turn this into a document where it opens it and does all the stuff but there's really no reason to do that you might as well use the built in Jingo stuff or it's in the next one will actually look at how we can adjust context to show different data in our H

TML Document or inside of our template so stay with us OK so now that we've got this we're actually going to go ahead and get rid of this home old we no longer need that because we're not going to reference it anymore now what we're about to do is understand how context works a little bit more inside of templates so you can see that in particular verses how this home old stuff works we're really just abandoning this this was really to show you illustrate a point not so much to stick with it right so it's going to or that and we're now just on home so we've already seen that we can use some context variables but something that's really cool is inside of our templates we can do all

The stuff so many go ahead and say curly brackets percent if HTML Bar curly percent curly brackets and then will do and if so this is a conditional you can actually use conditionals inside of Django templates so that means that if this variable equals this is another way of saying equals to true then we can actually render this stuff out or whatever this is so I'll just say for instance if a rule underscore item we'll just call it blue item as and boolean item and I would change a similar bar to Buda an item here on a save both pages so if boolean item then it's going to renew that number and of course I didn't change it to fall so if I refresh that boolean item is still there but notice that it says the H

TML Of are coming through just went away it just said this is coming through so that original variable there's no error that ran it just isn't rendering it right but it does go away we'll show you how to bring it back in just a moment but let's go ahead and actually show you how to go false so now if I say that view and then refresh on the stage to Melle that random number goes away so this is how context actually works I can actually change what's inside here and that will actually change how it's rendered on the template I'll say that again I can change anything in this context and as long as I'm using those contacts variables Slike this I can actually change how they're done so this is a variable This of course is a conditional and we can also loop through things so we can actually use a for loop in here as well soldiers say for some item in some list In for OK so like conditionals whenever you use a for loop whatever you open you must close so let's go ahead

And look back into our view and I'll just say some list equals two Well I'm going to say no and then I'm going to make another random number and another random number so some list is going to have a bunch of random numbers now I can add it into the context This context is starting to get a little bit long some to go ahead and cut that out and just say context equals to that Dictionary OK Space it out a little bit and then put context in here OK so my song list is going to be in there and we have it on based on HT

ML Notice that it's here so some item this is an arbitrary variable name that we give that only exists inside this for loop so say some item here I'll do it here and then I'll even put another paragraph tag and say some item is and it should be blank once we actually render it which will seem but now we've got this I'm going go ahead and just put a brake tank here we save it refreshing here and now if I refresh notice those numbers are coming through there's there's the random numbers in there and so item is blank so it's actually not showing as we sort of expected so this is some of the valuable things with these templates right we can change the context as we see fit it's a really really useful thing now of course we haven't remembered anything yet we're just kind of coming up with a random numbers and using random different variables and all that right so I refresh and they're still showing us all of this stuff and there's another thing that we can do in that's called a template filter so what I can actually come in here is say if let's get rid of this action curly brackets there it's kind of an auto complete thing so will say if some I Again this variable right here with a pipe straight up and down line and we just write out divisible by

As one word and will say two inside of Quotes if it's divisible by five to put an end if there again once we open a condition we must close it they will say even number save it And we refresh in here Even number Cool so as far as these sort of built in feet

Template tags and whatnot you can actually go to gentle Project dot com and you can see something like a template IE In I would recommend kind of playing around with this on your own to see how it is this stuff works now there's all sorts of reference there and we definitely do a lot more of it on our projects on joint sci fi dot com such projects the ones that pretty much focus just on Django OK So another thing was I said this is just kind of being empty right so there is another template item that we can use or template tag called ver they'd I'm So verbatim and then we have to end it so we'll say in verbatim that means that I can actually render out my variable items here right so fight for some reason needed to have them show up they will in fact I can do the entire page like this so we'll do verbatim at the very top and then verbatim at their very end and we refresh and now it's showing me in the actual template itself the raw template itself vs She actually rendering out that context so something that's kind of cool dunno if for whatever reason you needed to write out some code like we have right That's a way to do it cool so again this is some of the basics and using template tags and template context and if you do want to learn more about it there is a huge a long reference on built in template tags and features we will be doing some more stuff but some of the schools comments by seeking comment things out and leave them in the template tag there's another one called cycle which I would recommend you try out we already did for a loop in there so it's just around playing around with those things to see exactly what it is that you can do but this pretty much covers a lot of what you may end up doing on your actual projects now I will say too that you shouldn't have too much logic in here right the logic should actually exist in this view so for example the number coming through what I would say is if instead of having the ball item here else let's say condition

Rule item equaling to true And if condition Then will say no equals to that number on me show you this second Otherwise we'll just say none OK And I'm to get rid of this item of context variable so if conditional bull I don't mean here this is where I'm saying the logic should exist and here go back into the base and now what would you say is if numb so if the number is not none basically And we get this error here and argument must be a string so let's go back into our view so I changed numb to none so that caused this error right here so this has to be a number so I'll just put it as some random number for that list item there so again we'll just come through here

You know a little bit cleaner refresh and there you go so now it's showing us this random number now if I change this to being false There we go so that's where the logic should actually exist in the view function itself not so much in the template itself but again this is a lot of playing around with it and seeing how all of these things work now if you remember back to how we first wrote it we had homebody shells are template name not base so we're going to do in just a moment is actually talking about template inheritance and how that works Stay with us So now we're going to do is talk about template inheritance and to best understand this it will make it easier for us to have multiple views so I'm going to be used on pie inside of course my restaurants app and I want to go ahead and copy home few more times so we're coming here and now we have home one two and three Hoops and there we go and they want to go into your L's and we're going to import those other ones so home to home three and now we're to make some New York else I'm going to copy the one that we've created and I'll just say home two and then home three and change of use and here now these aren't practical necessarily but they are or will be using Make sure you have that column gone OK so we save this and we save our views now if we go into our page and we go to Home to it shows us a different one we go to Home three to chose a different one right so it's three HT

ML Templates that are being rendered now what it can do for each one is make a new template so let's go ahead and do that I'll just say home to Don HTM

L and Then Home three That HT

ML and Will keep the other one base so back into our views home to is now home to and then home three is home three and then home two and three will just go ahead and copy base bring it in save it and bring it in save it again and this time will just say home to hello world or rather just home to him to get rid of a lot of the text that's in here say that home three same deal get rid of the text and just put home Three back and I view I will get rid of a lot of the context we don't need it because this is about template inheritance so is going to read the context And now we've got home one has contacts two and three do not so if I refresh in home three I see that it says home three two and one hopes actually one is just one OK So we've now have a couple things that we just learned number one critic new pages is super easy right we just add the trailing slash and then we add a slug or a basically a page that's ready to be easily use that is it doesn't have any spaces in it that is not a valid your L

If you want to have separators issues a dash not an underscore you could use a period I wouldn't recommend it I would use a desk and all or cases OK so we figured that part out and we showed you how easy it is to have a new views form right and we also showed you how to change some templates in there cool very easy and very achievable if you ask me it's pretty straightforward I mean we already figured out how to do one now we've done more advanced ones but here's where things fall apart if we look at a study to meld home two to each email and home three donates to mail we actually end up repeating ourself a lot and if we start Using any CSS or Anything inside of this page

It will really fall apart on our brand stuff so let's actually give an example that I'll just say another age one and I'll say movie picky dot com OK so I save that and I refresh movie piggy dot coms on that page but if I go home too it's not on that page so I actually want it on both of those pages so A way to do it would be to copy this and paste on this page and then we can repeat that process for every page going forward but if you want to know something that's really cool about the philosophy behind code and software it's called dry that is do not repeat yourself or don't repeat yourself as a contraction to make it that nice dry acronym OK So that means that we don't want to repeat ourselves or we want to limit the amount of review repeat ourselves as much as possible in Bay study shemale home got each home to to Israel and home we actually were Peter self a lot because we have first of all the brand but also all of this stuff so if we any CSS or Any other sorts of things that are related to the metadata we would have some issues and that's where template inheritance really shines and that's where it comes in so instead what we're going to do is I'm going to keep this doc type H

TML and I make a new template in here called home daddy to mail And what I want to do inside a home is I'm really just going to have all of the content that's literally just related to a home got H

TML So I just put it in here I want to save it go back into my views and go home and change based on a shell to home to the shelves back to that right so we now have it exactly We where we wanted to be so we save that I refresh my home page and it's actually still working if I view the page source I will see that it is rendering out the to melt Now this is not properly validated H

TML but Since Chrome is an advanced web browser it knows that this is each mail so it will render it correctly it doesn't actually need it in what I'm saying is it doesn't need the doc type HT

ML Stuff to actually render HTM

L but This is invalid HTML

Itself so how do we fix that well let's go back to the base station mail and we're going to use a template tag called block content and then we do in block content And what we want to do is put all of our content inside of this area whatever this area is we want to put it in there and to make it even more clear we'll just put it did class called content Or rather declasse container so this is the main container that's going to hold all of our content so since I've got this I can now jump into home dot HTM

L and Use another tag called extends and base studies to Mel so extends a nother templates and then we are going to go ahead and do block content around our content and then In block content we say that we refresh our home page and now it has movie piccy dot com there and if I look back in our source I refresh notice that all of the other stuff is coming through so basically what I did was or what Django did was it took in only this stuff it got rid of all of the template tags and replaced them in here so let's actually look at it with what I mean so if I say hello there Exclamation mark and I call this block content to we say that I refresh air now got rid of everything in there says hello there that's all it says but let's go ahead and copy this again and then put block content below it And notice there's still stuff inside a block content I refresh in there it goes away but the content actually comes in so it's it's taking this block out so it's like I'm cutting this block out and then pacing it with this BLOCK OK So this is going to you're going to actually play around with this to see it work now what if you did have some continent here that you wanted to include inside of your content to get it going to get rid of this but what if you wanted to include that hello there instead of home got each smell it's really simple you just do two curly brackets blocked out super We say that a refresh and hills actually have hello there in it we say that based on each Mel it's only there once and I can move it anywhere and I can put it in another area and so on right so I can still use the content that's in there but this is called template inheritance This allows me to actually change the layout or the content that's actually inside of any given template and then using the extends call allows me to go there right so and home to daddy shemale I can do the exact same thing so the main content that I wanted was just this home too so I'll just say extends

Based on a shemale and then block contents and then in block Now you don't actually have to write content but you can we put that home too so we save that and then we go to home to and now there it is really picky is still there And we have we can actually look at home to source as well and it's showing the same stuff notice it still has container in there OK So now that means I can make some medication so let's just do that I'm just going to do some basic navigational Ziz do a treath to the home page of the say home and then a traffic to the home to page and say to So there is some Urals there now let's say that refresh Notice it says home in two so if I click on home it takes me to home cook on two takes me to their goal so we've got a very functional page now and it's actually working because it's replacing this little block here with the templates that we have decided so let's actually name these things as they would I'm actually in to get rid of a home for him in change it to being called

Contact dot HTML I'm to get rid of home too and call it about done H

TML And I'll just call this put in home to all just changes to being of the out And I'll copy this and go into contacts and change it into being contacts All right so far so good and now since we've changed our templates let's go into our views and change them we call this about change the context to being about and content or contacts

And contacts that we go finally are your L's when you change those as well so say about an contacts so changing these about contacts and about save it refreshing hair and less change or your L's on that base station Mel So this will be a balance call it about and then finally the last one being contact and contact we say that refreshing here and now if I click on these links it actually takes me through an You are else changed so if I copied this and pasted it or open a new tab and pasted it it's going to go where it needs to go so going back to this we've figured out how to handle the Your else we've returned the responses and we're returning better responses now we're not really ruminating things yet but we're turning better responses when I say remembering things of course I mean related to the user or something that's stored in the database we haven't sorted thing in the database yet OK So that's it for template inheritance if you have questions on this let us know we we will cover more of these things but if you're confused about this at all you might want to re watch it or at least play around with how these blocks work now you have to use block to make it actually be changeable so I'll do one more example of that and we're going to put a title tag in here and I'll just say this is going to call it movie picky dot com We say that we fess up here we see that it says movie picky if I go to any of the pages and stay as movie picky so what I want to do then is say block title or block head underscore title and then whatever we open we must close so in block head underscore title OK so I can actually copy just that block I don't need the title tags just this block here and I can go into Home done HTM

L and I'll call I'll say welcome To pipes and then block dot super And keep those curly brackets closed again when it opens must be closed we refresh and there we go we've got welcome and I can go ahead and copy this again on contacts And call it contacts

And copy again And go on here and say about Now you might be like hey you're repeating yourself you're putting this on every page Well not necessarily right action Holme I'm going to leave Welcome off and just have it as The default which is movie piccy dot com refresh the reel they're going to about we're going to bow to choices about contact contacts not showing up so let's make sure we save it refresh or go cool so it's dry and it shows us the concept even more on a whole nother level All right so that's a template inheritance again any questions let us know other ways this could go on And we got one more thing that we can actually add in here and that is the include template tag this is something that's actually really useful to really break apart your templates and the point of this in the reason you'd use templates is to really just condense the code that you're trying to fix and or work with rights on based on H

TML We actually have some sort of a navigation here it's not a pretty navigation but it is navigation on the list so I can actually cut this out and I can use something called include But before I do that inside of this template folder here I'm to make a new folder here and I'm going to call it snippets And inside of snippets I want to make a new file call it now have that H

TML Paste this in there is my new now bar so instead of base I can just go include and a string to what I want to include which is snippets slash now have got HT

ML Again snippets is the name of the folder it's in and then nab that HTM

L Is the name of the file so we save those things and I go ahead and refresh in here opes I didn't close off that snippet so I see that the error that actually happened It doesn't actually render it so going back into Base see how I have the print the C's or the curly brackets and percent we want to have curly brackets and percent at the end as well so we refresh and now it's there so my nav is showing up everywhere so that means that I can actually change it how I like so this also means that I can include other sorts of things so snippets some to go ahead and say CSS

Study HTML and Then I might as well say J

S Study So my CSS

and Javascript let's go into my base and inside of head I'll just go ahead and say include snippets CSS Thought HT

ML and Then at the end of my body tank I'll go ahead and say include JS OK So snippets the name of the folder is arbitrary templates isn't exactly arbitrary but snippets is in this case this kind of makes sense these are sort of snippets these are little things that I'm actually using vs the other templates are actual full on templates to give you actually ends of using those sorts of templates but this is going to be up to you on your own style and how you want to go about doing that so instead of C

SS What's actually add some CSS

Is go back in to get bootstrap dot com Go to getting started and yes I'm using bootstrap version three point seven RC Three point three point seven not Version four Not quite yet you can venture to version four if you'd like but it's not public or it's not fully released yet so we're going to stick with three point three at this time I'm to go ahead and copy their bootstrap CD and CSS

Only copy these two things here and as both of them in with CSS They are CS

S Themselves hopefully you know that all pieces into CSS The H

TML and Then the javascript I'm to go ahead in copy and paste that into JS

Thought HTML and I might actually

Have to import J Query it says Please note that all the javascript plugins require J Query so I'm actually going to leave out J Query plugins in general for now but I will get J Query C

DN Just in case because I might need it so I just do a google search for J Query CD

N and I'll go ahead and get the J Query core on a go to get the minified version of three point X To copy this come back in here and do script source equals two hoops I want to copy the link Andrus we go close out that script and I'm to probably have to put this above there so I go ahead and do that I revulsion here and what do you know if it actually changed the way it looks right and that's because if I go into based on H

TML I have these items in a container so it actually brings it inside a container so that means and I now by can do the same thing and I can say divs class equals to container Close off the divs refresh and it brings it in and that we go so now we have a little bit better styling and also a better understanding of how this include tag actually works we will clear this up a little bit more but this is something that's useful all across the board so these little snippets you don't necessarily have to do it just on base H

TML For example let's go ahead and put a snippet for side bar dot HT

ML and I'm going to use a URL

Class here so a list class And an unordered list will just say item one and then Item two OK So we've got this here and now in our About page I'll come in here and say include And this is going to be

Sniff it's An side bar but HTML

Close out those princes is on the about page so we click on a bell notice it is showing the those two items To test this we can add a third item item three Refresh and there it is contact doesn't have it about does home doesn't we want to bring it into contacts which you might we could get in the contacts inside of the block content of course we refresh in there and split save it go back into contact and there's contact about contacts There it is it's working just as we might expect so if you have any questions on this include tag it let us know Otherwise we'll keep him

There and so every once in a while you shut down your computer and or you shut down terminal and you get something like this lets say for instance you shut down your computer and you open the back up and you've got this here how do we actually reactivate our system then it's really simple we're going to list out where we are or PWD and We see that this is the virtual environment as we've created before so all I can do is source dash dash or sue me dot dot slash bin flush activate We press enter and that reactivate said virtual environment so let's go ahead and do that again where I'm opening a brand new terminal window here and again I need to navigate to my project and in this case it's just going into the dev CD try Django one dash eleven and then again source spin slash activates and of course the actual activations just slightly different if you're on Windows and then we CD in the source and then we do Python minutes on the run server and if we need to deactivate for some reason we just type deactivate and then deactivate it and then we can reactivate it all we want so just a little information on

Doing those things well let's keep going Now it's time to introduce a different way of or enduring a view or in other words rendering a way to handle a URL So we're going to be doing is using something called class based views now the big reason you use something like class face views is to eliminate repeating yourself or eliminate redundant work so if we actually take a look at three these three things for the most part they do the same thing there is some variance here with the context it's going through but all three of them really just render out a template with some context in this case these are empty context but they're just doing the exact same thing the rendering a template but they're not doing it much more than that right to they're not smart yet they don't remember things yet so Intel they remember things these are fairly common pages you might in I'm doing as we've already seen we have contact about at home now you might do more advanced things for contact but if your initial contact page is simply just like

Email us right so if we did a mail to and then your email address mail dot com or something like that e-mail us that might be your a neutral contact page and in fact I don't think there's anything wrong with having this long term because people still use email all the time so maybe that's how your contact page ins of being so since we have those things that's actually change them into using something called Class A fuse now you're don't want to use the reference a lot and I will say that class based views are a little bit harder to understand than function based views because there's just more to on they have more built in features in to them as they stand so in here we've got this is the oral for the class misuse Of course of you search Django classic Fuz API You'll see this this is just the reference for all sorts of things that class issues do the things that we're going to be really looking at as we go forward are really just these first few base fuse generic display views and generic editing views will worry about the display and editing later but for now we're going to be using just the standard base view view so if I click on that one it will take me down to the standard base few here and this is it so I can just really important here

And remit just come in here and do from Jane Doe views import view so the first thing I would do is I'm a convert these two to being a different kind of view so I'll just say class and it will be contact view OK Some give us some space here and heritage from view so that view that we imported here so it inherits from that view class and now we have to define which methods this view allows for now what do I mean by methods well if we look at our terminal here every time we go to a page say for instance if we go to home about our contact this happens right so in here we see it says Get So there are different kinds of methods for everything that we work with which I can get in those methods yet but we do see that the fairy first method or the method we're concerned about is get so we have to do is define that method here in order for this view to actually handle it so it's going to turn So a good method is that request type of request right so when I click on this and hit enter me as the user I am getting the information from the server so it's called a get method so that's what that is very simple so our view has to be able to understand these get a Methods we have to give it the ability to handle those get methods and that's what this is for so in here since it's a class face view we have to add in something else called Self because it is a method inside of a class so you need to have self in there and then it still is passing in the request so the request is still an argument to this get method and then finally we can add arcs and keyword ours which I'll explain in just a moment also and you're going to probably do this by default on class for a few years you can do it on function if used to but you'll probably do it by default on CLOs face views and again I'll explain that in just a moment so finally all I have to do is return that render function so just like this I can still return that context and this case it's just an empty dictionary here so I'll just do it just like that and now we have our new view which is our contacts view as a class view so in order for us to wrap this into a your L We go to your all stockpile and then now we're going to import our content view now is how we actually write the Centex for classes this is a python standard thing so you want to make sure that they are capitalized on those two new words whereas function base views are always lowercase and then they use underscores if you were to do something like that OK so now that we've got this context view I'm actually going to change our old one to being context view and then since the class we have to create an instance of that class so we used to Dot as view and that will actually run much like a function view view does on its own so this is a callable function based view which we'll see in just a second as to the error that may happen so if I refresh in here it's still loading just fine right so nothing really changed so what are the things that we could add that to make These ours and keyword argue do anything well if we go back into where you're else and the

This is slightly advance so you don't have to follow along everything I'm doing here but I'm just going to change the regular expression just slightly To have the ability to pass in something called an ID So I'm allowing for some sort of argument to come in so again will go into more detail in in the future but just so you know now if I go to contact one hoops I gotta have to save my you're else here but I go to contact one it still shows me this contact page if I go in here now without contact one that it doesn't show anything but now if I put any sort of number after contact it will go to that same page so if we go back into our view and I actually print out the keyword ours or the past arguments I refresh in here I can actually see in my terminal the ID That's coming up here now this will come in to play a lot more later but I did want to make sure that you knew that's why you might by default put Q or Darks OK so let's go to get rid of that save that one and we'll also get rid of this right here again just really to illustrate a point so you guys can see exactly how it is that's working the next thing I did want to show you is like an error that may occur that you might forget to do and you might forget to put as view so you might just import your class face you much like you do with your function based views and then just to content view I refresh and here hoops Let's go back to the right URL

Here and I get something like this so that means that you didn't actually run your view but it's not giving you that great of an error it really isn't so let's save it and we'll save the euro as again and I refresh everything is is just not working as it should and that's just one of those errors that you may end up seeing OK So that is An Introduction to class face fuse Now if you're familiar with HTTP

Methods there definitely are other ones right so we can do something like post and put SO This then allows us to handle those kinds of methods which is something we will cover but I'm going to go ahead and just comment those up for now and focus on how do we do this even more efficiently given that we have no context that's something we'll talk about very soon so stay with us So again we just went through that view or the default view for CLOS if you use to kind of basically change our function base you into a classes you created this has well it has a few like just as many lines actually a one extra line then the context view so there's actually a way to make this even more efficient even better because really all we're doing is rendering out this template so if we go back in here we can see right below view there's a template view so let's go ahead and look at that and the template view with that allows us to do is really just name a template right so we're going to actually do this right now first of all I'm going to go ahead and import will do from Jane Doe views to generic and we're going to import template view I believe you can also do generic dot base but I'm pretty sure that we can just import it from template view if I save it I have no issues so John Eric also allows you to do that import so to do the exact same thing as context view I'm going to just go ahead and copy the template view class and I'm going to get rid of these comments here and I'll just do class contact template view now and we'll save you and was just to template name equals to contact study Cherelle OK so now we've got that what's going to copy this get into your L's and import that template Cup contact template view And again we're going to have it as you say that here refresh go back into our contacts refresh in here and we should actually get it but it's saying method not allowed so we're going to save that again and save that again

What little error here we have only the view as there we should put template view oh so this is actually something important to note to notice that I didn't implement the get method here but I do see that the error is coming through here so it is showing me that there is an error I just am not seeing it on this it's just the method is not allowed so it's a four hundred five error that's just something to note when you don't implement a method for a view a default view so we change that back to template view and we refresh it here and there the template view is now working OK cool so I can do the same idea with about and also even home so let's go ahead and do that and I'm going to actually just call this the contact view and get rid of the old content of you we don't need that anymore so my your L's on it also just get rid of this now template view so we only have one import here I'm also going to get rid of contact home and about We will be changing those right now so me go ahead and copy this hoops Open that back up So copy this paste and paste first one is going to be the home view And then finally the about you

OK so now I can actually get rid of a bunch of things but I'm an important the home view and about View And change these in here as view And then about As view as well OK so we've got that savior your L's are looking good our views are looking good but there's one thing our home view is not correct anymore so if we actually take a look at it it's now going to be empty right and it's actually going off of the wrong template even so we want to do a home here and about here OK so now it's definitely a long the lines are it's right but on the home page it's no longer showing any of those extra context items so if you look at documentation it actually shows you what you need to do but basically in here we're going to add a overriding method called Get context data and it's going to take in self Args and keyword args so I called this an overriding method that means that I am overriding the method that's inside of the template view it's it's a default method that comes in with this class face view and that's just how it is it's predefined for us whereas our function base views nothing as predefined for us other than the fact that we take in a request everything else is not predefined this is one of the big advantages of using cost base fuses they take out a lot of that redundant information so I can do is to get that original context or to get the context that comes in by default I want to call that default method using the super call so we do super home self so this is getting the template view class basically and then when to get that context data I'm going to pass an ARG and cured R

's So we're passing in these arguments as a fail safe so putting these are always a fail safe in this gives me the context that is being passed by default to this view this is going to make a lot more sense once we go into more depth and use In things that are remembered but the important part here is that we can actually see that this context similar to this it's much like that all right so let's take a look back on our home page will refresh and in our terminal we see that it shows us context and that context is specifically for a view and it's actually giving us the home view objects now inside of this context I can also add in all of those things in my or original home context Now notice how I paste them the tab in the spacing is and correct some of these and save it now I've got this context data in here and if I refresh that data is now back and of course numb what were we doing we said conditional bool I don't let's put that to true again and then we refresh and here and now it's giving me all of the things as original OK So that means I can get rid of all of my function based views for class based views now before we actually leave I'm going to tell you one more thing that we absolutely can do to make these even more efficient and that is let's go ahead and copy this import here and go into our your themself imported in and instead of having a view for about and contact we can just do template view in here and then all we do is pass in the template name as an argument and then we do about to HTML

and The same thing is true for contact and then just contact out HTML We save our your L's go back into these views and just delete those off we don't need them anymore and so in our Urals we just are going to import that home view in the only reason we're importing the home view in the first place or or customizing that template view in the first place and because we have this extra context if we didn't have that actual context we would do this same thing right

There so if you have any questions on template view let us know otherwise let's keep going So we're actually at a point where we actually have a functioning Web site I mean we can add in a bunch of other pages but now in lieu of just doing straight HTML

We could use Django right now as it stands as a web site I mean it's really just ready to go obviously you have to change the style Anan and some some other like nice things to it to make it more of a you know robust or better looking website but even some people might think hey this is the simple sort of web site that I want I mean there's an author that I really like his name's Derrick severs and if you to sit org and read about him this guy look at how simple this website is but it's very effective and it shows everything that he is a really cool guy if you haven't looked him up definitely out but I mean how much different do these things look they're not that much different when it comes down to it other than he has a bunch of content and if you look at like his articles he actually saves some things so if I click on something this is an article that's actually saved in his database so what that means is it's time for us to start saving things to me to start remembering things so we've already really handled the first and second thing but we haven't remembered things and then had those first and second things were being remembered either so by default Django actually has some built in back and features so I believe we actually already created a super user but even if we didn't Let's go or even if we did let's go to make another one and I'm going to show you that it already is creating something that's already saving something so if you Python it up the why create super user going to make another one and I'll just say j Mitchell three not put the e-mail address in and just put whatever password in my case I just put low In code I say a whatever password because we are in a test environment once you take it out of a test environment and send it into the wild you're not going to want to have a whatever password you're going to have a good password OK so now that we've got that my server still running I have I have a new user here now and I can actually go in to the admin so we go type in admin and this should take you to something like this so I'm a Type in my username which was J Mitchell three and learn code I had log in and there we go to go ahead and chrome save this password because I'm to be using a lot this is the back and this is actually Django connected to a server so if I click on users I see that I actually do have two users I already did create a user but these are actually saved in a database where is the database exactly where that database is in our DVD

Sequel lite three file this right here so when we run those migrations what we did before which was Python atop the why migrate what that did was it saved a bunch of data in the database now what is this data and where did it come from exactly so if we look at our settings module or based upon our case right now and we scroll down we see this thing called installed apps so these are in there by default one of them being the you guessed it the admin where we are right now on our actual browser the next one being off as in authentication as in I logged in as a user right you saw me do it content type sessions and a few other things that we're not going to get but this is how our database knows about this stuff right so our databases actually correlated to some of these things and it allows us to save stuff so off is one of those things that we can see visually right here in the admin is these are authentication authorization these are our users that's coming from this app right here We're to go one step further and even look at that app so inside of Libya Python three point six site packages Django Django dot Contrave Django dot contra dot off and on to just zoom out a little bit so I can find it for us there's this thing called models up high so I click on models and if I scroll down a little bit I'm going to see I see Group group is a type of model and then if I scroll down even more I'll see a class called user Or sorry rather abstract base user so this is actually the user model so this is the data that's associated right so we see first name here by clicking on one of these users first name we see last name email where they're active Is active staff day joined all of these things very few of us will down I should see joined right there last log in I've got all this stuff that's already Associates a database because of the code that was already created now I'm not going to touch this models I just want to show you guys that it exists in our virtual environment and we can actually take a look at it so we want to do is something very similar but not a survey for users because those are already exist we don't actually have to touch the users but instead I want to do it for Rush I want to have a list of restaurants first right so I want to be able to allow me as a user to add my favorite restaurants I'm not doing this where it's like a yelp type of thing it's much more for me it's my own personal thing so I mean I have my own favorite restaurants on the right in all the information for them and I'm a save them in the database so to do this we are going to do our very very first model itself so we do class and I'll call it restaurant And it's going to take in models thought model

So models that model is the class in hair Reading from so we can actually map whatever we type here to the database so it can actually save in the database so all see here is just same name and I may use models dot char field and we'll do max length all explain why we're doing this a second one twenty and that's it OK so we save this and since we created models and we added a new app we did both of those things so I want to add them into my install that's in here right so all I have to do now is add in restaurants So what this does is it's telling Jane go hey you have this app at restaurants which is this app right here hoops or restaurants You have the separate fronts and you have inside that app we have something called models much like we just saw with off so since since Django knows about this now and we created something in our models we can do something called migrations So we'll do Python minutes up here why make migrations so migrations we press enter and then we do Python manage up the why migrate So what this did was with look at it we created when you said make migrations So there was no history of this model ever existing right so the database doesn't know about this model so we have to kind of prepare Jane go to tell the database to know about this model so this does that this prepares Jango to tell the database about this model and then migrate actually tells the database about the model and makes the database fit with the model so they work in conjunction with each other they're nice and align now toast looks rather Again I'm actually in a copy name and paste underneath it and say location and this time I'm just going to go ahead and say knoll is

True and blink equals to True Image have these over so they're aligned OK So we've got name and location so we added a new field to our model we added something new to a model that already exists in the database how do we know it already exists because we created a migration and then we migrated it or we added it to the database so we go do that again so we created migration this as this new field to two location to the restaurant model and then we have to let our database know about it so I prepared to go to let our database know about it and then I have to tell J You have to let our database know about it it's two steps always always those two steps whenever you make changes you're going to run Python it up you why make migrations and then Python is IP why migrate so prepare the changes make the changes in the actual database Well OK so now I've got this model let's go ahead and bring it into the admin so we can actually take a look at it so open up admin dapat I in side of restaurants we just do from DOT model so I'm importing that model restaurant we'll just do admin dot site DOT register So this method imports the model itself so this model and as it into that I am and so if I save this file as well as models of high of course you always want to save these files before you one run migrations to you otherwise nothing's going to happen in fact it will probably look like this right so no changes detected I finally made changes and that's album migrations work OK so now we've got it in the Admin it's going to look and then click back on home and what you know here is that restaurants app and the restaurants model name now there's a little bit confusing so what I'm going to do instead is I may come back into my model on the change restaurant to be restaurant location change the name of the model so Made a change to the model and now we can't import Russia locations so let's make sure that admin is of dated correctly And again with classes you want to make sure that you name in that way so I may change the model so as to do make migrations and look at it it's asking me to do remain the restaurant thought Rochelle model to Russia location yes I did and then now I can migrate and what do you know it changes everything in the database I refresh in here and now it's a little bit more clear restaurants app restaurant locations I click on this and then I can add another one and I can say my name and location so I'll say the first restaurant Baja fish tacos one my favorite talk of places right here in Newport Beach Well it's actually technically coast I'm a sessile let's put it in coastal Mesa I'll go ahead and save it and there we go we've got a restaurant location object that is it it's actually saved in the database so at this point you can add all the locations you want and now the key is to get this data out of the database and put it on our Web page that something will do very very soon stay with us

Now before we actually jump in to allowing anyone to see these items in our views I'm going to add a new item to the actual model to give you some practice to it so again when restaurants models up high and I add in category and I'll just say models Char field max length one twenty and also in the legal to true and blank equals to true I know I told you that I would tell you what these things mean so what it is is this character field has a maximum length of one hundred twenty characters that space is any sort of alphabetical characters or any numbers that's one hundred twenty total it won't go beyond that it will raise an error if you try to or it actually just And do it and then knoll being true and blank being true no being true means that it can be absolutely empty in the database so the database that's database related can be empty or knoll in the database being true means that you can leave it blank on your built in Jane Doe forms or even the admin so if I said blank equal to false will take a look at what that means and you say get some to say that now I have at it what do they do I added category into a model so I added a new field to my model so what do I have to do I have to run Python managed up the why make migrations and then Python managed up why migrate it shows me what I did hit migrate make sure my database has it and then I was going to go ahead and check out the admin right so let's go back into the admin in a click on that object and now I see that categories here for saving continue it's now saying the field is required that is what this blank is doing so if I say true again and what I do I made some changes to my models so I have to to make migrations and then migrate always every single time you definitely want to do that I refresh in here and continue and what do you know category becomes no longer required but categorial say Mexican as in Mexican food and we hit save and continue and that object is now in there OK So that is some of the basics of saving data we kind of understand that we went through before and now it's hopefully it's a lot more clear and you might be curious as to like what kind of fields you can put in here and that's really simple if we do a search for Django model fields this is how you might end up searching for them right so we're working models and we want the different kinds of fields first thing that comes up model field reference so I click on that I can actually scroll down I can read about those things that I just told you about blank knoll and then all the fields themselves so that field types if you look on the right hand side here you can click on field types or of course you can just go to that your L Here and you can see all of the different types of fields we've been using char field and if there's an argument on here unlike here it's option to do them an actual argument then you that's usually something that's required of you to put in there so what let's say for instance this comma separated energy field I'm to add it and then remove it just to illustrate a point here so will say end field and will just say equal Sue models

Dot com a separated in your field I'll save that and because I added some data to my models I run make migrations and as soon as I do that it says char field must define max length so it's giving me the error Russian app rational model and the field name and it's saying that a char field must define the max length so that's technically a char field so it's inheriting from a church field so saying you need to make sure that links in there so Jane is really smart and it's actually gotten a lot better on the errors themselves and Stephen's showing us that this comma separated in your field will not be removed in two point zero So let's not use it at all it's actually remove it I'll save that and I do make migrations again no chains were actually tech did because it didn't even allow me to prepare for those changes because we had some errors cool OK so that's pretty much it I mean there's a lot of other things in here such as Date field this is another one that you might in the pusing and a Date Time field so I'm to go ahead and do that I'm going to say date or rather timestamp so when we actually add it into the database will do models thought Date Time field And there's a couple things that we have to kind of say either now equal in the false auto now and equal in a false OK so if I want to use a date time field like this this is no problem so let's say for instance we had this let's take a look at it I'm going to go ahead and make my migrations because I made some changes and look what it's trying to do we're adding a non noble field timestamp to this model but we don't have a default So this is the thing is like once you have database items and you try to add a new field you either have to give a default so a default value of something or you have to say knoll equals the true or the last thing would be to provide a one of the fault when you're running your migrations which is what we're going to do and provide a one off the fault notice that the date time and You tills the time zone while model modules are available and we're using a date time field right so I can actually do time zone dot now hit enter and what do you know it actually does that and then if I run migrate it creates that timestamp for me so the server should still be running that is so it's actually look at the admin for that for refresh and here I now see that I have this timestamp right so the timestamp is based off of what I just did now the reason I'm telling you about this is because of these two items right here timestamp actually is more referred to when it's actually added to the big database so auto Now add should be true not false but I did want to show you that there is a way that you can change these inside of the admin now is soon as I do this you can no longer change in the admin and all that is also true for auto now so we're going to get rid of the extra arguments here because you don't need them and we're going to put this as updated Ans will save it I made some changes to the model so what I have to do make migrations and then migrate and now if I go back into the admin I refresh and here it's no longer there like I actually can't make changes to those although those fields are definitely on that item but these are automatically done for us they're automatically saved for us in the database which will actually see when we take a look at these but when we take a look at them on our views but this is an important part and it's something that you definitely probably are going to want to do on a lot of your models especially something like restaurant location so like if you haven't updated this restaurant in like five years you're probably going to want to know about that you also might want to know when you first added it to your you know your idea such as if it's an anniversary if you've done it if you actually did it two years ago you might want to know that even if you updated or make it made some changes to it you might want to know hey we had this location two years ago that is something that you might really consider having That's why you might do both of these fields and again my own date time field or let's say my own date field Equals who models to date either date time or date field and to really just get rid of all auto now and an auto now being too false and we're just going to tab these all over now I like doing this to make it nice and clean and easy to read it's not necessary but it's something that's good OK So this is another field again that's how you would do it if you wanted your own custom field in there for that date object I'm spending a lot of time on that because you in of using these things a lot or at least I end up using these things a lot the other kind of fields we will go through as needed for this particular project but I do recommend that you check them out on your own and really start to experiment if all things go wrong just

Delete everything and start over literally delete the entire app and just create a new one you might even have to leave the database and also create a new one but that's how you learn to get better right so stick with us Now it's time to take a look at these items we actually want to see them in a view finally so to do this we're going to jump into views dump and I'm actually going to remove the home view elsewhere like I don't want the home view on my restaurants anymore that doesn't exactly fully make sense in fact I don't even want the context data for this home page anymore either so I'm going to delete it completely and just go back into your L's belief that home view also or what we can leave the import there but commented out and I'm going to give my home page its own template name just like that if I want a more advanced home view I can do that in the future but for now I don't even want to I want to leave this completely blank like as if we were working on a new app All by itself OK So we've got our view set up here and what I want to do is see all of these locations that are stored in my database so we're going to do it first of the function based view so you can see how that's done and they were going to do it in a generic view or a class based view to see how that's done too because both have their advantages but one is definitely more of the tape has so we're going to define our function based view first and we'll say restaurant list view and it's going to take him certain itself but a request and it's going to return render request we're going to put a template here so we'll just say template name and then we'll do context of course those things we have to put up here template name legal something name context equals to an empty dictionary So this is our list view right so we actually want to put some context in here and we also want to template now what I'm going to do inside of my apps I'm going to actually put the template folder and here I explain this in a second I will put templates and here and then to make another folder inside of that and name it the same as the app name so restaurants in this case and then inside of that we're going to make a new file in there and we're going to call it restaurants Underscore List dot HTML Restaurant list OK so now our template location is restaurants and rusher on its

List dot HTML OK So that's our template name and now we want to add some list here so let's just call it call it object list and it's going to be equal to some sort of list and for now I'll just say one two and some random numbers and we'll just leave it at that so this restaurant list feel I'm going to go ahead and copy that add it into my you or else again we left that import for a reason so I can bring in that restaurant list and I'm going to copy the about your I'll paste above it and we'll call it restaurants and we'll do restaurant list view there we say that not a big deal here and then in my restaurant list H

TML Will just say each one restaurant list close off each one we save it and let's make sure a service running looks like it is make sure all your files are saved to opening up our page here we're going to go into restaurants And we see rush hour list of course if I look at the actual source of this we are probably going to see a little bit of an error and that is we only have the H one now

This is a little bit review but you want to make sure that you extend so extends based on the shoe mill And then we put it into that block contents And BLOCK OK and then whatever else we had in based on the smell I think of the title we did have that so many go ahead in copy that over bring it in as well outside of that other block I don't actually need all of this stuff but this is just a way to reference it also block super And I'll just put restaurants BLOCK super OK so we refresh in here refresh in here and there we go we've got a little bit better formatting probably not the best but better and now we can iterate through that list so I'll just say I'll put it UL here

An ordered list so for item or let's say for object or OBJ In object list I'll explain that in a second so and for whatever we open we must close course object list is the Context object we gave it come back in here and we just say object or OB

J and Of course that's the list item itself You save that and we refresh in here and we know we've got some numbers coming through OK So those numbers don't mean thing though but we want is the actual data from the database so much like when we import into the admin we can do from DOT models import restaurant and this is going to be restaurant locations I believe a call of Russian location OK And we're going to say now query set Equals to restaurant location the objects all don't worry we're going to explain this stuff a lot more later but what this did is a list of items that are

Stored in the database so object list is now that query set when you go ahead and save it restaurant list this should still be in there so if I refresh an heresie it restaurant location object that should look familiar we go into the admin go to rational occasions rationalization object what do you know this is actually a list view much like this is now just more actions on the admin but it's still the same thing now if we had another restaurant let's say for instance Kielty this is Newport Beach and this is also makes in tacos we had save we now see that as Russia location object twice I refresh it here that's not really what we want so there's a couple ways to handle this number one I can use dot notation to the field so object is the actual what's called an instance to the actual item itself so if I do object name or OBJ To a name refresh it now shows me that name and I can keep along with that logic and go object Name dot location

Category Dot timestamp Dot updated Save that and refresh and here there you go so it's actually given us all of that data and the dot notation is going off of the individual instance itself and the field that's associated If the field if this was not a field so if I said title here we go back and refresh it doesn't even show anything right because it's not an actual field so I saved his name and it's working again so it's actually showing us these items and these things are actually stored in the database something that's really cool is this first one actually has a time that's older than this second one because the time stamp Happened before we most recently updated it so it's actually updated I'm just going to come in here into Kielty and hit save refresh in here and T O T It has now been updated to minutes after is created and it's like the other one hit save

Refresh and here again updated as well these two are different now OK So that's pretty cool we now have a restaurant list with different items that are actually displayed and you wife you wondering why does this still say restaurant location objects Well we have to we have to or we can implement something called an STR Method that allows us to return what we want so in something names I believe this used to be required I don't think it is anymore doesn't look like it but that will allow you to have your object being named something different so instead of it being some object name or or the name of the model itself you can just return that and that will actually do that and that's also true for over here so if I went back into that rational list and just put OB

J and Put a break tag here Will see it twice right so versus versus going back into those models commenting this out So that's real simple easy change we did make some changes to the model so let's see for instance if we have to do make migrations in this case we don't so whenever you have to whenever you may change the model just just check in to make migrations just to make sure obviously once you are better you will be very used to not having to worry about that in particular Ok so that's pretty cool that's showing us the data that we want to see and is doing in a way that is fairly user friendly there's not a whole lot of complicated stuff going on here the things that before the one of the things that probably really doesn't make sense is is this query said part and that's something we'll talk about in the next one if you need questions let us know we still have to turn this into a class with you I realize that but I do want to talk about crusades before we do That so we'll see you in the next one

OK So let's talk quickly about query sets I'm not going to go into a ton of detail because there's just a lot to know but basically what queer sets do is it allows you to grab data from the database that's what it is and a default it's kind of getting this data from a database and it does so is a list so it's a list of items from the database in particular so if you want to learn more about queries set themselves obviously we have a lot of projects that talk about it a lot but you can also read the documentation and I recommend that you do so especially because this is such an important concept but let's go ahead and play around with some presets a little bit on our own but not necessarily inside a Django but rather in the python shell So yes the of the python shell is of course this right so if I type Python I go into the python shell might have an X It will allow me to remove from or exit out of the python shell so when you want to use the python shell for Django or your Django project and also the items in your Django project this is where you're going to have to do and that is Python managed by Shell this is a really good place to learn in understand a little bit more about how your data is saved in the database how you can work with models and all that now of course also doing it on the code itself is definitely recommended but you can also do it inside of the shell to really do rapid fire understanding of how these things work so I'm going to do is from restaurants That models import and this is restaurant location right so we're importing that I hit enter and I'm sure go ahead and copy and paste restaurant location objects to all and hit enter and there is my query set right remember how I told this a query said the reason I called it a query set is because that's what it is that's what Django Calls that they call it a query say of course you can give it a different variable name but for us we want to actually call it as a query set so I can actually iterate through this so I can do it for I or for object in this query set here so let's go ahead and copy and paste it again I can actually print out object name this shouldn't be that surprising we actually already did this we did it inside of our template right so that's this right here well there's there is more things that you can do to query sets right so what we can do is I can actually come in here and say QS

or Query set equals to this these objects again and I will do crazy set Filter category equals two Mexican Empty where he said Why send Well that's because we want to do category underscore underscore I sacked equals to make skin so that means it's a case insensitive just another way to do it so this is a filter this is how we can actually get down drill down as to what our queer sets are going to be so another thing you can do is actually update an entire crew so that means I can change things to an entire crew set so what if it did QS The updates and I said category equals two so Q

S Of course is still related to this variable right here since that entire query set and she's a Category two let's say American And let's put it as a capital And now I updated two objects was a return back how many objects actually updated when you QS

I still see those two original objects and now for Duke us by pressing up to QS Dot filter the makes him I hit enter and now it's empty right but if I do up again and do American Hit enter it gives me those things right so I actually made chain Just to all of those items in Q

S That update is the way to do that by and large I mean it's a very easy way as you see we just did it so let's actually change it back to being Mexican because that's what it is and now we've got this crusade that we've got some Mexican food in there and that's good well what if we wanted to do Mexican American fusion and what if that was a category and that's an item we wanted to add now one way we could do it is by jumping back into the admin actually creative another way to do it is actually in this shell by creating a new object now if you're familiar with Python or how Python classes work we can say instance or OBJ Equals two or restaurants

Location and put princes after Right so this is an instance of this class I hit enter and you OBJ Dot name equaling to and we're going to call this let's just say pay way And O

BJ Location is going to Newport Beach And OB

J Category is going to well this is actually not mexican american this is more of like Asian fusion OK And then now when you OBJ

I see that I have this instance in here of a restaurant location I do OBJ Name it's going to give me all of those things if I get OB

J Dot the time stamp What's it going to do well nothing because it's not actually saved in the database until we do OBJ Dot save I hit enter and I do O

BJ Now I've got this new Russia location and I can do my crusade again so I'll do QS Equals to Russia location objects all and that gives me now three items

and here right so this is a new career set well we can do another query said were filtering these things down so QS Two equals two Russia location objects That filter and will say category I exact equals two Mexican hit enter Q X two and now we've got two different ones right so if I do QS

T Exists this should say true right so that first query set actually exists if I do Q S two DUT count it will show me how and how many items are in there right instead of using the Python function length or L In the end this is not as efficient especially when you have thousands of items QS To count is how you do it so so a little bit more methods some things that you absolutely can learn about and play with we covered a lot in a lot of our projects but I want to bring this to your attention mainly because this is how we're going to be using our generic views going forward now there is one more thing that I do want to mention and that is this right here this is actually not how you go about creating new items in your database you can do it this way but more than likely you will not instead you'll use the built in Python built in general method the SO will say object equal Sue restaurant location objects dot create this time we're using create and we'll say name equals two let's call it chronic tacos and we're going to add in a few printers in here and that's going to be location equals to Corona Del Mar and then finally our last thing would be the actual category which we don't have to put but I'm going to and you guessed it it is Mexican food I'll hit enter and now if I do OB

J There's our object again but if I do OBJ Dot timestamp this time it actually gives me a timestamp or the other one didn't if you remember back to that again this variable is arbitrary so you could call it something different but we now have a few items in here so why do Q

S Rational acacia objects to all We see that we have a few items QS That count shows us how many items we can also do a query set or Q

S Equaling to Russia objects the location objects thought filter and we'll say category I exacts equals to Mexican and will exclude So this is still a query set we're going to exclude name I contains equaling to let's say tacos let's just put a capital T Here Duke us like Count one and with a QS

or The career set location is that one right so it doesn't it excludes that item and it is Mexican food so again these crew sets we can actually take a look in here there are different ways on how you do that by using fuel look ups here's a whole long thing about how it does and yes you can you can take a look at more in-depth instruction or location that they do but again we will go into this a little bit more going forward that is just a way to do it inside of the shell itself so if you have any questions of what we did here let us know and let's keep going and of course make sure you exit out that shell Now that we have a better understanding of how the sets work we can now use a generic view specifically for List View because of how this query set works we can do that so let's take a look I'm going to go ahead and do Django list view generic or generic with you am going to click on generic display views and if I scroll down we have list few here it shows us the import it does dot list but I'm pretty sure I can just do The import here List View much like I did before and now want to say class I want to do a few of them so we can see how this even more on how these crew sets work first of all I'll do restaurant list for you and it's going to take in a query set of this same item here And we're going to also copy that and paste below it and we're going to call this

Mexican well let's call it makes can rationalise to you OK And then one last one and we'll call it Asian fusion Now if you remember back we just did filter And this can be a category I sacked equal Sue Mexican OK and this would be filter

Category notice the double underscores of course Asian Fusion OK so we say that let's go ahead and add these over into our your L's and we're going to import them I'm going to use parentheses here to import a few different ones and make it cleaner Rationalist view Mexican restaurant Less few and Asian fusion originalist view So rationalise few is going to take over the other one

And then I'm to go ahead and copy this a couple times And we'll do make skin and Asian Next thing He can't so we save that and really go back into our projects refreshing here notice that says template does not exist it does not say anything about not finding a template or template is not valid right so there is already a default template but let's go ahead and use our original templates omission and come in here and just Template name on all of these

Save that refresh what do you know there is are restaurants and if I go to Mexican There's a Mexican good Asian there's Asian or it so it's actually working fairly dynamically but what I showed you before was we had the ability to write in some parameters here right so like I actually showed you with a contact page there was a way to pass in parameters with a function these few but how do we do it with a class based view so what if I wanted this to be a little bit more dynamic of a List Well it's really simple I just go in here a new PC and What's a slogan And we're going to use a slug field but I'll just use any characters for right now and then I'll comment out this one can explain this in just a second and we're going to call this search restaurant list for you instead and we get rid of these

Go back into our restaurant views and changes to being search get rid of this And what to do here is instead of having this query set I'm just going to do define get query set self and return crew say so at first it's only going to show Mexican food but we will print out the self keyword arcs So we refresh an Asian use QS

That should not be QS but Rather query set so the first one should print out In here it prints out slug being Asian OK not my skin so this right here has an argument that's coming through a slug So explain this again this is a keyword argument that's been passed to Disney you inside of that view

You we can grab it from the instant so doing self thought were guards that gives us the same thing that we saw before when it was ours and keyword arcs we're still getting that same sort of idea here down with this crew sent so I can actually better filter this down so it's a slug equals to self Get slug and I can use Dot get because it is a dictionary itself and will say if slug then will filter it by that slug Otherwise we will just give none Restaurant to objects none save that and will refresh Asian gives us none so it says right now it says I exactly but what about I contains or a few other things so there might be a few options we want to actually have here so I will show you a little bit more advanced stuff so we'll do that Django dot DB

Models import Q This is something called Q look ups so when we were using Asian fusion we used I contain is for member so it was I Contains that So if I leave it as I contains and refresh it's showing that but I want it to have more dynamic I want to filter either one so that's where this queue lookups comes in and we can do filter Q Right so this is I was actually this is that original one so if I refresh it here it shows me nothing but if I go to two Mexican It's going to show me those Mexican ones OK So that's working that part's working so it's still the same if I put a pipe here and paste roughly that same thing and do I contains This is saying this pipe or that OK

So we refresh makes can still work in as as expected Asian is now working to OK So that's a little bit more about crew sets and how these you know default generic views are really really valuable for us right so I only have realistically one sort of way to do this and here's another option as well is I can actually make it one list view so I want to go ahead and do that I'm a copy of this out paste it in here and instead of it being returning none I'll just return all so if the slug exists then it's going to do a search for it if it doesn't exist is going to do all which was that default anyway so I can go ahead and get rid of that and I can now get rid of this searched list view here And here in this print statement because we don't need it now this rationalist fuel is printed into our your L's just that and put that there there we go we save both of those refresh my restaurant list or the search for that one is there and just your standard restaurant list is also there this one now has pay way in there right so if I do Asian It's not actually working salutes come back here And make sure our views are saved correctly

Rationalised few save it there we go so now it's working again so Mexican OK perfect so again that's how we do cruise that is the way to do it and this list few makes it more dynamic for us to do that and we don't have to worry about all of these other things in fact that template name is something we over rode right so the default so if I just get rid of that and save it this is the default so I can actually just come in here and change my old template to being the one that it's giving me You know explain how that works in just a second and there we go so I changed the name of it to refresh and it's now working again so the name itself is going off of the model so the model name is Rochelle location and that underscore list because it's a list view that's now showing that OK so if you have a question this let us know those Let's keep going So now we want to see is a actual profile or an individual page for any given restaurant so when I click on it it's associated to what stored in the database and it shows maybe some more details about it if we want such as the menu items we like there so let's go ahead and create that now I'm going to jump back into my restaurants A views on a copy the rationalists view and paste it below hand here and instead of get query set I'm just going to put that query set back up here just saying all restaurant objects and we're also going to import the detail view Here and just change a few things on this

Again this is how nice generic views are so we go ahead and copy this and go into my your L's here bring it in and we're going to grab a restaurants here And underneath that I'm going to add restaurants instead of slug like this I'm going to do An ID So do P ID or actually that's two P

K Oh that's a primary key which I'll explain in a moment all right and then this is restaurant detail view very good so you might be wondering what these are these are called regular expressions so we actually have a little post on a common regular expressions for jail your L's this post kind of explains or only shows you different expressions you my in of using and the one we're doing right now is the object id But instead of using the keyword name of order we're using the or even the keyword name of ID We're using the keyword name of PK Primary key which is automatically generated inside of model so every single model has one if we look at the admin itself and we click on any given restaurant flits make sure our server is running up cannot import name or restaurant detail view let's make sure we're saving everything there we go going back into a server

Our database gives me and we see that if I click on any given restaurant I can see that there's a number here that number is called the primary key is also known as the ID So PK and Idea are the same OK So that means that in our Your else here this is the primary key this is going off of that field specific to that model so the restaurant detail by default looks that up it goes for that item so this restaurant detail view if I come back in here and say one I know that I have a rush item of one but I have a little bit of an issue here and that's because we have this slow we have this other search view that we created before I'm actually going to comment that out and not do it that way and that's actually not a great way to do it all changes in just a moment but instead we want to have our details coming after restaurants and I'll explain that to right now so when I refresh here after I've got got rid of that other view in the URL

I see that I get a template does not exist so Django knows where to look for these templates I mean we didn't really go into this yet but if you can just look here it shows you that is looking inside of source templates restaurants restaurant location detail and then it keeps going down and even looks inside of Russia It's the app itself that's how this works so in based upon This template loader thing right here we've got Django templates here that is actually the default back and forth templates and it's going to find inside of our apps as well and that's what this is for so if I got rid of after is here and refreshed it's only going to look inside of where I put my location right so this right here but I wanted to be in the after rectories because I want to be able to move these apps with other projects so that keeps it nice and self-contained that's kind of the point but I just want to mention that for we go forward or so now we need to create that detail of you original location details I'm going to copy this and we're going to go into our templates here and we're going to crude it so restaurant detail partial occasion template and just like a list feel to go ahead and extend a bunch of things here but now we actually don't know what the restaurant name is right so I don't have the restaurant name at all here in fact I don't even know what context is coming through by default right so I have no idea what context is coming through by default here so what do we do now this is going to use to thinking back to how we did it before how we overrode context before and that's get context data self args and curate ours so what I mean do here is I'm a first of all print off self like you are are so you can see what those are and then I'm in the super context equaling to super The class itself so this is calling that detail view class that method in their they get context data method and we're going to do ours and curate args will print out their context days And then will turn return that context now and most details are actually all generic views engine go you can do this and it will show you what that context is so if we save that refreshing here the template exists we are going to turn all we see the keyword arguments PK

Is one and we also see the object threaded gives me an object as context so object is our context variable I recommend that you pause this right now and look at the list view to see how that works too but since we have this as our object right so object is inside of that context we can go in the detail and do stuff like object name and then Object location And actually put even small here and say object a category And then we can even put a time stamp act Time stamp

And all even put Object are updated and show you a little filter called Time sense and just say go So that's a context template context filter and it says fifty six minutes ago so say updated Fifty six minutes ago get to refresh and then real so this is now that item so we go back into restaurants and these aren't linked at all so let's go ahead and just change it to two and then we go see it's working it's exactly the way we want it so this is now our detail and it's working just fine our views are coming through our keyword arguments are coming through and all that so what if we want to change our keyword argument that is you know your L's What if I wanted this to be something different Order idea or rather restaurant idea I could do that I save it and I refresh in side of the details view it's going to give me this error so it's going to either be the object primary key or a slug we don't have a slug we haven't talked about that yet but it says objects or ideas I mean it's still the same sort of stuff that's coming through here but we have it in as slug so good common regular expressions we can see these different ones in here so I can change it hell I'd like in this case I want to keep it as rest ID So this is the object ID itself but instead I managed to update this and here and will say to find Get object and takes in self args and keyword R

's this is another built in method to the restaurant detail view and I have to import something else called Get object or fall for And what this is going to do is we're going to say the rest idea Eagles to self that keyword are stuck get rest ID And now the object equals to get object or for a for this short cut here and restaurant location and ID it goes to rest I did You could also do PK Has to rest ID you can replace Pete ID with P

K and Then return objects save that go back into our page here and refresh and what do you know it's now showing up again OK cool so that is a really good start as far as getting our object to tell I'm going to go ahead and give rid of this context data because we don't need it any more is really just about discovering what's in there now by default all detail views and the context variable is always object much like all lists use the context variable is always objects Lou Pst I'm just keep that in mind when it is that you're going through this OK So that is detail view in the next one we're going to actually talk about slugs and creating them I will say that if you want to see that common regular expressions this is the your L Right there that will take you right to it and yet it's worth taking a look at to understand this if you want to learn more about regular expressions check out our project thirty days of Python we talk about it more there and we have a few different videos on regular expressions itself we have a whole project on various missions actually aren't they swaddle see you in the next one You know one of the things that you want to do is when you design your Urals and yes you are the one who gets to design them you're going to want to make them more robust or at least easier for you to go about reading them and that's kind of what's important here is I want to make sure that this is readable using one that doesn't give me any description whatsoever about this U

RL but Saying something like Baja fish tacos would right so I already know that that's probably the name of the restaurant or something of that sort now this is called a slug and that's a slug field or is or rather a slug they could be saved in your database as a way to look up items so we did a look up in the last one where we changed it to rest ID This is called a look up it's actually going in to the database and it's either going to get that object or raise a fall for it and show you that but if we did some other number there it's going to give you this page not found but we wanted to be a slug it and luckily we created a little Tauriel a little guide on our site on how to create a unique slogan generator specifically for Django So that means that this is automatically going to create our own slug Field for us based off of our model and it's to ensure that they're unique as in no other instances have the exact same slug much like no other instances have the exact same ID every idea is auto incremented right so this is for the last one three The newest or the next one would be five and so on so let's go ahead and create this we're going to go to all models and we're to criticize like field so we'll just say slug equals two models that slug field and we could write unique Eagles to true we could add that in there that's a nice way to make sure that nothing else is using that same slogan which I'll go ahead and do that rest save that for just a moment but we're going to go back to this unique slogan generator to continue off everything else now one of the things that I want to do is own a make a utility file so this is actually something called a utility function so it's going to actually create this generator for us so instead of restaurants or to go to a new file here we'll say you tills up high and I'm sure I'm copying literally copy and paste this code here I do need this random string generator which is something else that we've created of course this year L Shows you it so if you go to that your L

It's going to show you Right below here this unique Or random string generator this this creates a random string for you to make sure that your slugs are unique so I'm just going to add this in here And we're going to get rid of that So really quickly as talk about what's happening here is it's going to take in an instance or an object from a model and then it's going to use those built in method called slug defy the slogan I will return a slug it turns a slug but it doesn't ensure that a slug is unique for that class so then it basically just checks to make sure Or it's unique in that it returns a back one thing to note here is this instance the title are instances instance that name not title but I'm going to leave it as title instead we're going to do is jump into models and add a property property and we'll just say define title takes itself and there's going to return self that name so what that does is it just as the ability for us to say object dot title as you seem we can do that right now just test it out object a title on our Detail View Refresh in a detail that exists make sure the server is running we made some changes since we didn't save them so unique is not spoke directly and I totally blew it on that rule so let's make sure that every time we make changes we go Python it up here why make migrations and

Python and then we get this error of providing a one off default right now now this is a little bit of a problem for us because we're adding it after so instead of you doing unique I'm just going to allow it to be blank and knoll so I'm going to hit to change that again saved it and then when I make migrations and then Python managed IP why migrate and then all restart my server OK so that back we refresh in here and notice the title is still working OK Again we added inside a remodel we added this property method that returns the title so that we can use dot notation and get title and of course that is so in a utility function we can actually generate a slug so let's go ahead and generate this log in the shell and then in the next one will talk about signals and do it there so do Python minutes up you want to shell and do from Russia It's the models import restaurant location and I'll just say objects restaurants Location objects that gets ID was the one this is that get call that we did it's very much like that except it doesn't run a forum for and we got object here so object type name object a title might still working all those things are good next we're going to from restaurants dot you tills Imports and we want to import this unique street this unique slug generator Looks like we're getting a little bit of an error it's possible that I didn't save it so let's exit out again and try that one more time going to go ahead and copy and paste some things sometimes you forget to save OK so its import this is well there we go now it's working so this you need generator will just go ahead and print out the unique slogan generator

You know get rid of this space here Unique slug generator of the instance of object we had enter there this Baja fish tacos made it for us that's really really simple so it's actually do it's like a five but if I have another item called Fish Tacos it would make it unique and for sure unique OK So that's it for the unique slow generator and the slug field and the next one will actually add this into the database So now we're going to do is introduce signals signals are very straightforward and how they work basically if you're about to save something it will send a signal if you're after you save something it will also sit in sin Signal So I'm just going to show you how this is done by these sample so going into your models we're going to go ahead and do from Django D Be done models thought signals

Important pre save and post C OK so we're going to create a couple receiver functions here and that is going to be pre save or let's call it R L as in restaurant location pre-sale receiver And it takes an sender instance args and Q Are darks And will print out

All say saving and then all print instance timestamp And then saving time that works OK some a copy that same thing and then R L post save receiver And saved and then the timestamp OK so now that I have receiver functions we have to match them in so do pre-sales Doc connect And the receiver function To the sender which is the model

I just follow along with me and just copy what I'm doing now explain it as we finish this and then the post saved will just to post save OK So we've got two receivers one is right before something save one is right after so let's go into our admin and just hit save in continue Now into our terminal where we printed things out or saving it and it gives us the timestamp and then it's a saved right there roughly the same then so let's go ahead and go to add a new restaurant location will say new item or you know just a new item will delete it and I'll go I had it saving continue and what you see is saving nun and then saved and all this right so basically what this is doing is right before it actually saves to the database this pre-sale is called which sends some data in this case it's the instance itself so the object we can actually work with after it's done saving it also does this so there is one more thing that we can do after it's done saving which it sends another argument called created whether or not this item was just created that's how that signal is going to kind of work the point of this is if you save an item there's two signals that are called that one is called presale and it comes with some data that you can do some stuff with and there's another one called post save that after some saving it can do some stuff there Ill send the object as well as whether or not it's created obviously the priest save doesn't have that because it doesn't know if it's going to even be created it might be stopped for some reason but basically that's how it works so again this is going to be playing around with as much as possible but the cool thing about this or the priest save receiver is we can set our slot now so I may do here is from you to import and we called it the unique slug generator And all we're going to do now is say if not instance dot slogan then we'll do instance the slogan equals two unique slug generator of the instance

So preceding again I don't actually have to call instance to save here because it's about to be saved so if I said something new here it will set it for me so I can also say if not slug let's just try it and I'll show you instance the new are assuming title equals to another new title Or not title but name rather because that's the name of the field OK so we were going to do that they're going Kryten other new one Saving of another say new item here And what we're going to see in just a moment is it's going to do the saving none saved in the database one or hit saving continue notice that slug was created and it is saving nine saved in the time stamp so that's how it actually works we go back notice that there are now two called another new title Now that's because we said it here now this is different than the post save receiver this one you'll actually have to call instance not save but notice you are calling Save again so that means the post save receiver function will be called again this right here is an infinite loop that will quite literally never stop so we save it and let's go ahead and come in here hit saving continue on the model it's loading and then it says Max or Persian death and that's because it's just looping over and over in this will endlessly because I just saved it so that's where the only other option would be if not instance dot slogan something like this

Is OK because we are setting the slogan And so this block will never be called again right assuming that the slug is actually set so let's go ahead and just not save it on this one And we will do it on a not different one OK so let's go back in here and we'll go into a restaurant spot fish tacos ahead saving ten you notice it does that it did actually call the second one and we can verify that by seeing Saving saves into that a few times but it basically did actually save the uniques signal generator or unique slug generator two different ways on how to do it the way I recommend is the presale of I always always want to avoid saving the instance that's being passed in here another way to you This would be to create a profile or something of the sort based off of this restaurant so there's a lot of other things that you could do but for now I'm just going to leave it in as that unique slug generator and get rid of all of the post save stuff or leave it in its comments for now and then it will be deleted later so if you any questions and signals let us know otherwise we'll keep going So you might be wondering what was the purpose of all this it seems kind of tedious just to create these slowed things well it's really about the UR

L It's making the Ural more robust or at least looking a lot better so in here we want to change this to being a slug and now of course we can use the common regular expressions guide to find a better way of writing slow so if I do a quick search in here I can see that there is a way to write out slug so I don't actually have to know how expressions work to make this work so that's a way to have a slug field actually work basically it's doing any characters and dashes in there to make sure that that slug is actually being created as it should so now that I've got that I can actually go back in to my views get rid of this get object call save it and now my list view I'm just at a link here finally and that is going to be we're going to say a a treath So the hyperlink reference is going to be two restaurants Slash objects that slug or sorry object OB

J Dot slug And then we close that off So that should actually give us to that you are ill we save it refreshing here that's now given a page not found because that's not a valid slug but if we click on behalf of tacos it shows us Chronic tacos is saying non Why's that well that's because we haven't created it yet so hit saving continue and we want to make sure we create all of them just by saving him because we created those signals and another new title Notice it says another new title that's it's on the slug whereas another new title has some hewn eak characters at the end so they are absolutely unique going back into our restaurants we see all of the slugs here I go ahead and click on and there we go I can now see these individual views that is why we did it specifically for how that your real works it's just a lot more user friendly of a U

RL and It's probably better for a SEO all around so that's it we will see you in just a moment Now that we have a better understanding of how slopes work as well as how detail views actually can function now it's time to actually jump in a little bit more to understand how it is that we actually look up individual items from the database right so we already have a way to get a crew set or a list of those items but we want to see how do we actually get an individual item now we're going to do that by using the python shell so it's going to our terminal and notice that I don't have it activated so to pin down where I am I can just go to source been slush activate and do the same thing twice I like to have two windows open so one could be the server running and the other one can be a way to enter any commands I need but in this case I'm going to go ahead and go into the shells of Python as I

PY Shell and we're going to import our model so the restaurant location of course so from restaurants that models import rational location Now we've already seen the crew set which is the model name for the objects that all or I'm for a filter or some like that and that's of course as they give me a list of items now one of the ways I can grab an item from that crew sent is just by doing QS

Dot first or QS That last or QS and Some sort of list position that's a way to get something from a query said Now this is often very effective in the thing that's really nice about this is if you filter something down much like we did in this view so if you were to filter down based off of a category or anything of the sort so let's just do it so you can see what I mean and I'll just do filter category equals to make skim

And I do QS Notice that it's empty so we'll do that category contains Or I act either one and when you QS

And there we go got a few so QS The first on this one might be the same and it is QS The last might be different in this case it is right so again that's a different way in how you can get the actual object itself or the actual instance of the item but that is not technically a good call what a good call is is doing something like restaurant location objects that get it and then you get it off of some parameters you know oftentimes you'll see P

K or ID because of the same field but if we did PK Going to one it gives me an object at with that ID which we could see if it OB

J Equals to this and I did OBJ P

K It will give me that item right so this is what happens this is how you actually go about getting something but what if I try to get something that doesn't exist something like P K E coli to twelve thousand you get this does not exist or right and this would also happen if you To function based view so let's go ahead and just copy this and paste below here I'm not going to actually implement it I'm just going to show you the example so if we said detail view and let's say for instance we passed in a slug or a primary key both of which you should know come from the your realm much like we've already talked about but if we go back in here and we take a look at this now I have an argument that I can work with so I can do PJ You see we can do restaurant location objects dot get P

K Equals to PK or Of course of this was Ideally this would be ideal equals Aidid or it would be slug equal to slug OK so that's how you'd actually get an object's Let's call this object or rather O

BJ and In the context will be objects OBJ

and Then I'll use the restaurant location details so restaurant location details so this actually should pull up the same thing as what we have Otherwise you would have to just change your URL I'm not going to have it that should actually pull it up intel we get a slug that doesn't actually actually exist right so if we got a slope that didn't exist this call how it is currently would raise this does not exist or much like it did here so there's another method called Get object or for all for now it's actually see that inaction and go ahead and import that and we'll say OB

J Equals to get jacked or for all for and then we use the name of the model which are cases restaurant location And then the field that we're looking up so if I did PK Again equals to twelve thousand again same exact thing I hit enter and he gives me an H

TTP Response now it's still an exception but it's handling that and it's returning in Asia The response so that's actually what's happening versus just returning the exception itself so to both of them but notice it says during handling of the above exception another exception occurred so basically it's saying hey there's a four or four Now the detail view does that by default it's built in we kind of already talked about that but I did want to show you that there is other ways to look these things up so much like our slug field so if we did slug equals two I think we did something called

Fish Tacos and hit enter that gives me an object if I try it with a one at the end it gives me the know restaurant location matches the query and of course it gives you that ATP For four so this is the message that would be sent back so let's actually see that error in action so I'll go ahead and run the server Python managed up the wire on server And will look back at Bob's tacos if I add one here at the very end it gives me that page I found and he gives me the actual error that shows up so this is called an exception now there is one more way to handle exceptions I don't want to get too much into how exceptions work so I'm just going to show you a catch all versus how it may work otherwise so let's go back to that initial excess exception if this right here so it's O

BJ He goes to that now we can say Try and then OBJ

and Then we write except and then we'll just say print not found And now it just prints are not found so this is a way to handle an exception especially when you do something like that now a way to avoid this from happening is to use a query set so we just do queries set and then we do objects Russian objects that filter PK Has the twelve hundred so this is clearing the day A base for a Primary Key equals the twelve hundred or twelve thousand whatever the number and then we do Q

S or QS Thought exists and we get false Of course if the Q

S Did exist so if we said if QS Dot exists we could say OB

J Equals to QS That first so similar to how this is but in this case it actually allows us to you know clear the database and then just set the object to whatever the item is in there and that would actually give us a new object right so let's try this with a real example so do QS

Equals two Objects I filter slug equals two and soon the Baja Fish Tacos I don't think you need to I contains here soldiers go off it a lot so if QS Study exists we're going go ahead and printouts took us that first

And when you know we actually get an object there so why am I telling you all this in why is this important Well it has to do with how the detail view and the detail crews that work in particular if I actually filter this down even further so if I actually put a filter here then that means the objects will only look up inside of the crew set much like this So again let me let me set again so if i actually a filter like this then the detail objects or whatever we're passing with is a slug or a primary key is only a look up in that query said so let's actually do it so you can actually see it's all the filter and then we'll do category like I exactly and will say agent OK And now if I go to Baja fish tacos the actual one And press ENTER I still get page not found again if I do to all It comes back poor Now what's the reason for this well there's many Reasons for this that will be very useful in this case you could say I only want the detail few of you know Asian restaurants now you could do something very similar to this where are you are L Changes as we need or we add some other parameters really I can go into that but the main part of this portion which will lead us to what we need to do in the future is we can actually filter it by user so we can filter our own restaurant objects or the things that we have and the things that we want to look at Nats a way to go about doing that and then our detail will still come up in their inside of that query said so we don't actually have to filter down the get recall call so one more thing I will mention is when you do a Get call it's very similar to the query set in the sense that you can do it by multiple things so I can do P

K Has a twelve thousand title equal to some title or I think we call that name actually sorry not a title And you could absolutely do queries just like this this will this will actually look up those things in the same way now of course have used the wrong things such as title it will also say you cannot resolve keyword title it will tell you the only things that it can filter by the crew said method probably does the same so it's actually look if I just filtered it by the same fields it will tell me that I can't do that because those fields none of these fields are on our models none of them are here you know we have this property method it's not actually a field on here OK so if you have any questions on querying the database with a good call please let us know otherwise let's keep going and I would say if this was confusing we watch it and practice because a lot of the good stuff in the query says stuff Probably won't really set in and tell you practice with it quite a bit at least that was how it was for me so we'll see you very soon So we've talked a lot about displaying data and getting data right so that's what we've done so far as listing out these items or displaying individual versions of these items and all the stuff is coming from the database but we haven't actually created these items on our own so other than going into the admin which is an acceptable way to do it but it's not necessarily how we want it to work right so I could go through the painstaking process of customizing the admin I don't recommend that and actually the developers don't really recommend that either because if you do a lot of customization to the admin you write might as well make your own view for it we'll talk about that in a second but the main thing here is the Django admin should be for your staff it should be for you know the people who are on your team maybe a small amount of people and that's where you're really allow only them doing it if you have your end users using the admin it's not really designed for that and some aid for that so if you end up minute delayed in a lot of data even I would even suggest that you create your own version of the admin doing what we're about to do and the big thing here is creating items in the database so actually being able to add new restaurants into the database from our own your L

In our own view so that's what we'll be doing it's really that key part of remembering things right is like having our own form as well as our own view that will allow these things to work so to do this I'm going to do it the long way and the way that is long term not recommended there's a better way but I want to show you the long way so you understand more about how forms work inside of Django and that's the key part here is is how forms work inside of jingo Now we go into a lot more depth then this on joints if you dot com plus projects under the Course Django forms unleashed or even the latest version of that jail forms by and large haven't changed much but what we're about to do we'll at least show you some of the basics of it so you can you know build ways of extracting data from your users or getting data from your users so instead of the restaurant's app we're going to make a new file in here called form stop by I'm actually surprised the Django doesn't generate this by default but it doesn't so let's go ahead and do it and we do from Django import forms and we do class anima call this restaurant Create a form Let's try that again Rio the restaurant create form and it's going to be forms thought forms OK so much like our models we are going to create a bunch of fields here so I'm actually going to go to the models and I copy some of the fields I'm going to copy name location and category the other things I don't need so I'll bring it in paste it in here and instead of models field will do forms touch our field and I'll leave out these Max items here and on the knoll equals the true blank used to I'll just say required equals to false instead

OK so we save that now we actually have a form Not a whole lot going on here so what we can do is in our view we can actually create a view that will handle this form now I'm going to start off with a class function based you so you can see how that works and then we do it right we will put it in a function Could So it's going to import that was so we do from forms import restaurant create form And we're going to Another one another view here will do define restaurant create view and it's going to take our request as usual and as usual or return a template and some context I don't actually need this crease and here is want to copy those things for a quick so restaurant location

And actually I'll just call this restaurant study form bodies to mail OK so the context here we are going to leave empty So we can see how this generally works now the first thing I want to do is I want to actually render this form I wanted to show up in formed So before you that I'm going to just go ahead and create form of HTM

L Instead of my templates inside the restaurants in the form of HTM

L So like detail an extend base and do some of the stuff so just copy that detail view close on out and then we'll say add restaurant as the title and we might as well put that in the H one as well sort of all those other things and I'll get rid of this stuff as well so we want to form here And we haven't added the form yet into context so I'll just show you how to write an HTM

L Form so it's form so form tags and then some sort of input so in this case will do input type vehicles to text Well I don't name to it in this case will be name so name as and title and that's a little confusing soldiers call it title for now and then we'll just do placeholder equaling to your title OK so we're going to do this a couple more times and with look at the form we have location category also text so again this is going to be a location So the field name itself category And then the location and Cattle

Or do keep in mind this is the long way to do it just showing you how to do it so you know OK so we've got our a few inputs here and then we need a submit button soldiers to button type Equals to submit And then we'll just say save OK So we've got a four here now and let's put this view in so Russian preview into YOUR else you can import those are the restaurant critic for you here and I'm actually in a copy of the restaurant list view and add a new one and call it creates The Rio So we actually have this item here so since we have that item there is one thing with our slogan is We don't necessarily want the slug to be

A I don't or we don't want to be create right so if the slug is not none I don't ever want this to be create so if slug equals two Well we'll just say if slug in Unusable or Don't use Then we'll just return that slug again

Now the reason I'm doing this now is because it came up just now so say don't use equals to create OK so again don't use as in the you or else here I have the ability I could have created a slug called create and then that causes some conflicting issues here with the or else now again this is like doing it right now if I were to do it wrong I wouldn't have just done that but in my utility function I'm just going to you know return it if for some reason that creates done it's going to do it again it's going to create a brand new one for us in general OK so and will say New slogan equals two we probably need to change that so I'll just put another new slug Equals two Slug plus random string generator And will put size four

In fact we will probably just do it like this OK so little little way around just making sure that our slugs are good and it's not creating any problems for form that is definitely a preemptive thing and it is something that may or may not come up in the future but it's something that's really important to note because I've seen that issue happen and come up few times OK so let's go back and we're going to go to create And this create page needs to have the right view So we'll save that and we're refreshing here and there we go so now we have the ability to add a restaurant Well we kind of do let's put a title here and we'll call it Hydro and location Newport Beach and category equals to aqua making stuff up ahead save and everything goes away but then you may have noticed our Euro has changed so this is called a GET parameter those are GET parameters and if I print out my request so if I do prints requests to get I will be able to see those printers by refreshing I can see all of them right here the screen dictionary it's showing all that data even though it's not in the form anymore it's showing up here this is not what you want to do

In some cases you might want to do it but in this case when we're sending something to the database we're not looking something up like we've seen here right so that's a good argue that's a keyword argument this case but you could also use it as a good argument if you're not looking something up you definitely don't want to create a form that sends the data to your L Now why does it send the data to your L and That's because the default method of it is Get All caps that the default method of the form and when you go to your L

If you remember this is getting an item so it's a new title here new location new category and a hit SAVE THE get item up here actually changes so it actually adjusts it but instead what we need to do when we actually save data to the database we do something called posts all do print requests post we'll save that and we refresh and here now my terminal I should see two empty things one has those items because of what's appear the other one doesn't So let's get rid of these items up here And now or get request has empty query dictionary and also our post does as well OK so when I say post data I literally mean just flipping this to being called post so now I had save web salutes refreshing here now and save I get this forbidden error now this is because whether cations handle post data and when they handle post data that did it can go to the database and without security in between that data that means that another site could potentially send some data so right here if I was on another site I could potentially post some data to this URL

I could actually send the data to the euro because I see this form here I could do it there is a way to do that so jangle has some built in security measures that prevent that from happening so if I do this it says forbid it's not allowed so that request is aborted so that means is I don't actually have any data coming back see how there's a POST request here but there's literally no data coming through We can see this again by if I do if request method equals to post then I can print POST data OK You can also do that same thing if a request method equals to get Prints get data from print get rid of those query said things are and will refresh and here will it continue nothing should be printed in thing was if I go to the get method I see that the good data was printed right obviously this is also showing those things too but I just want to make sure that you see their view function that's actually handling that So how do we make this security work how do we actually make this form work and that is by using something called a C C S R F underscore token and this is the security token the generates when you're on a page so now if I had save no errors happen in fact if I go to the terminal again I see that post data has been printed so I can actually print out that post data by do it again print request that post and I can see the data that's coming through we refresh it here continue all do new item

New location and new category all of these things should print if I hit save the data goes away but all of this stuff is now coming through notice there's a middleware token that's that security measure that's really important and then we have all of that data actually coming through as POST data notices posted are right there so this is how you want force to work now there are other methods for this one of them being put when I cover that right now it's actually probably not something you'll do for a long time unless you're using something called an API but More likely you're not going to be doing a put at least for a little while so now that we have the ability to request this data we want to make sure it's good and valid but before I even request and make sure that it's good and valid I'm just going to show you again the wrong way to do it and that is to get this data and then so It's a database so also a title equals to request top post bucket and title now I did this doesn't get called but you can do it

Like this and use brackets because it is a dictionary but dock it means that it could be empty and then we also have the few other ones which was location it's basically the same thing in a category which is also basically the same thing that's why it's going to copy and paste that location category OK cool so it's listed again I can confirm form submission or I can just go in here create and I'll actually add a location and this time and I'll say to poll a And I'll say Irvine and Mexican OK I will hit save look at my terminal and the data still coming through all right very good and now that we've got that we're going to do it one more time will say OBJ Equals two restaurant location objects dot create and we're going to create that location based off of this data so say title equals the title location equals to location and then object equal to or assuming the object but category it goes to category cool and then finally I'm going to go ahead and get rid of this

I also don't need this good method anymore but I will just commented out in case you want to look at it I'm going to do an HGV Response redirect so I already have a Shiba response in here and I'll just do return HE

B Response but we need one more and that's called a redirect Return a response redirect and we're just going to go back to restaurants so after this stuff is done it's going to go to restaurants and it's going to show us our new restaurant let's go ahead and refresh in here and new title all say Chipotle a Vine Mexican and I'll hit save Notice that I have a title is an invalid keyword argument for this function so I actually have an error maybe has caught that maybe not should be name not title so we save that I can run a refresh on here I can even confirm form reefs the mission again hit continue takes me back to my restaurant list what do you know here is to pull a it was created today at this time there it is so I just actually created it will try that one more time in a call this place creates

And of this is because of what we did a little bit ago and location Irvine and category none go had it save I go to creates boom that create your will was created that's exactly why I did it OK And then of course none is related to the location that I actually put Now I want to show you all the stuff because this is the absolute wrong way to do it this is very dangerous because you're not actually validating this data and anyway we created this form for a reason and that is to validate the data to make sure that it's going to be valid for the parameters we set here that is something we'll cover in just a moment but if you have any questions on how this portion works please let us know I will say that this is how a lot of Web sites use the work whether cations used to work this way until hackers starting getting malicious and figured hey they're saving data directly from what I'm sitting here so I'm going to send a bunch of crap and ruin their site now obviously going the general method isn't full proof like they constantly are updating security but it is still better to go that method than to go this old school way and all the while the reason I show you this old school way is because this is actually how the forum works it works this way you put it in put some HTML Text here you add some data you had some in

Puts you have a button you have a method and then it goes back to the service so this is true with any form not just Django this works on any form the only Django part on this form is the C or If token which actually gets converted into something different so let's go ahead and go back to that create and we'll go and inspect the element here and it actually gets converted to it's own input the input type it in and there is the input right and that middleware is generated for us by Django OK but a bit longer of a section but I wanted to make sure that you knew all of this stuff and how important it was to solve that slug generator problem of course if you weren't using slugs that wouldn't be a problem and also if you weren't using a different URL That also would be a problem all right so

Thanks for watching and we'll see you very soon So now that we've done it the hard and wrong way it's time to do it a little bit better of a way and that's by using that form that we've created so long ago and the first thing that I want to do is show you why you use the forms in general I'm going to go hang copy this and in here instead of that I go ahead and comment those out and I'll just say form equals to this Or create an instance of that form and we're going to go ahead and do request post so we're requesting that post data same thing and they will say if form is valid Then We'll actually get those things all do instead of request post data will do the form data so form dot cleaned

The data Doc get and this case is titled So that's what we've been calling it except on the form we call it name so we're going to change it on our other side in just a moment so it's going to go to name location And then To category OK So this is taking all of that data in to that form and validating it in here again I'm going to show you what that means in just a moment so it validates it and then now we can say that So this is the jangle way to do it it's better than what we just did it's not the best way to do it but it's better than what we just did so it's going say that back into form of the shemale we're just going to change that first one to name Of course you could try it being title but the name is obviously correlated to this stuff that's going here and also the field names here right so the field names here are the same as this OK so we save it now it's refresh and I'll say you And then location and location whatever I had saved and it brings you back to new item location location OK So that actually is working the form is now Jane Doe is now handling the data so that's pretty cool so I now have just made that form a little bit better and we go ahead and get rid of that and it's definitely a little bit further along but what I want to do is I want to make sure that if there's errors of this form how I can actually handle those errors so what we're going to do is we'll say if form got errors we're going to print out forms of errors

Otherwise we will return this is the response redirect which actually I don't need to do this Else clause I can just put it in the form valid method OK So this is also an else clause so I can also just do else here of the form valid method on request post but I'm not going to do that and you'll see why in a minute OK So this is going to print out those errors Now how do we actually simulate errors without getting into too much depth Well if we go back to for money she Mel and change the name back to type Will will actually be able to see those errors in action and the reason for this is because title or name is required so let's go back in and all refresh in here and hit save Nothing seems to happen but if I look at here it's an era list name this field is required so it's actually printing out those errors right this is form validation at its very basics so we go back in a form and all changes back to being name we say that refresh and here he continue and sure enough it still saying name is required but I just add a new item name and I hit save and it saved it New other names in there I click on this and what you know there it is it just doesn't have a location or a category which is no big deal but that is true that's the fact Well that's fairly straight forward behavior not too bent but I feel like there's a lot of redundant things happening here inside a formulation melike Why do I have to type out the field names twice I have I'm here what I have to have them here well this is another advantage to using Jing a forms is you don't have to do that you can actually generate that form itself or a blank version of that form and pass it in its context and then use Django to do the heavy lifting to get rid of this right now so again if the request method he goes to post is going to do all that stuff so this is how we handle the post method

So I can actually make an instance of this form or instance you ate the form itself and After I do that I can add it in its context right so there's my form and then back informational I can get rid of these inputs I'll just comment them out and I could just do form as PT or As paragraph and then all render out I form actually this right here I'm going to cut out and just take it out of the block in general and then I'll just leave that in there for one video and soon OK so far as P

R one section rather and then I can go to create and what do you know here is when you form this is a Django form now and it has some things that the other form doesn't such as it has labels on it now it didn't before and now it can do new item or rather I can even just say save and it says Please fill out this field so it actually added that requirement in there of course we could also write out required equals to required on and put itself but the form already does it OK so I'll say a new car whatever it save and new cars the new new item there obviously isn't really a restaurant but it is actually creating the form so the back of that view this is how it's done pretty pretty cool now I can actually take off a couple lines here because of how forms are done by so in this case it's In this case it takes in the post so I'm actually repeating it twice I don't need to do that instead we can do is request top post or none and this eliminates these two lines And now I just had this back and now it's actually exactly how we want it to being and of course we could we could render out the form errors inside of context as well so you totally could say errors equal Sue none and then you can put errors equally to form got errors And then we can put the errors and here

Right so we have to put it is not because of this is if statement we have to define it before we can actually put it in context and that if statements are always going to be called so we go ahead and get rid of that now and inside of my form I can just put errors And I'll do that all right and let's go back create None so it's shown None right now and I'll just do it if errors

And if I say that hit save It's not actually giving us any errors because it required item right so it's actually not showing us those errors themselves so what I want to do is use something called form validation and we're going to just you clean name will you self and I'll explain this in a second so do self name because to self cleaned data that gets a name And we say if name equals two who do Will do will raise forms validation error Not a valid name otherwise will return name

And we will come back into our view refreshing here and will say hello as the name will hit save and there's my validation error right so it's actually showing me these validations themselves and how we did this was simply we took the field names and then we add clean underscore in front of it as a method and this is call that when you do Is valid it's actually validating that data and this is a way of how it validates the data so there's a custom way to validate that data it's not the only way but it's one of the ways they can actually go about validating that data so now we look at this we're like OK this is looking good but I think it can be better and in fact it absolutely can be better not just the view itself but also how the form works right so like this portion right here one hundred and ten percent can be better and that something will cover very very soon but this is the power of using Django forms over using standard HTML

Forms and hopefully it'll make sense of it doesn't please let us know otherwise let's keep going So now we're just going to blow your mind and make it even better like it's going to be ridiculously better and ridiculously easier so what that means is I'm no longer going to be using this view at the end of this video but the first part I'll actually still use it so you can see how to make this even better now instead of my forms I'm going to go ahead and import so from doubt models import or restaurant Cation and we're going to use something called model form Salut class and I'm going to call this restaurant location now create a form and it's form stop model form and we'll just do class and Medha and model equals to the model name and then the fields that we want so what are those fields let's go ahead and take all of the model again name a location category I actually don't want slogan there soldiers to name location category And name Location and category

Cool now that I've got my form designed let me go back into my views and I'm import this form and will just change the form valid in the form itself to the restaurant location create form and now will do is a comment this out if form is valid format save So we save it and the no redirect Let's go him go back here and I'll say another new awesome form and another location or whatever save and another new awesome form well I just got rid of a bunch of stuff here gone right lot more effective a lot more efficient then that last one but again I told you that we can make this even better actually A little bit more in par with this detail view and that is something called a create view So scrolling down we're going to put it down here and say class a restaurant

Create A view and it takes in the Create view The Form class will be equal to The restaurant location create form and they will say template name equals two Restaurants

Herb restaurants Let's form about him L Am going to say that from him I'm going to copy this go into your L's and we're going to import this in I can actually get rid of that other one because when I can use it anymore and I'll replace it and the do as view as normal will say that and let's go ahead and look at it and we've got to create view at a restaurant where you know looks the same doesn't yes it does so now this is going to be create view location created view and save and I get this error right so I get no your all to redirect to now if you remember how our view looked we had this a sheet of here were a sponsor return redirects we actually had a success URL

So now there is saying define a get absolute you're all method not going to do that quite yet and steady we're going to do is just say success you're L Equals to that OK But before you even try it again I'm going to go back into my restaurants and notice create view was created and actually made it that new one so do create and will do success your well done here save back success your L Look at that four lines versus you know like ten or eleven I didn't do a whole lot of customisations Now if you were to new customers ations in between form valid and form save you could write so you can customize here but since the model form you might not want to customize there instead you might want to use hey signals you might want to use a signal inside of that method right so when I was explaining signals this is where that customization come through this is kind of how signals work so it's kind of like like a pre save is right there And then like a post save is right there now of course you could customize the Create view to you do all sorts of things in there but that's essentially what we're doing we just overrode this entire thing one one thing I would say is if errors is no longer errors it's actually going back to being form errors instead of the context that we called it so if I say that and bring back that clean validation method

I can come in here and try that again so go to create and ologists a new We had saved and there validations back it's working again it still works on this one because this is still technically a form it's still technically just like this it just added one extra element which was the ability to save things like that original form you can save but now it's working specifically on this create field and we get rid of some of these comments and here we just don't need them anymore if you want to see them just go back older commits or or on the lectures him self or the code inside to get up but now look at it like look at how much how little of code this is that's really just not very much I mean granted we have to make templates and the year else too but overall to actually handle this stuff it's fairly straightforward OK so We still want to talk about a validation that is how this this like create this part right here why is that validation working rich show you a few different methods of getting that because saving the right data is actually really important when it comes to building your application of course this is not a very good example of it but you do want to make sure that you are saving at least valid data validated data or like clean data when it comes through and saves on your database so then you don't have to do anything in the future like trying to change your fix it so one of those things being like which we're actually not going to cover this because it just gets into too much context or it's just too in death and too involved but one of them being that maybe I would want to validate make sure these locations are real locations and I might want to make sure that my name of that restaurant is a real name or the category only fits in certain categories and that's actually what we're going to be doing we're going to make sure that it's only a certain set of categories just to show you how that works or I So if you have any questions on this portion let us know granted there's a lot of build up to this but it's actually very very easy to do I just want to give you as much context behind it so you can understand and appreciate how well done this was as far as the creative you hopefully the video was to but create view that is just I mean the developers behind just think of everything and that's something that I'm just so appreciative of is as learning create classes views and how much easier they actually are OK so thanks again and we'll see you very soon So validation Jango validation is a big topic for enjoying all forms there's a lot of ways you can go about doing it so I do recommend that you actually check a little out the documentation only because there's more to it than just what we're going to be covering here this there's a lot more to it and it's just going to take some time With you playing around with it and luckily actually it has a lot of different you know examples of what you can do with validation which is really cool so what we're going to do here is kind of extend what we did on this forms clean name validation stuff now I already mentioned that when forms are called they go through a series of allegations right so it's basically in a call any custom validations you have but it also has it's built in one so if I did something like e-mail this is not practical to use case but if it forms an e-mail field

And I just save that e-mail field that says go ahead and say that and then go back into that create view if I looked in here I can see now that I have e-mail I can try and do something that's not an email field and all had save We need a name I mean automatically even the browser is kind of validating whether or not it's an email field so you don't want to make sure that it's a valid email and otherwise it's not going to allow it right so in this case it's enter ballot e-mail address so that is how these kind of Django built invalidations work also notice that the actual validation message is showing up right on the field using the form fields so really we don't even need this if forms we can say there is another one called non Form errors non field errors I believe that's what it is will actually render those non field errors out instead so non feel there is meaning errors related not to the field themselves but to some other thing going on in the actual validation so when we think about this that's the built in methods but this is a custom method so I can also do it where I do define clean in Mail and I'm using e-mail because a really easy way to kind of check how this stuff works and I'll just do e-mail equal to self that cleans data that gets e-mail and will say if e-mail Are let's say if you do you in email we will raise forms validation error saying We do not accept edu emails

So if you do you an email yes this conditional can one hundred percent be approved and I recommend that you do a quick Google search to see how you can improve it but otherwise we can just return this e-mail and I'm really just doing this for an example so I'm going to go back in and will say name is dot edu and also ABC At AB

C Dot edu I'll hit save we do not accept edu emails OK really really easy right it's not really that complicated as far as how this actually works now of course I'm not going to put this field in here because we don't need it instead I want to do is go one step further and actually validate this category itself so when I want to add in a custom validator I'm going to go ahead and create a file for it inside of my app called validators Validators pie And going back into that documentation it's on balladeers we were going to go ahead and copy their example here OK so I'm not going to worry about translation in this but I will at least have my validation error showing up and the you some some parameters are string substitution I'll worry about this in just second but basically this is the core of the validation it just takes in a function and then validates whatever that value actually is so I'm going to just you know adjust this to that clean email one Walt let's do it with a clean e-mail one first and then we'll come back to that category so you come in here I'm going to show you the example of not actually an implement it but basically

Instead of forms that validation error will just you raise validation error and e-mail is now equal to value and we don't need to return anything best just how it validates it just like that OK so now it's two categories with say categories Equals to a list of categories so we'll do Mexican And we'll do Asian American and what it Called So now we've got categories will do define clean sorry not clean we don't want to call it clean anymore Actually we would rather call it validate you could call it clean if you wanted to but this is an actual About nation where clean is usually associated to a method on a form not just general validation so it is that validates category so the category field itself these two again are examples but this is actual what we'd be using and we say if value in categories return or

Will we don't need to return a thing and so will say if not value in categories then we'll say raise validation error this is not a valid Category Or even better we can use some some substitution here and we'll put Value and used the F

So we go in and copy this imported into forms we do from the validators Import validate category so down here I can actually override the default of that field itself by doing forms but char field and we do validators he goes to follow the Category and also say required equals to false Because it's still not required save that and will refresh and here OK so I'm going to do name

And a new category that's not in there and I'll just say location and height save location is not a valid category Very very easy well what if I did make skin a lower case As in Mexican food I hit that Mexican is not a valid category although it is it actually is a valid category so I might want to like make this a little bit more effective or efficient so I'll say if value not in categories And I'll just give the value soul to say cat equals to value capitalized Or

So if Value not in categories and not cat in categories so that it continue to use A little spelling error on capital There are no refresh continue And it's looking like it that this string substitution can't do that

So that is a method that often is working Maybe it's catalyzed it's There it is catalyze OK so after I did catalyzed I just allowed it to come through and it actually is saving it so that's checking whether or not it's in these kind of gory or the capitalized version of it is in that category OK So that's one method we could actually put it right here on the Form itself or we could go one step further and put it directly on the field itself so again we will import these validators so from the validators Import and this is validate category

And we just add it in as a char into the char field itself and say validators equals to a list though that category being one of them I made some changes to model so what do I have to do my have to make migrations of Python minutes of he why I make migrations and Python minutes of the why migrate now that I've got that let's go ahead and go back into my form forms up high that is on a comment that I'll save it and will run it again Go to create and I'll say new name another as Category A hit save another is not a valid category and I'll try Asian lower case i had save what do you know it works so I actually from really want that catalyzed So let's go back into our validators and can we actually change the value to being capitalized let's just try it some of the value to catalyze leave it in as can't actually the value equals to cat so I want the catalyst version of it so let's try it out man else a new item Asian again hits a new Asian item how that we hit save Asian item it's still returning the back so it's not actually returning that that catalyzed value let's just try it this way And the reason I'm doing this is to show you that this is how I actually try things out I'm actually trying it in real time right now I don't remember this because it's something that you don't need to but it's something that's useful to see so your case Asian hit save still not working so all these allegations are doing is actually checking to make sure

Or that this is true it's only looking for a raised validation error that's it nothing else so when you want to modify the data itself you can do it inside of the priests a receiver or the post save receiver either one so what to do now is just say instance and I can save it every single time if I wanted to instance that category equals two instances category that capitalized Or catalyzed gives me not the And let's try that again and I'll make yet another one The elevate And we'll call it makes

It save validate Mexican and kept alive what do you know there it is OK So that is some validation This is little bit more advanced stuff but it is also very very useful we now have everything we need to create stuff it is not really a whole lot more you learn to create things I mean yes you can make more advanced validators You can also know that you can validate everything with just clean and so just a clean method Well actually validate everything but you can look it up in the documentation to see how that works but by and large validating models and also fields themselves is very easy and also very useful now will say that if you actually start creating a bunch of validators you can even create your own field itself that is different in a lot more advanced but you definitely can create your own field there's absolutely nothing wrong with it and that's how code works and that's what's so exciting about it is that you can make changes however you see fit it's completely yours and you can modify Jingle All You Want All right so if you have questions on validation let us know otherwise let's go So how we are right now we have a list of restaurants that basically anyone can add to right so if I just go to restaurants create no matter who is logged in you can actually add in restaurants and this would be true with any other sort of model right so we would have restaurants would have a list of restaurants all of these things could be added by anyone in some cases you might want to do that but more than likely what you're going to want to do is associate this data to a specific user so once that users log in that's the data they see so like this would be my restaurant list and it would have all of the restaurants I like and maybe even the count of items that I actually have added to those restaurants and of course I'm referring to menu items so I get to have fish tacos there's a couple things I really like getting there and they have some sort of a ring to them as well so I want to have this for me as my own user but then every other person who wants to use the service would be able to do the same sort of thing so that's what we need to do we actually need to start associating data to a specific user and it's actually not that complicated it's just a matter of how we associate these things now the model itself has nothing built in currently to an association to actually create that association we use something called foreign keys so if East do occur Google search for Django foreign key there's actually a lot of information that can come in here so we could go on the either many to one relationship or the model feel that reference in the foreign keys themselves is just a model field and it works in the database so there's actually an association in the database that will happen as well and we're going to look for foreign Key so that option there and it's going to take us down to the actual field itself so this is the model field that we're going to be working So when I say Association I mean literally it's like ownership you know once we make an association that association sticks until we change it and so we can change how our query sets work based off of that association so let's go ahead and create our first association on our restaurant location and I'm going to add a user model here and it's going to be models dot foreign key And we wanted to read to the user model now what is the user model and how do we know what that is well of course we can also do a search for Django user model and that will actually bring up contra off and this will actually show us the user model now you can also learn how to customize this user model with the default user model is this one right here and again you can see a custom user model here now how do we actually get this I'm just going to show you how to do it and that is we can go to from Django dot com import the settings and we're just going to say user equals two setting off user model now this is a failsafe to make sure if you do change it to your own custom user that's how you going to do it versus actually importing the user model itself this is the way to do it just believe me on that one OK so we're going to associate the user to that one well I'm not going to call user for now I'll just call it owner so so we don't confuse things at all but in the restaurant location we have a now field called owner and we have it associated to a foreign key called user Now if I say that it's and we do our migrations like usual it's going to prom for something as you may guess what it's going to probably for and of course the same anon at Knoebels so it can people NK field or blaming the database that is or knoll in the database

Owner is added to this model but we don't have any default or we don't allow it to be no Also we can provide a default in this case I will provide a default and I'll just say one one as en user ID equaling to one and the user ID equaling one becomes even more clear if you go into this migration folder and search down to this restaurant location owner we see that we put this default as to one and look at the default for a few other things here right so this is how these migrations work it actually has some history to it and it has this on delete method here which is done by default and then where this model goes and that is to the settings not off the user model and of course if you had it a foreign key to some different model you could do that right so foreign keys work with models there's definitely a lot to foreign keys so do check out Django models unleashed on our site on joins if you dot com to learn more about foreign keys because because again there's just so much to it that we created an entire series for of it the general idea is we're associated in these two items together and now it created a way to have this I mean this can't be knoll now so this is no way this can be blank so we actually have to update how we create these in our database shortly but for now I'm going to go ahead and run the migrations of Python as of the why migrates we did make migrations we had the default and then we migrated into the database I already have my server running some to go ahead and jump in to the admin and actually already in a restaurant location here and now I see that the owner is see a theme I have two users in this actual database as we see here so those users are associated Now to all of the original restaurant locations so literally every single one is associated to that user So how do we how do we actually get this association well There's a few different ways on how we can we can find out what this user own right is what restaurant locations this user is associated to and how do we do that well I'm going to do it in the shell first to show you how it's done and then we're going to have to adjust our pages to compensate for now we have these listings based off of users so let's go in the shell My thumb is up here why shell and I want to import the user model now so not the Russia location but the user model itself so do from and there's going to be Jane go dot com Tribble off import get user model and then say user equals to get user model Get user model in there so user the objects that all that is how we're going to get the user model now of course there they're kind of different Right so this right here is just grabbing the name of the user model and it's using that as a foreign key in this right here is actually grabbing the user model so we don't actually import the user model itself and again it has to do with custom models so when you get more advanced and you want a custom user model which we do cover as its own series as well you could do that but right here we just want to make sure that we can get any individual user instance much like we've been doing for the location and as we see here this is a query set writes a quick set of users and that very first user or rather will just say see if the user equals to use or not objects that get idea equals to one member we set that as the default in the restaurant owner location the foreign key to the user model the default being one or that single that ID of one right so that's this user So what do you see if the user and there it is right so I can actually get this Users stuff so user to user name you see if you user I don't think I have an e-mail but I can get it and there is no email there and so on and you can even try to see if a user to a password and you get a hash password and I can get the raw passer That's how Django has things built in by default and again that's why you would be using their user model and if you ever do some customisations you never ever want to see Iran pass right here that is a grossly unsecured project OK so now that we have this I can get the direct associations to that model and give the direct associations being if we go in here they are these things right I can get all of this stuff active so let's try that user model active I think it might be is active actually instead of just active so is active and true and then is staff true and so on right you see you can do all sorts of things as far as the Association is concerned but we have this new association which was related to the restaurant location right they are associated there associated both ways and I'll show you both ways in just a moment but first we're going to show you the reverse way meaning we're going to use the user itself to get the restaurant items that are located again that scroll back up I only have imported the user model I haven't imported Russia locations at all and that is how this is going to work or actually go back and get those reference locations that are associated to this user Now the method of how it's done is we grab the user instance in this case we call it see if you use or you know that's the user instance right there of course if I just said OB

J Which is something sometimes we do stuff like that or instance See if the user that's another way we might call it's right so all of those are just an object from that user model so we want to get the relationship to the Restaurant location model right we want to grab the associated data I know I'm being redundant with this but it's kind of important to understand how this works and it's really simple we just do instance dot and then we get the reverse relationship model name so in this case we call that restaurant location and then I can say to all let's let's just try this way First I'll hit enter and I'll say it has no attribute restaurant location Now the reason for this is because we have something called a foreign key or here instead of a one to one field a one to one field would have this association they were very very similar but they're not the same so instead what I have to do is use restaurant location underscore set so underscore set will get me all of the associations from that particular user to the restaurant location and we jump back and I hit enter and what do you know here's a bunch of restaurant locations and yes it is a query set so that means I can do queries that type things on that too so I can yes I could filter and we could say category I exact equals to Mexican And there we go we've got our restaurant locations that are Mexican that are related to this particular user and we never imported this restaurant location so again I'm going to come back in just overview what we did here first of all we added this user as an association to this rational occasion by way of something called a foreign key there are a few different types of foreign keys but that's the one we use this is so I can have a Thousand One user can have a thousand restaurant locations OK once we do that we actually did some tests and we imported this user model in and then from that user model we did a get call and then from that instance of the user model we were able to call all of the associated restaurant lowkey

Ations Now this particular user has restaurant location so if we actually look back at our user the objects and all to that second user so just go ahead and do get ID It goes to two that gives me my second user so say J M A user equals two this and then Do J and User dot restaurant location set to all I get an empty query set and that should make sense because this restaurant location this user doesn't have an emotional cations set up that course that something will do but it's important to know that that's how you do it now how would we do the call if it is different like what if it was called I'm not going to run the migration and I can save it but what if it was just called restaurant well simple we would get rid of the location we'd keep underscore sets done all this part makes that foreign key call

You know if it's called ABC You would do A B

C And that's only if the instance they were using has a foreign key in there if it doesn't then it won't actually do that OK so let's keep it in as a restaurant location we'll save that and then we'll go ahead and exit out of the python shell and then run it again by the minute of the why shell now we're going to do is we're going to get restaurant locations themself based off of a user ID So do from and restaurants Top Models import restaurants Location And then we do restaurant locations the objects that filter and we'll say user underscore his her ID Eagles to want we hit enter and it shouldn't be user should be actually owner so let's try that again

Owner and notice it says Phil exception can I Resolve keyword user into field because we don't have anything in here called user it's just owner right we changed the owner so I did underscore underscore ID because this is actually accessing the fields over there I'll show you how that's done and say OK So that gives us that query set so it's actually another way to filter this down so I can also do it by username right and I could say I sacked equals to a string like that there we go so that is how you do it so you all you own are right here is associated to the user model username here is of course that field in there and then I exactly the way to look it up so another thing we can do is actually get the user model from the location itself and this is a little bit more advanced but I want to show you it because it is it's they are really actually connected again in this shell I have not imported the user model and if you don't import the user model is say something like that user not to find users not find so again we've got this QS Here and I'll just say OB

J Go seek us out first And now OBJ

Does owner equal to a user model rather user instance so I can actually say user equals to OBJ Dot Owner dot underscore underscore class now that is the user class as we see here so I can actually do user to objects to all they are absolutely one hundred percent associated everything that I just did here can be done with the user model in the reverse for Russia location let's see that so I'll say see if the user equals a user to objects to all And see if you use or restaurant

Location set to all We got to see if you use or should be not at all but first Actually let's keep it up I kept it as if user to all try to do it there that's why it's a queer set object has no attributes which it doesn't it's only on the instance so we try that again and they're real and so I change it as the first user and then gives me all those rational locations again and from here again this is a new QS Equals to that

New QS or A new object equals two new QS That first new object what is going to be hopefully you know Yep it's a rush on location and I can get R

K It will soon like restaurant class new object Underscore underscore class and R K objects all whom they are associated one hundred percent associated OK So hopefully that clarifies on how they are so should how foreign keys can work inside of models there's a reverse and forward Association and the reverse association is always on the field that says foreign key user right so whenever you see a foreign key and then some model name that means to get that object we have to get the user whatever that user is or that class is so class instance Dot model set to all that is how you do it from grabbing that individual class so hopefully all of that makes sense if it doesn't please us in the comments below or even more specifically you might check out general models unleashed to really get a better understanding of how that works in particular it should make more sense once you play around with it yourself and start adding other models and other foreign keys because you can absolutely do that all right so in the next one we will or Shortly will actually start clarifying this a little bit more by implementing in our views

OK So we have a Russia location and it's time that we actually associate this data when we're saving it on our view if you diverted at all and went to create and try to add a new location again You might have received something like this not no constraint found right so say in restaurants the app the model name and then the object itself or the association in this case it's owner ID which is related to this there's no idea associated to this particular user Now if I set a default here it would go off of that default I'm not going to set a default because in this case we definitely don't want a default so what we want to do is actually set this user based off of the requested user now first and foremost I want to make sure that my users logged in we will do authentication here shortly but I want to make sure that logon at least to the admin right so you know the admin and you log in to a user in this case J Mitchell three is logged in not see if he OK So back to my views how is it that I actually save this like I've got this create view here but I don't have a way to save my user so before I actually go in to the they create view I'm actually going to bring it back and do the restaurant function based view so we can see how it's done in that first and it will translate over to our restaurant preview which will see in just a moment but let's go ahead and grab this crave you and were to go into your L's and of course I want to make sure I import it again And we're going to replace the other one I'm going to comment it out The next

Parentheses and comma OK So we've got a restaurant create a hit continue the same sort of thing will happen when I try to do it again after the name location and a category that works I had saved same thing happens right OK so back into our view What I can do here is these four valid method is how I'm actually going to go about doing that and I'll save If request that user is authenticated Then we'll tab all this stuff in And well I almost want to save it here so I want to say user equals or request the user I'll explain the request that user part in just second but we have user equals to request the user here

And we just checked if they're authenticated So what we need to do is turn this form into a potential instance or an instance that's going to happen that just hasn't saved it so that means is I mean as you say instance equals to form does save commit equals to false so we're not quite saving yet but I do now have an instance so then I can just say instance Dot owner because it's not user it's owner is that and then instance that say this is roughly the same thing that I just had but now I can attribute onto this instance that is I can set something like owner on that instance itself so we go ahead and save that and what I also want to do is if the user is not authenticated I probably want to make sure that they all are right I want to have some way of making sure that they are that's something we will cover for the Create view itself in the future but right now this actually would sort of cover it it just won't validate the form or the formal validate it just won't do anything you want to show us really any errors that happen as a result So I'm going to just add in an else clause and return redirects to log in this is not the best practice but it's showing you how you might consider doing this or some intuitive way to do it so request users authenticated then will save the data and will associate that user Accordingly I hate continue hoops we spelled authenticated wrong so will say is underscore thing to K Did there we go

Try that again was missing a T There And we'll just create a couple new ones and I'll say J Mental three Newport the church and America are well I don't think that's one of them sold just Asian save and they're going to go into the admin look at rational locations look at the last few of them I see that Hey look it's actually associated is associated correctly to that actual user now to open up an incognito window with command a shift in or you can also go to file new Incognito window and go to create an image try and make a new location new look and a location and category and a hit save and it goes pays off our But it redirected me to log in OK cool so that's essential how you're going to associate this inside of a function based view if your questions on how to associate users to a model used in function if you let us know otherwise this could go Now it's time to associate a user data to a Class A few folks or a few is a little bit easier to do it because it's fairly straightforward right we can just put in this clause if request user is authenticated then we come through here anyways but I'm going to go into the Create view and override another method and that method is called form valid

In self and form and in here will just do pretty much the same sort of thing as we did appear And instead well we'll just copy this exact same thing and we'll see what happens so what we're turn the super call of restaurant create view self and form valid form OK So the arguments are self and form and then we have to call that at the end so I actually probably don't need this and I probably don't need the I probably just need these two things so let's go ahead and try that out all go ahead an import it or make sure it's still imported it is am going to change it back to what it was we saved that And we go back into our create view and do new name Location Newport and skin will hit save and we say request is not defined OK So first there we go back into our view right here the request is not find Now the request does come through on every single Class A few but instead of it being passed into individual functions we have to do self that request so that's it self that requests the user is that we refresh and here I hate continue and it looks like it worked I got a new name there it is we go back into our rational locations new name what do you know OK so how do I know to remove this well basically what it what the Create view does by default is it runs a form valid method and at the end of that form valid method it would do that save so much like what we have up here before it says form is valid it's basically doing form got save and as default that's what

Happens so when we come in here and say form save commit equals to false This is calling for about savings doing roughly that same thing but it's adding any other functionally That may be built into that form valid method So potentially a little complicated but really again we're making that instance from the form itself because it's a creative view that means it absolutely has to be a model for me can't be just a regular form and then from there we go ahead and save this data or rather it can be a regular form but it's not going to work exactly like this if it is and again we can do these priests a VIN all this other customization inside this form valid method if we want it but I wouldn't recommend doing it much here other than something like this OK so that's how we do it on both form valid on the Create view and the be the class if you and I function if you we have those down and this is true for editing things as well it's pretty much the same thing but the next part of this is more specifically is like I shouldn't even be able to see this form if I'm not logged in right so when I go to create I must be logged in to actually be able to see that and that something will do now there's actually some built in ways to have that happen versus what we did appear where we did this redirect and yeah I could probably do that redirect inside of this form valid method instead like that but again even like I said in the last one that's not a good way to do it it's actually better to have a log in required to even see that particular view now there are instances of course that you may want to show the view but you and even the former but not even allow them to submit data so there are instances you may want to do this but by and large I don't think that's the case I think more than likely you're going to want to have them logged in to see this View Now the other option of course is just render the form out if they're not logged in and then have somewhere on their same like blogging to continue or something like that stuff with a lot of different things you could do with it anyway so let's actually go through the requirement of logging in in the next one Now we're going to do is show you log in required both as a decorator as well as a mixin So the way that we're going to have a logon required is to wrap this entire function in something called a decorator when really we go into decorators much but basically it's a function that wraps another function so it has to meet some criteria before it's going to show this all that simply means is once we have this decorator it's going to it's going to require you to log in otherwise it's going to force you to a log in page now luckily jangle has built in for us there is a way to do it but the logon decorator is by far the easiest way to do it on your own so I'm to go ahead and copy this logon decorator

At the very top I like to keep these things knife nice in alphabetical and we use logging required use at logon required and that's it so we save that anime go back into my Create view but I'm open another Incognito window where I'm not logged into now enter and looks like it's going through still and I know why and that's because of our URL I was going to undo it here and make sure my function based view is actually there and I'm go ahead and copy this since we've been using both of these quite a bit I'll just make a whole new line for it OK so my comment out the class a few one and will refresh in here notice it takes an account log in and then it's their rights it's actually taking me to a different view to make sure that a log in vs what I'm actually logged in which is here

I can actually create something again so that is how you do log in required with a function based view and of course I can also just say log in just like that and let's go back to restaurants create And hoops no we don't have it like that's let's look at the documentation Scroll down it should be log in a URL

Equals to that Ever go to an actual past parameter you can also use a redirect field name which is a convenience function as well OK so let's make sure the servers running that's rerun it again and we refresh in here and again it's taken us now it's taken us to the log in your elf OK cool so that's a way to kind of have a log in required in the just where it goes and then on your realm you would have your own view with don't form to actually log somebody in now what we want to do is the same thing on our cost base view now it's not really that much different so we'll go ahead and say log in at required Mixon and this is mix ins are for class space views so what they do is you just add it into your class a few and then it adds in all this functionality to it so I go ahead and copy this And again paste it up here and again will paste it right below this one based on alphabetical logon or choir makes and I stroll down right in front of create view I just put it there with little comma we say that and now we're going to go ahead and reactivates that your L Right here uncomment this out comment that want to save it flip back to Chrome and also go to a new Incognito window I'm going to copy this U

RL Go in to create hit enter and when you know it goes back to accounts log in and then it Directs to the creative you again so what we have here is the ability to also just say log in your L and Then put that same log in your Will we can save that paste it in and again it's back to what it was over there now if you want to actually have your U

RL To always be on logon what we can do is actually eliminate those And we can go into our settings so in base dot settings we can actually set up our log in your L Here so say log in

Your L and We'll just put it in this log and say that we paste in here again and what do you know it's log bytes change it to ABC I save that

And we pay set in again it goes Dave you see OK So that can be a default now of course the view itself will override this ABC One two three four whatever so the itself can override it if you wanted to go to a different logon page which is definitely an option especially if you're doing some sort of landing pages and stuff like that but in this case we're just going to have a default log in and that's pretty much it I mean that's how in this case is the decorator and then down here in this case it's a mix so two different ways to have this log are required on two different kinds of views again we really prefer cost based views but I like showing you them side by side especially early on not really to confuse you although it might be doing that but more to show you that there are many different ways to accomplish the same sort of thing that's really what it comes down to and what I'm showing you here are a couple of those ways that we would do now long term these these are the methods on how I would use this is what I would be doing Because it's less code and it and it's also a lot more simple and simple in my opinion is better than complex if you can make it simple but still effective make it simple OK So that's it for this now we have to figure out how to actually do that logon process and that's something we'll do here very shortly Stay with us

OK OK So we have this longer required but why what's the purpose of requiring somebody to log in hopefully you know this intuitively but if you don't let's actually just take a quick look at it I'm going to get rid of this log in required makes in re quick and we have saved this just make sure your L's are on that clock face view and we're going to go ahead and try and save some data now in here I'm actually logged in over here I'm not right so I'm on a Incognito window right here and I can double check that I'm actually not logged in by coming in here and saying admin and who's not that but admin and it's going to prompt me to log in OK So let's go ahead and create and know this I can at a restaurant here I can say a new restaurant and location Mexican and I hit save and what do I get I get this simple easy objects anonymous user must be user instance so when you're not logged in the requests to request a user is considered a different class it's considered anonymous user so it's a different model altogether it's not the user model it's an anonymous user and that means that you can't associate it the same way right because it's not the user class you can't save it as the user class instead it's a completely different class now if we had anonymous user here yes you could say that but we don't we have user so that means that when we do the logon require It makes in the choir and it makes then It ensures that this right here is an authenticated user and this is a logged in user there's no chance that it's not because of this right here now assuming you didn't customize anything or change some of the default features that for sure it's safe we know that this is going to work there's no doubt in my mind that this would work one hundred percent of the time and most are some major hack or something like that but that's also not going to probably not going to happen at least not anytime soon OK So let's say for instance we've got this now we've got this long required but we can actually log in we've created the URL

For it we've done all that stuff we actually can't log in so if I go back into the documentation for using the Django oth system right so genuine authentic Asian system course of I did a google for that Google search and it's going to find it You can actually search for log in view So logon view is actually its own class that's new in jail one point eleven so it's a view class much like what we've seen before so it's a class based view that we can do exactly what we've been doing with but in this case all I have to do is import it on my or else so this is a shortcut to where it is there's also of course if I did a search for this I see that here is an import specifically for the URL So I'll show you how they do it and then I'll show you what I normally do so let's go ahead an import it like this

So this is importing in all of the views and then doing just a subtle thing here I think that's too much right I think instead you should do Dot views and then import the view that you want in our case we're going to import the logon view and we also change the your L To just being logon right so the bears are your L And now I'm going to put it right there right of course you can use accounts log in but this is the Ural we're working with and Alpha refresh back in here What should happen when I load up create it should actually have my log in required links and back so let's make sure that saved their go and now refreshes as this template does not exist rush registration logon we don't actually have something for that log in view and since we're not really customizing that view we're going off of the default here I'm going to go off the default template location right so we have these templates here I want to make a new folder and I'm going to go to the error and say Oh hey it's called British ration so let's call that folder a stray Sion And then in there

We're going to call it log into Mel again much like it says right here now going back into documentation if I go back to where that log in view was the class itself this gives me all sorts of things here right so I'm going to show you how to override this in just a moment but we have a bunch of things or actual views that we can change we also have what it does you can read about that but basically if you have next in the your L It will log you in directly were new to notice this next this right here a log in view by default will go there after you successfully log in and another part that's really cool is it already has some template code for us so we can copy it and we can just bring it into blog and we save it and we go back into our template does not exist in cognito window refresh and we got reverse for a log in not found this is not a valid view pattern or whatever so what that means is there's a shortcut method to actually having here Your else if you scroll up a little bit you'll see a an example of a full you're Well that is first off a regular expression then a callable view as in a function based view or a class issue as a function and then finally this name portion so this name portion is a shortcut to allow us to go to things so if I name this to a logon this is a shortcut specifically to that log in view so I refresh it here and now it's showing me hey I don't have something for password reset OK so let's go and take a look Now a big part of the reason I don't have something for password reset has to do with this code right here but if I scroll down a little bit there's probably a password reset of useless go ahead a new password reset View

And or you know it's right here so password reset view it also has a password reset email it has all the stuff all if you actually play around with that I'm not going to touch it right now instead want to do is only get rid of this right here now does it says as soon as you have set up the password reset view and your URL Configuration we don't so I'm cut that out save it and all go ahead and refresh and here and now my log in view actually shows up now that you're shortening or that name hoops meaning close that bring a bag open the Ural name portion this right here the reason we saw that error Look at that error again we see this error right here we can actually see it inside of the code itself

Let's look in here Looks like this error is not being rendered on this So I'm trying to look for the URL

Rendering code itself it's not showing it but I do know that it's in the logon page because that's where we added it and that This URL Right here so this is that shortcut method to the Ural name so when I went back here and I called it log in if I call it logon A

BC I can actually adjust my Ural here to be log and ABC

Will cover that more the next one but it's just made to show you generally how that those naming functions work so refreshing here and now I can log in again I had logged in and now I can at a restaurant and notice it directs me so that's a built in view to Django it's completely built in so what I challenge you to do now is to implement the password reset view very very similar notice it has a template name and it's called password reset form so I'm giving you some hints here and then also the template context so the context variable is in a template is a form and it says C Form plus above for resetting the user's password so it shows us the class up here of the class that actually are sent So the last thing that I will give you a hint for is that template will have something in it It won't look like this but instead I'll go hunting just get rid of this we can actually do form as P We say that refresh in our log in so let's go ahead and go to admin log out and then log in there is our log in so it's going to have something very similar to this just to keep in mind OK So we'll see you in the next one

So now let's improve our navigation we talked about that your real name in pattern stuff like this a little bit but now it's time to do it even better and we're going to do is we're going to jump into are based on HTML and Are excuse me into snippets and now have the mail and this is our current navigation bar when we use something called a for if that is a

Hyperlink reference to some sort of page right and in here I can make it smarter and I can say your L and I can give it some sort of it your path in fact I could do it with all of these and I will so this quote so the thing that's in there is going to be based off of these names so my home page I can say name equals to home and I can do this same thing over and over again so we'll do it for restaurants We can do it for restaurants again we'll call it restaurants Creates Then we'll do it for about I'm not going to do it for the individual one quite yet and then we can do it for contact

OK so going back into our now Bar Mitzvah changes to home About Contact we say that we refresh in here and if I actually look at the link so you're going to look on your screen down there you'll see that this is about this is contact This is home And if I open it up that will be true now what if I change one of these your L's in your old Such as if I changed HOME TO BEING A B C We say that we refresh in here and now if I open home into a new tab it goes to the U

RL ABC

and If I go here it's no longer there so this is a nice convenient way to make sure our Urals are working all across the board it's something called reverse the reverse pattern to actually go to that in particular so the other one we can do is of course restaurants And now I can actually use this anywhere so I can just copy and paste this everywhere I want to instead of trying to remember some URL Pattern that may be fairly complicated

So if I refresh again I can now really navigate a whole lot better This is also true for the detail view we can actually use something for reverse for detail so I'm going to show you how to do it with these your L's on that list for you so let's go ahead and go back into List few here and we'll go ahead and name the your or else first so in your A list up by going to come in here and go Name equals two Restaurants Detail we'll save that So restaurants detail or rather restaurant detail that's the name of the U

RL So we're going to my templates and to my restaurant list In this age ref right here I might be tempted to well let's go ahead and keep that for just a moment but I'll put two of them here And we'll just break come out

I might be tempted to just say Restaurant or restaurant detail as the URL So put these and double quotes you are ill is that

OK so I might be tempted to do that and if I do I refresh and here it says reverse for detail with no arguments not found because one pattern tried it couldn't find anything because we don't have any arguments in here so all I have to do is slug Eagles to Slug we say that a refresh and now I've got those links working so that's a nice and convenient way to use the your L Short pattern specifically inside of a template but this is actually not the best method in fact if you remember back to when we did our create for you Right here we have this success you're L Now if I take away that success U

RL and Go back to create something let's make sure we're logged in And notice it's making sure a log in a ready and I say AB

C Location and makes a candidate save say no you're L To redirect to either provide a URL

or Define a get absolute Your L Method in the model now's a good time to do that so I'm actually going to go ahead and copy that go into my models and underneath STR I'm going to find get absolute Your L

Self and it's going to turn something So this is where I can actually return you know let's say restaurants and then we can use the slug here and use F and Which is to self not slug and I refresh and here he continue and when you know it actually redirects me to where I need to go but the same problem occurs like the your will is hard coded here so if I changed This year L To something different

This is no longer valid So there's a method for views that makes that even better and it's called reverse so from Jane Doe doc or get your L Result Vers import reverse And set of doing this what will do instead is will return reverse and that your own name again which was a restaurant detail Again go back into your Else you are all name restaurant detail And then we add in some keyword arguments keyword ours equals to a dictionary slug and self thought slug keyword arguments are always a dictionary there are keyword arguments the keyword here is the key here is the value secured arguments so we save that and now if I created again I go to create and will say reverse

Whatever working location And make skinhead save reversed Working OK cool so that means back on my restaurant list I don't need any of these I can actually just use something even better that's object to get absolute pure L and That's probably what you'll see most of the time so I can get rid of that other year L That doesn't matter anymore we save it and we go back into a restaurant list and of course everything is still working so that's the reason for those names that's the reason for the short patterns themself and it has to do with the ability for you to change these at any time and not breaking everything so you're not hard coding or repeating yourself over and over again that's the key here all right so if you have any questions on what we did here please let us know before we go away there is one more concept that I will just bring up because we're on this topic of reversing your else and that is how this part works really when you create an app and you want to add to be reusable the app itself would have its own your wells so let's go inside of this app and we'll call it you're also Actually copy the original your L's and I'll paste in here everything that we need some to go ahead and get rid of everything we don't need right so I definitely need your L

Because that's part of it the template log in views I don't need the admin I do not need the restaurant views I do need but I'm doing a relative import I'm also going to get rid of the or comment out the create and list views because we don't need those function based views anymore and I'll get rid of everything else so let's go ahead and get rid of this And I'm to go ahead and get rid of this we don't need the about stuff we're only keeping the Urals that are related specifically to that app OK so now we have those YOUR us if we go back to your old stop by I no longer need to rewrite these but what I want to do is I want to include the Your else and this is This last part including another Euro call basically we're going to do now is just import include here and to use this example here and paste it below but instead a blogger to use restaurants and this is going to include restaurants that you or else these old Urals we can comment out OK but if you want to add one thing in here and that's name space and we're call call it restaurants or the name of the app itself So we're going to say that let's put a comma at the end here let's go back into our year else here since I have that first restaurant here I don't need these other ones well we'll talk about that in just a second but I'm going to go ahead and keep this saved and hit refresh and here notice that this isn't working but if I add another restaurants in here it will give me this reverse call being incorrect so this is actually the UR

L That it is now and why is that that's because you're else starts with the restaurants and that includes these other restaurants that have basically a pin's these other ones right here so what I need to do is get rid of these restaurants right there so I'm actually no longer being redundant and instead of a change these in just a moment but will go ahead and say that and let's actually get rid of this namespace for just a moment we'll come back to why that works and why it will work so we save that we refresh and the restaurant lists will work in just a moment so restaurants here and there we go so our detail view is not working correctly so back into our your L's is just not showing up so now we need to do is actually just creates this namespace and then go back into our your L's and we call it list Creates And detail OK so in models we need to change this to using restaurants colon so this is the name space and then the name itself So the name itself the name space and then everywhere else we did that which was one of them was in the templates now have restaurants colon list we save that refresh and we're getting the restaurant detail not found so we're going to find where that is

By doing a quick search inside of our app Copy that and go back in we do find find we want to do find and replace So just to repeat Fine and files rather So we've got restaurant detail And are going to put it into our current project so let's just go ahead and add this going to add folder

And we'll put it into SRC Here so there's a current projects we're just going to look for that restaurant detail portion and that's in here so it's actually commented out Inside of my you or else I probably don't actually need it anymore so what I want to do then is refresh the server

And it's still giving me that error So let's make sure everything is saved in here As far as everything all of our other stuff So getting restaurant detail is not found That is not a

So let's get rid of this We're going to get rid of all of these because we no longer need them And again let's go into our models make sure that was saved restaurants detail there it is that was the problem OK so this just wasn't saved it wasn't showing up but it wasn't saved OK so now it is working OK so we just cover a lot on these You're else first of all we talked about the names and why those are shortcuts and then this ability to add in another YOUR else file is how this works You are definitely all play around with this as much as possible but basically we want to include other Urals we ignore that dollar sign the dollar sign means it's the end of a string in this case it's not it's going to want to look inside of this Urals file which is in Russia your L's and in here that's where we've got these and of strings things right so that very first one restaurants list that in there and so on that's why you always see those dollar signs at the end of those YOUR else OK so if you any questions let us know otherwise let's keep going

OK So we've learned a lot of the core of Django like we really got a lot of the back end and now you are pretty much ready to be able to make a lot of your own apps right so you can take this concept behind the rational occasion model and the restaurants app and you can actually start to build your own of course is a lot more to Django than what we've done so far but but really you're at a point where you can totally do all the stuff on your own and we're going to go ahead and create a new app so it's sort of review as to what we've already done so far it's going to be a lot faster and different I can explain it nearly as much but it will at least show you how all this stuff is implemented just more robust and just a quicker way to do it and yet we're going to be referencing some of our previous code because it's stuff that is a little bit repetitive you know once you learn how to use general apps critical ones is really not that complicated so the purpose of this service is not to list offer assurance that is not the purpose at all All rather the purpose is to list off Menu items that you like at those restaurants and the way you like them so let's say for instance fish tacos there's a salad there that I really like I like a specific way and it's not on their menu so in a sense I'm making my own menu for fish tacos these are the things I like there and that way I can share that with friends so if somebody ever needed to pick up food for me or just wanted to pick up food for me then I could you should literally be that way because I am moving very very pick him a picky person when it comes to food and if you're anything like me then you understand the value of this if you're not like that then the main thing for you is to really look at this as a learning opportunity now and also understand that even if you're not picky as far as the things that are on the menu item itself you might like to have a list of things that you like at all these different restaurants so that's another thing is like you're probably slightly picky even if you you know don't modify the menu item itself you probably like a handful of things at most places right so that's essentially what this website it's all about is doing that in particular Ok so now that of the way that means that this restaurants like app is is really just there to kind of explain how that all this stuff works it's not really a functional restaurant location app because it's not getting real rush ons like it's not getting the real live stuff I mean it's going based off of user data as in I entered all this data and as we see down here for a stroll down I have places called validating even a user name something that's not actually very good data so to get better data for that we would want to check out our our project called geo locator To This project in particular will show you how to actually do geo location with the P

PI and Google Maps in It's different a little bit more complicated that's why we're not really going into it but it allows you to get the real data from these real places which is something you would probably end of doing on your actual service OK so with out of the way let's go ahead and get this other portion started to activate our virtual environment and I'll just do Python manage up the why start app and call this menus now of course it's menus because these are the menu items that I'm going to be having on my particular page like my individual profile if you will OK So inside the models we're going to do a couple things here I want to import the user model so from Jane go dot com import settings and I'll do class and I'll just call this menu item or rather instead of a menu item I'll just call that item I don't need it to be anything else we also don't want we also sometimes might want to check to make sure that it's not already a python classics such as prent or class like I'm not going to class class right so that doesn't make any sense and packed I'd bet Django or Python would raise some errors if you do that so we'll call item models that model and I'll say user equals two models thought foreign key Settings dot off user model

OK so every item that is created has to have a user that makes sense for it the next thing is every item probably has to have a restaurant too so models that foreign key will now we had associate the restaurant to this place Restaurant to this particular item so to do that association much like we did with user we just are going to go to from restaurants top models import restaurant Location and we're going to put that in as our foreign key so now all of these things are associated much like what you did I mean I want to challenge you To go ahead and try this out all of this instance model set stuff and all those reverse relationship things we did a little while ago I want you to after we get this in there I want to actually go ahead and try that out so here I'm just going to give it a name so that this is actual late related to the data so this is the real item stuff this is just associations Now these are fairly optional I mean you don't have to actually have this like if you were making this website for yourself you don't have users you probably wouldn't necessarily have a user model or if you want to put the restaurant name just on the item itself you totally could do that but again we're doing it in a way that's just a little bit more robust for our system here so models of char field max length he goes to one twenty course we have to said that that's a requirement of that field and then we're going to do contents or what's actually in the item itself so this is going to be something that we're going to use a text field for so we can have a bunch of different things in here and also say help text equals two separates by comma that is going to make a lot more sense

Once we actually do and we see what those contents are so separate Each item by content come up or separate each ingredient or one Ingredient might make most more sense but we'll leave it as item OK so we get a name for the item the contents that are in it and we can also say the exact same thing but excludes And I'm going to give this an optional value so I'll just say Blank equals the true and no because the true because the contents that could be how I wanted this is pretty much how a you may want it OK so then we'll say whether or not this is a public item as in I want to share this with every

In the world Right will just say public equals two models field default equals to true OK And then we also will just say image will leave I was going to say image your L Like a UR

L To an image but why should I leave that out for now because adding images is just a whole nother level of things that we're just not going to cover this time OK so we've critter model I do want to actually put in timestamp as well as updated in there as well And I'm actually going to define for them we'll do class Medha and we'll do ordering equaling too and we do reverse updated and reverse time stamp so this gives us a default ordering and in this case it's going to say what's updated most recently will be this way if I want to updated the like the oldest one to be this way so just add that minus flips it up and down so it flips the query set so when you do Item objects all this will give the most recent item updated first rights of the most recently updated item first whereas if I did it this will give us the most recently I did it updated item last reverse order and that's true for timestamp too it's just the general idea that OK So we've got our models and our model and here all the fields that we're going to associate at this time so that's pretty much it for the model item itself let's go ahead and just add it into our settings module into install that's And we're going to just call it menus we save that and we go ahead and run Python mention P Y make migrations and Python it up he why migrates And the reason I didn't do make migrations right after saving many years of course I did save this is because we had to add it to install apps before we could do that OK So that's it for actually creating the model now let's go ahead and add this model to our admin you know do from models import and this call item and then just to admin that site register Item

OK So let's go back into the admin take a look at here in a whole menu items click at item here we go we've got a user here we've got restaurants these are all of the restaurants coming through and we've got name and contents separate each item by comma OK So the big reason that we did that is because when we actually want to get contents I'll make a method in here called self I'm just to return a list here and this list is going to be based off of self dot contents that's split by comma and we will also just put just the comma there no space after so this is going to split up our contents that are in there to have it as a list and I'll do that same thing for get excludes OK So those are methods that we absolutely are the should be excludes not contents those are a couple methods that we actually want to have so that women bring to the south the contents of any given item will show up OK So in the next one we're going to actually or in the next portion we're going to actually make this into different views to handle all of those things especially now that it's in the end that if you have any questions let us know otherwise let's keep going So now it's time to do all of our views Go ahead and go interviews dot pious side of men useful do from DOT model From the models important

And then also from Jane go dive used a generic import we're going to do the list to view the detail view that creates the view and the update view OK so we're going to then create each one of these so do item list view and takes in list view queries set equals to item Well actually we would want to do a different query set than the standard one will do define get query sets and it's going to take and self and will take will return self or rather will return objects that filter and this is going to be a user I'm just trying to score or actually let's do user equals two self-taught request user So this would be if the user is authenticated of course and we'll have to worry about that a second but if we go ahead and copy this a couple times And we'll do that HERE HERE and HERE OK so we've got our detail view first

And then our create view And then finally our update view OK so our list and detail views are sort of straightforward on how it is that they're going to work but our create view and update view require a form so let's go ahead and create that form a new form stop by and just to from jingo import forms and then from the models import item in the new class item form forms to model form and we do class Metta model hoops colon model equals two item fields equals two And we said contents we probably want to have the restaurant

Contents excludes And finally public Think that's it let's go ahead and look at our model contents excludes publico we definitely want to have the name of the item to OK And we're going to go ahead now that we've got this form is good and important in here so from the form's import item form OK So create View Form class equals two item form And update view is the same Don't think we looked at a view yet

Pretty sure we didn't actually that's OK We will look at it and it's not really that complicated but it is something that's pretty cool so now we've got these four views now we need to wrap in our Your else so I'm to go ahead and create a you're all stop high file inside of my menus app on me use my restaurants have your L's as a reference oldish combi that entire thing go in here and paste it in obviously I'm not using the restaurant you or else anymore instead I'm you using item list view item detail view item update View and then item create the view with these in alphabetical order a little bit better OK That would go Crazy you is going to be on the Create view again list view will be the list view and detail view will be that restaurant detail view and in this case all bring it back to being primary key and using just digits

You could make it a slug you could do all this like fields Duff in this case I'm not going to do that because more likely my list few of all my items will be just the initial thing right off the bat so that's what we're really worrying about or focusing on here so now that I've created these ULS I'm to go into my main project folder here and I'm just going to go ahead and copy this restaurants here else and just come in here and say items as and menu items so menus your L's name space being menus That Rio and now it's going to take a look You go to items we hit Enter template does not exist and that should make sense so let's go ahead and make that template we're going to make a folder inside the menus will say a new folder called templates inside the templates are going to make another new folder called menus as in the name of the app and then a few new files and here the first one is going to be item list So go ahead and say this is item List dot H

TML and I'm to go ahead and just come in to my restaurant list and copy that paste this over and will say Menu items

As the titles that go for object an object list all this stuff still works with the exception of this get absolute URL Method soldiers leave it in as a hash tag and will say object name that's going to be the name of the object of course and then inside here I'm just going to go ahead and also put in the actual contents of the either so we'll just say content contents And I'll make another an ordered list class here

And then we'll do another for loop inside of this and will say for item in object I get contents And four And then the list item Item OK and let's copy that same thing for excludes And basically what I'll say here is I'm going to have to change these to be it's get contents or it's contents at all for both of these so Let's go ahead and put an IF statement in here and say if object of contents

And if And again the same if statement for excludes Now the end if we say that refreshing here and we get we have nothing called I don't list showing up so it's restart the server real quick And there we go we have a spelling error syntax error in four And four all right refresh no menu items because we haven't created any souls go to make one and I'll just use it on the current logged in user restaurant as a matter and I'll say on rafts or radio

And again using the separate by contents mixed greens Of Ocado or actually I think it's Kwok Guacamole Carne a Let it slide this is already there onion The

Cheese OK I'm sure we got guacamole there right saving continue OK go back refresh and their guilt so we've got our actual item contents and we'll just say no corn Onions Yes you know any other sort of ingredients that you don't want on there and we go ahead and say that refresh There we go So it's actually showing these menu items based off of what I have in the database and it's fairly straightforward So as far as the unique individual view again we'll do object id because we said the idea itself or rather we shouldn't actually do that like we've already talked about instead of going to models and copy this get absolute you're all method and bring that in here and it's no longer restaurants detail instead it's menus detail and it's P

K Equals two self-taught PK We also need to import that reversed call some to go and do that import reverse never go Back into our list we'll do object type get absolute Your L

Say that refreshing here and click on that absolutely or L It's not actually taking me anywhere that's probably has to do with how the ULS are set up so let's go ahead and look at them in euros that pie we haven't created for you here so that's correct let's go ahead and look at our views I don't create feel that should be right too And then I detail view again I did detail you that's correct too So let's go back into our Your else here List View create view

So it seems as if the order of the Urals has always mattered actually but it seems that for some reason we need to actually put the list of you at the very end to make sure these are hits or checked first as far as regular expressions are concerned and I'll go ahead and refresh in here on item one I did this does not exist that's what we wanted and of course this is also true with the restaurants here else too This may have changed with with the newer versions of Django on how these patterns are actually hitting if you actually know the answer to it please let us know but now our detail views are coming back OK so going back to our menus Now if I click on any given individual item I can go ahead in creative detail here i need of still make that view of course and then also my Create view Here it is right there so we also still need to make that one as well so it's going to make the template for the detail view By the detail I'm actually just going to go ahead and copy the item list one and instead of it for object list

We don't need that anymore And I'll just have this back or cut that portion out And instead of OBJ

We'll just call it objects so I'll go ahead and cut this out Paste that there and get rid of the link we no longer need it so object everywhere OBJ Was we are just going to change to objects

We save that refresh There we go we've got our own refereed Oh here I'm also going to put H Two and do restaurant name refresh in there and call it location There we go maybe this should be like just a paragraph tag here Contents never go OK cool next thing of course was creates and this is an actual form so let's go back and look at our create view inside here and we've got is this restaurant create view with a template name itself on So Russians up for let's go ahead and look at that this is ad restaurant it's really not that complicated as far as how all of this is going so what I want to do is stead of being called at a restaurant I'll call it title or I'll just use the context item of the title and we will go back into our view here first off of the restaurant one will define this as get context data self args keyword arts

Context equals to the Super call of the creative you self and get context data Of ours and keyword our Context title being to add restaurant Return context they were built and now I'm actually going to take that form and move it into my main templates and we'll call former the stimulus stead when you form an H

TML Copy of this form of HT

ML And in there there we go Not a huge deal here but it's basically allowing us to reuse that then and now back into our view we can just say templates

Name equals to form dot HTML For both update view and create view

And then for context we can grab that get context data From our restaurant so we'll just go ahead and copy this bring it in here And we'll do get context data and just add it in to this And that's going to be updates By them

And this will be created I'm OK and creative We already have a get absolute Your L Method in here so we don't have to add a success you were ill at all so let's go back in here refresh and here it is sweet got to restaurants name and all that so looking roughly like we wanted to I'm so I can actually add in items here again and I'll say talk or to tacos and well it will do to carnate Well it's actually just called Karni Assad tacos flour tortillas Rice black beans

And we'll just say side Rice use those commas side of lucky it's OK we could save This gives me that not no constraint found as was expected maybe not by you but hopefully he'll get it eventually so valid self and form object he goes to form got save commit equals to false object to use or equal to self that request that user and we're turn super Of item create view self and form valid of the form

OK And we've already done this we did it right here oops I think I might have made a little mistake no it's all right OK So save that refresh and here it continue false is not to find tubes that should be capitalized and we'll try that one more time there we go so now we got a hernia so tacos also at that same location so You can now create a bunch of things for all of these different rush ons very simple and straightforward which this should create if you should say create item OK so now what we need to do is actually have it where I can update these items which is something we'll talk about in the next one let us know if you any questions here otherwise this could go on OK So one of the problems that we left was when we click on restaurants we get literally every restaurant that exists And that is true regardless of who the owner is so if we go to a restaurant locations reverse working is J Mitchell three which is my current logged in user

And if we go to actual Baja fish tacos that's a different user so the items that I created here are incorrect right so this one says Ball have starters but it shouldn't be able to be associated to that one because they're not the same owner so there's a few different things that we can go about doing here and the first thing is going into our views into this create new we can actually add in some form keyword arguments so we actually add in some stuff to the keyword arguments for our given form we'll see that means here and second so if we do get form keyword ours we can say self and we do keyword are equals to the superclass So the default item of this in this case it's create view self and then it's dot cell type get keyword ours get ers who can get form keyword ours And then we can add into these keyword arguments Something we'd like in this case I want to add in my user so do self-taught request user and then return keyword arcs so these are the arguments that we're going to pass into our Form class right so we have a form class here we're passing some argument It's to it so that means instead of forms of pile we actually have to handle those arguments and we do that in our in that class so this is a default class never to be using them we do self the arse ourself args and keyword are and I'll just go ahead and print out key word arse dot pop user now I can also say use or equal to none here so either one of these would work not both of them but either one so I'll actually leave it as that I'll just go ahead and print out user and then we're just going to do the superclass item form self in it And arcs

Q or Darks OK So the key thing here is all I'm doing is passing in the user now to my item form and of course if I look back at my console and we go to the Create view I can see that I'm actually getting my user and if I did an in cognito window I'm not going to see a user so that means I'll have to actually add in the mix in the correct mix and to require off an occasion which we've already done and that's that long and require mixin that we did on the other views so I def we want to make sure that's done otherwise the next portion is going to run some errors so in the Create view we want the logon require make sense of day view as well so that way when we go to this URL It makes you log in and the no come back OK OK So now that we've got this you user

Right as we've seen here there's the user in our form we can actually update the query said it's coming through to the foreign key so that is who this right here OK so going back in To our form inside here we can really just to say self-taught fields and then the actual foreign key they were using and the query said that we want to change so that's restaurants Dot query set equals two and now now that we have the restaurant crew set let's just put it it will to empty I'm going to hit refresh that and list item I can actually do an empty query set soldier say I have to actually add in a real live crew settles go ahead and import ones will do from restaurants top models import shot Location because that is the model that we're using again in the model field there it is so that's the model we're using so that means that the queries that we need use so you can do objects none and that's an empty query sent we refreshing here and now we've got Rush items we don't have any or we can do a filter so we can do filter owner equals to user or that past user so we save them and now for refreshing here I've got my menu items going right there so that is the actual query set that I would be working off of with this given form so that allows us to just really narrow this stuff down quite a bit which is really important because then it allows a more robust way of working with this entire system in fact my restaurant list probably shouldn't even have any other items either unless one of those to be public as well so going back in two items

Now that we have the ability to Mina fi the restaurant locations another thing you absolutely could do is exclude if locations actually existed so let's go ahead and say excludes And this is going to be Item is knoll equally to false so item being the model item so if you had a different model here you could do that so I'm going to say this will go back in to create A crew set exclude but exclude that excludes no S

They're OK so now I have only a certain set I'm going to go ahead and use new as one of them and I'll say item three year whatever contents ABC One two three another hit save OK there's item three let's go ahead and create something new I come in here notice new is no longer there so that's how you can limit out what you want to show up here but of course I don't actually want that exclude that's just a nice little bonus that you may want to use sometimes and when he says is no all that's really just saying if this relationship is no old then then we can work with that if it's not an old then we can say true so unused items if I just want to show on used items will say item underscore underscore is nil equals to true so that's just the opposite so this is saying used you know the true bullion stuff you can do it either way OK So all of us play around with that more as you'd like but the next part of this is actually updating any given item so that's where these Q Are arguments actually allowed us to do some updates to it right so if I wanted to I can actually put in an instance here if I had one right so let's do keyword ours instance equals two and we're just going to put any random object here so the objects the item the objects dot

Filter self that requests that user and we'll just do the first one there OK now again with a keyword arguments we go back to this form and I can print out that instance so keyword arguments stop pop and we'll call it instance we save that refreshing here User object is not Iterable Let's go back in here that's why so we say the user equal so that And we now look at a terminal we see that we have this instance object and then we could fill in all those fields with that object or instead we would use the updated view which is this right here

So going back into our model hoops we no longer need that instance coming through And then that means back into our form we also don't need this instance So instead I want the prefilled the data based off of a URL

So we come into your L's and we had that item of Dave you want to go ahead and copy the detail view paste below it and call it up Dave you are actually we want to above it so say a date And we'll just say update OK So let's go back into our items Click on any given item and then say update And now it actually has that particular item notice the restaurant list is empty so let's see why we go back into our forms Oh I know exactly why and the reason the restaurant list is empty is because we didn't pass in these keyword arguments here so go ahead and save those just like that item of gave you

Refresh and now it's giving us the appropriate items themselves OK So that is some a little bit more complicated data and how this all works but going back into the form Let's go and look for the instance again and will refresh Rather let's look for the keyword arguments in here that are being passed We refresh and it's going to be Course we have to be on the right

Page and here are the keyword arguments Notice it says instance on the update view whereas on the Create view it doesn't have it right so that is if I want to actually make those changes so that means that this instance is in my form's keyword arguments so that means that I can use it as well so if I go back into form about HTML Let's just show some stuff here will say if formed instance

And if then we'll just say H one Form instance that name Close off H one we save that in the Create view it's not there In the update view it will be there

Form instance the name And look back on wrong form relation will actually let's go ahead and delete that one because we no longer need it it's we have it in our base formulation Well there we go IT FOR ABOUT instance we refresh and there it is so it's actually the object instance itself that's coming through here and again going back to the Create view it's not going to show up that is if you for some reason needed to show that instance I don't need to soma leave it off but it's just some other additional things with forms that are fairly useful for actually doing things and I'm a change might add it to or update to it so let's go back into your your L's change update to edit will save that and they're built OK so if you have any questions on what we did here let us know though as those Keep go So we're getting a lot closer but we still need to do is change how restaurants and also how many items work in general so restaurants and menu items both of those things should be only for you as a user so once you log And those are the things that you see you see your own stuff on home maybe profiles and stuff we haven't built yet we might want to see other users things so I can click on a user and then see their Rashawn list with their items on it right or just their I them either one OK so to do this to work towards this goal we need to update how our views come through on the restaurants in and of course we have some of these old function based views which I'm now going to go ahead and get rid of I do not need those anymore and I will make sure that I'm not importing then anywhere and I believe that I might have some of those imports on this and I do have some of those rational view imports no longer need them and I can get rid of some of the comments as well OK so going back into the restaurant views the get query set or the restaurant list for you we still have this slug item and here we have a lot of stuff in here that just doesn't fully make sense for this list view anymore made a lot more sense for when we were actually doing some searches so what I mean do here instead is on a return restaurant location but all and owner equals two self-taught request user and again that list you what we're going to do log in required mix in

Detail same thing and also same thing owner being the logged in user they create for you of course is how it is I can also make an update view which is really not that difficult I don't have to change the form valid method on an update view because the user when they create it is already valid right so I don't need to reset the user is essential I'm saying here so I'm to go ahead and bring in that updated view And change the last one to restaurant You OK so we're going to bring this in into our ULS for the restaurant restaurant update view and again just like the last one we had it off of the detail so rush on up Dave you Should be here I'm actually in a change to be calling at it even though it's an update if you will say Ed it because that's pretty much uniform much like we did here and also call that as well OK so now that we've got this I can Refreshed use We've got a little bit of an error here it's a look back at our views

And our crew said method this should be get query set down here OK Save that refresh and now for going to restaurants all got zero and should not be all but filter Good OK Good to see that that doesn't work OK So when we filter that this should be in views let's make sure that they're all just like that they are refresh and now I only see my restaurants so rationalised now should have a different name rather than rational list it should be just My restaurants refreshen here and there we go and what I will say is inside of this I'm a say empty so that object list is empty will say add

One Or add a restaurant whatever you want to call it and we'll do URL

And it's going to be a I think it's restaurants creates it might be restaurants but it will come back well and this should be in here if he calls to that And let's close off the a tag There we go we save that And they also say appear Put this into a small tag Close off the small to add new

Or maybe just add we don't need add new we can just call it add Refresh that would go my restaurants add a click on that that should take me to my Create view and it does A very good in my menu items four of my navigation I want to add the new item here so nav bar this one I want to do if a request to Request the user is authenticated Then we will show restaurants so if they're not then we won't and copy that and put

Items I think we call that menus though menu items So we can cook a menu items restaurants all that stuff OK I don't mean contacting about really but I'll just leave it on there OK So now that I've got this stuff it's only to show up if I'm logged in and of course if like if I go to a Incognito window notice the navigation bar has changed a little bit so now these restaurants are mine right and I want to edit or change the reference that I have so come in here edit them no so I have this added view causing a little bit of an issue so we're going back into our views here we do need that good Chris said method on our edit view as well or the update view mainly because it's still getting the instance and still getting the detail item in there much or if I mentioned that before but it does it actually gets the detail item from it and this is a little problem with the Area here so update restaurant we refresh in here and if you want to actually have The title here I believe we can use

String substitution we'll just say self gets object and then dot name Loops that wouldn't work in their will save name Equals to self get object of name Save that refresh and it's update new so update restaurant New

It Up OK and we'll call new I'm going to change just to actually fish tacos because the Mason We had save that would go menu items I got a few different items here item three it's a nit Baja fish taco again of course I want it it's An Item three is in here some night instead of that I'll just say chicken tacos and the contents chicken flour Tortillas

Salsa she is OK Save that go Good so now I've got some more realistic items here so restaurants there's a several restaurants in here that aren't real at all some are just going to go ahead and get rid of them rush on location them to go ahead and copy all these and just get rid of the ones that aren't real restaurants both users who don't need those any longer I just delete all those other restaurants that go so refreshing here and all I have is Baja fish tacos so I go ahead and go to menu items and there I now have my own personal use or menu items as well as my own personal restaurants now when I click on this since I'm logged in I could create You had to be a little bit different and that is when I go to a detail view I could in theory be able to edit it and view it at the same time so I can see the instance and I can also edit it if I want to and that's what I'll actually do is I'll change my detail view for both restaurants as well as my other part so it's going to users and want to change is also is also restaurants going to bring this back to being called restaurants detail updates Restaurants detail update that H

TML OK And then in the Urals and set of having a full service a preview for this I'm just going to bring it down to a detailed view come at this one now And go into restaurants go to detail of course it's going to be a template does not exist so let's go ahead and make one am called we call the detail update H

TML Notice I don't actually have to really change the HT

ML To fit what they've done and said it like this so good in copy this will say this of course is not object anymore its form got instance And we just change all of those form got instance and then we'll just also put former instance the title here And we know still working it's working fine except I don't actually see a form at all so down in my normal snippets I'm just come in here and just say form snippet studies to mail

Hoops not the folder rather we want a new file You file form sniff it cut HTM

L And inside a former HTML

I'm quite literally just going to be taking the form stuff and bring that into a form snippet so instead of form ready to say it includes Snippets form snippet that HTM

L And so my form always looks the same and back in my detail I will add a chart type here and will say H three make changes And we'll include that snippet and then we'll just say with form equaling to form so form of course is a context as coming through we refresh in here

Oh sorry to include but in call includes But include That's going to be on both of them Refresh and now we can make some changes to this particular item and I'll say Baja fish tacos and coastal Mexican food or whatever changes you wanted of course that's not valid category so save this makes going there go so that's how I could have a detail view as well as how my Urals may work now in this form something I don't like as I can't actually change the slug so I'm going to change it I'm going to give the ability to change it here and I'm also going to make sure that it's unique for this user it's a little bit extra stuff here but I'll go ahead and add it in your second Maha fish tacos I hit save what you know fish tacos cool so now if I get a rush on items I can click on them and I can see all those things I'm going to the exact same thing with the Detail View for my individual items because again I'm logged in so I should be able to change them at any time and that's really what it's all about here so back into our views we're going to be changing this From that to being I mean use Detail update

Dudie shemale And let's go ahead and create that instead of menus detail Update dot HTML

Again I don't detail coming through here object is no longer that it's just a form instance form that instance that name OK Back into or your L's switch those around here a little bit comment out that one bring in the Detail View Refresh and here are exactly the same because I made those detail updates the last thing I just need to do is say age three Make changes And will do include

And that's snippets form snippets a smell with form equal in a form Don't worry I will explain those in a second to make changes that would go a char Perfect so I actually see the detail and then I have the ability to make these changes if I so want so I don't actually have to go to another you rel to make those changes so chicken flour tortillas salsa maybe I want to add in walk Welcome Ole I hit save and other there I want to make sure I put a comma there had saved and now it's there so now its actual different items as a list so it's happening here is I can actually pass in context into any sort of include snippet nets how that's actually loading right so it's treating this as form so if my contacts variable was another if our contacts variable was another form of course I'd have to change all instances of form everywhere but that's how I would go about doing it I'll let you practice that or try that on your own but the main thing here being that we can actually update and date in the sort of thing now we could go one step further and actually have the ability to change the restaurant as well on this one single page but that's just adding a level of complexity that I just Don't particularly want to get into it this time but I would challenge you to try it out on your own it's going to be doing stuff with this include and then the only the hugest the biggest difference here is where your actions going to be you want to go to the Ural as to where you're changing that item so the U

RL We're changing this item or the chicken tacos this is the URL

For it so we would add the action being to that your L Action being two items three so keep that in mind if you are going to try to have two different forms on one page and again the huge advantage here is the ability to for me to go through search different things and just make some changes if I need to like this one I want to add in a restaurant notice it did have a restaurant before but it was the incorrect one so back into The tacos again Baja fish tacos now I'm actually really close to having the items that I want but the thing here is chicken tacos and Carney subatomic O's are very similar so let's just add in here and I'll just say Carney And I just add it in all of the other things that I may have wanted I had saved and now we have the science and the contents are the things that I actually want with it so that's for one particular item of course I need it and one more thing into my menu items here

And that's the menu items list I want to be able to create stuff so the small And I'll just say A equals two menu or you're ill men use creates And New Or rather just and we just need to add an item there save it refresh every go let's make sure we close off that small tag

And then also we want to put in that empty tag like we did before in T Copy the portion up here Empties in the wrong spot here Put its own Tegan there had menu item

Save refresh in there OK cool so now we've got a restaurant's menu items and so on And now what we need to do is actually just have the ability to show this all on one single user's profile so all of my restaurants and then all of the menu items associated if you any questions or some of those things could go So what I did was add a bunch of menu items and some restaurants to my account for it so if I walked down logged in as a different user I wouldn't see these things at this point so what I can do though now is build something that anyone can see that is I want to build a public profile of these menu items so it's just out there that anyone would be able to see and I can share with friends or family or whoever if I were wanting somebody to maybe pick me up something to eat or if they wanted to surprise me or something like that or just in general just knowing hey these are the things that I like so then even those restaurants can start to tailor how they actually design their menu if that information was valuable anyway so let's go ahead and make that public profile so to do it what I'm going to do here is a minute go ahead and make a new app Python managed IPO

I start app and I call it profiles and we're really just really working on a Detail you first so inside of here I'll go into from jingo dot views dot generic import detail view and we'll say class and we'll call it profile detail view takes in our detail view and we want to actually get a few things here the first thing is I want to make sure I get my user model so from Jane go to Contrave dot off we're going to import get user model and with that user He goes to get user model and we'll say the crew said it is equal to And that's user objects that all Actually let's go ahead and say filter active it was true or it might even be is activity goes a true

And then we are going to go ahead and bring this in to where you were else so it's either you or els we're actually just going to put it in here so all go ahead rather let's scratch that let's stick with what we've been doing and let's go ahead and create our own you're all stop by and we'll do from dot Views Import the detail view And we'll go ahead and copy are your L's again just kind of doing a lot of the things that we've already done You know have only one your pattern in here and that is going to be the detail view

All the other ones I can get rid of OK And all I really need is this your real import so your L Import and the profile details you OK So we need to have a actual look up here and I'm going to do it based off of the user name so the field that we want to use is username and It could be the same as a slug Depending on how you actually design your user names themselves but in this case I'm going to leave it just like this

So let's go ahead and grab this URL and Bring it or bring it over here and now when we do is grab menus And right above that we will say profile or rather let's just call it you as an users and we'll do profiles that you tell your L

and Profile Profiles for a namespace hit so try this out I'm going to profile slash G Mitchell three that's the name of my user Profile what you slosh and we got generic profile detail view saying it must be called with either a PK

or A slug hopefully remember how to solve this what we're going to do is we're saying Get object or fall for So now we'll see you say Define get object Takes itself and it's going to return get object or for a for as could be the user model we're going to get the user name equals two well I sacked equals to self Let's put it here and do self keyword arse Doc get user name I exactly go see user name and is active he goes to true we save that refresh and we've got a template the does not exist of course so we come in here and say template name equals two

and again I'll do profiles user to the HTML I don't need the crew sending more because of this get object call again that user profiles is not actually defined yet so we will define that second but basically all we're getting here is the user object that's it so I want to read

Turn back the user object to this any given profile now if I do not have a user name so if user name is none Then I want to raise a cheapie for all four Make sure it's just like that when you go an import it from the cheapie import ATP For a four because of the chance that there might be none so we do want to raise this being none this probably should also give us an H

TTP Four or four but whenever I do something like this where it's like a it's a dictionary value and I do it gets there's a chance or by default the default option is none so I'm just like having those safeguards OK Again we need to create a template so although a new following here will say templates first new folder and then another new folder called Profiles So again we're naming it all for that and then finally users out HT

ML and With this some to go ahead and grab one of my other one so I have details to go ahead and copy that and this time it's going to say user Username And

H one user to user name and then we'll worry about the actual contact context of that user in just a second so refreshing here and what the heck it's not showing up I just created the template it saved everything's correct it's all spelled correctly I look my view world's going on well the simple solution is because we didn't actually add this profiles app into our install that's now you don't actually need to add it in there for the view but you need to add it in there if you want to look for those templates inside of this app because we can criminal we're not going criminal we don't actually need to but I do need if I want to look for those templates I do need to add it in so that's what I'll do Anderson profiles here say that refreshed And now it's giving me this profile I can also get my other user see if he can see those profiles cool so now what what is the data that we want well I want to give my item data us all the items that I have and I also want to get my restaurant data so it's going to be dependent on what it is that you're wanting to display So let's go ahead and take a look inside here and I'll just say a user restaurant location set that all will say that and refresh there is our restaurant location set we can do that same idea with our items User items item set it all and we get all of our items so two different sort of things here right now let's go ahead and iterate through four Rests in this item right here So these are going to be my restaurants in four

So L-I Rest the title or restaurant game refresh there is my items there those my restaurants And well can we actually get the reverse of the restaurant can we get the items in there so let's go ahead and try that so inside of this L-I I'll put another or I'll put a U

FO In here Ted That in and all all do first is also a rest set but all say that refresh and what do you know I can actually get the items for this user related to the individual restaurants this would become more apparent if I had items actually named appropriately so go into my models real quick into this STR

Function in here call itself am going to return a cell phone Name we change the model so let's do what we normally do and that is Python managed of the why make migrations and Python it up here why migrate there are no changes but just want to be in the habit of doing that refreshing here and now I'll see the actual crew set so I got Carney some tacos and reparative chicken tacos And that's Baja fish tacos on Rep Rito chicken tacos kind of subtitles but so that's actually giving each individual item it's working as we may expect so back back to my user and get rid of this and now I can actually iterate through these in a safe or item in rest of them but all And will put an L I hear

An item name Close that off Close off the for a loop here save it refresh in here and there we go home now we have our items So we could go even one step further and make another ul Or just put a break here and have the actual content of the items in there still safe for

Ingredients or by NGOs use I N G for the ingredient In item get contents

We use Comma In for I shall put these in a span And others say style

Margarine writes For pics background color To Graeme refreshing here and there we go so we now have our restaurants the item itself and what's in it and that's I now have a very very easy way to see all of the things that I like and the locations the restaurants I mean this stuff obviously I can add a little bit to it so the restaurant And then the location and I'll say Rest location

And rest category Right so you can obviously do those things I'll just go ahead and bold this one Get rid of that line they're going to so it's now showing us even more and there's definitely a lot more styling sort of things that we can do but this is now a very easy way for me to be like hey go check this out this is what I want and that's those are my items now we don't have individual links to these items at this point but it is at least showing me the actual items themselves in there in a way that just kind of makes sense I'll go ahead and add a padding top bottom I'll say two pixels and then four pixels so this will just give me a little bit of padding there as we see here and then this one will add a style margin Of let's say five or let's to fifteen pixels refresh and that gives me a little bit more spacing between my

I also might just want to say B items We want to break here Or rather Maybe a pizza

Or go And we could style this better and we actually will but for now that is showing me hey here's my restaurants here the items i like they're cool and of course I want to see if he same It's doing the same sort of idea of course this user doesn't have the item so it's not necessarily going to show us something so maybe what would want to do is say if user item set Exists Then we actually can iterate through otherwise we'll just say P class equals to lead no items found close that off and if Save it refresh no items found for the user

The user is not found all these other users aren't found very good And now I can share this with anyone at this time I mean it's now pretty functional for myself so if I actually look at this in an incognito window I can see with this particular user likes And again we can solve this better and we will so stay with us but as you see this is fairly straightforward but still very effective it's showing exactly what I like in certain places because again I am male we've picky face wash will see you in just a moment So what I want to do is change my template to be styled in bootstrap it's fairly straightforward as to what we're going to do and actually it's going to be ready to switch to bootstrap version for whenever that comes out to it's fairly straightforward so we're going to do here is just change this portion into something that style bootstrap and to do that we're just going to change the L-I tag in to do of tag

And we're going to add it as a class of being called small twelve I mean it's going to go all the way across and then I'm also going to give it a day of class of row right above it Close that off refresh and that's actually now solve for CSS For

Bootstrap so I will make an age or line on the bottom of it so you can see that there is a line there Or on my row it's or a Sunni on the Coleman self I might add a style border one pixel solid and use grey With a nice little border radius Of five pixels just to add nice little rounding this to it as you see here and then we'll also add a margin bottom back to being fifteen pixels we see that and it gives me a little bit better of a look for the stuff not the best so I'm going to get rid of those styles and instead of leaving a light that will add another class in here And we'll just call it thumb nail this is coming from bootstrap version three if you were going to be using a good strap version for you do card and then card default but we're using

Shot three so just tuning thumbnail actually adds that nice of a border around as well as the spacing just something that's kind of nice OK And then I will just put an H one tag in here for the title And then below that will put the location in as a paragraph tag we save that H one is probably a little little too large for this let's put it in as H four and then we've got our idea so these are the menu items that we actually may want you can you can organize these however you'd like but as far as the span style and let's just add I'm going to go ahead and cut this out for rather copy and paste below it and I'll do a span class instead of a span style so do class equals to badge and I'll just do badge default Save that that badge looks a little bit better Might be a little too small of a badge but or an item itself like that text is actually kind of small so you may or may not want to use a badge it's really up to you or realistically you could probably just you

Item dot contents Or I think it might be just content the contents and that's it like we don't actually have to separate them out that's just something you could do as an option so we say that and there we go so we've got item content and we would just say Contains Now we don't even need that was Leave it is in is that item name itself that is something I'll probably want to bold soldier but that is a B

And I also don't need this items portion that doesn't really necessarily make sense anymore so now here there are my talk O's all the things that I want to see as far as how this is concerned now it's given me the name a location what kind of food it is and all that so the next part would be searching this and that's something we'll do in just a moment Of time that a little bit of a search inside here so the search being that I want the ability to make sure that hey these are items that I want right so like if I search for a hot fish tacos you know show up with a search for Carney a sudden that will show up so what I'm going to do here then is go back into my views and I'm going to add a new item hearing called Get context data we've seen this a lot but I want to update it a little bit not say context equals two super profile data detail view self get context data We actually want to add in args and keyword Arias as a safe Card for our contacts data it's probably not completely necessary but with context I like to make sure that I'm getting everything in there without running the errors so we get some context here and now what I want to do is actually say locations or restaurant locations or just restaurants Actually let's just leave it in those locations this is going to be equal to those restaurant locations so let's go ahead and import it and we do from restaurants top models import restaurant

Location And we do Rochelle location objects that filter owner equals to self Well it's actually in the self like it objects so it's going to be related this is the actual user model itself right and I'm going to cut this out for a second and say QS SIEGEL So that it was just you if Q

S That exists then restaurant locations ECOSOC us And that's pretty much it right so it's it exists than those of the locations so let's go ahead and look at that template now instead of doing that I could still leave that but I would I want to say if the locations exist then we'll just iterate through the locations So add

The other end if after OK and it's from the server again OK we've got a little syntax error on our view this needs a colon so we say that refresh still working OK so I also might want to have or to ensure that a restaurant items actually exist but I'm not really too concerned about that like so in our user we had this user items all exists so basically this is just checking to make sure that there actually are items I could use and change that logic in here so was it go ahead and do it For me used on models imports And Items exists Items exist equals two items that objects

To the filter use are equal so self I get object knows that I'm calling it object twice I actually might have that object inside of my context but if I don't I'll just go ahead and set it as a variable it probably is and it probably should be already in my context as an instance but will see then a second so I'll just put it exists so if I don't exist and my restaurant locations exist which they will if those are together I get this already exist my rational locations definitely exist but this is where my searches are actually going to go off of the restaurant location instead of just the items exists which will make sense in just a moment so since I've got this let's go ahead and save it Look back into our view and if I print out the context notice I have context objects I've user and view so user is Rick related to the actual user object itself it's not the requested user so let's go ahead and take a look if I had see if he notices there's the same user twice so all that means is I don't have to run this I can just say context user And now give me the same thing so that I can hear it still working locations there and then back into our user will just say if locations I can get rid of this other one So save that refresh there and now those two are working OK so now what I want to do is actually be able to search these locations for For this particular user

So I make a search button or search form and they're going to change how this query set is coming through based off of that search form so let's go ahead and jump in to user doesn't she well again and we're going to make a form here sold to form classical to form method it equals to get an action is equal to just a dot And we're going to put an input here so inputs type equals to text value Eco's or sorry not valuable placeholder equals to search Name equals to Q and Then Button class Eagles to be T N P D and default And value or

Type equals to submit And we'll just say search We say that refresh in here and notice that now I have this search button here so I'm to go ahead and search Baja fish tacos hit Enter notice that there's this query appear that showing me what I just searched so I can also set this value equaling to request to get to Q All explain that part in just a moment but now it's showing me what the GET parameter is so A

BC Searches that there too so the good portion docu in particular has to do with the name of this so if I call this query this would have to be query and when we search in here for fish tacos The URL

Has that query in there that's all that's really just did some a leave us Q Because it's pretty much what it's always going to be and now that we've got that down here I can say query Eagle Sue self cured our stock it Well that's actually not Q or Are so it's not a keyword argument much like a UR

L Would be Instead it's a request argument a request that gets docket Q So again Q That's coming off of the name and in here will say if query Then QS Egoistic us that filter and will say the name

Contains who is the query And double check to make sure that's accurate Query statement Russia location name cool So we say that and we refresh in here and I search fish tacos hit enter only show me Baja fish tacos great so this is its own new custom query set so I can actually do all sorts of things in here as far as the filter is concerned but I don't actually want to put all of that logic in here because I might want to use it again somewhere else so instead of my models I make something called a model manager and that's going to allow me to have it in their sole say class restaurant Location

Manager models top manager and will do to find search And it's going to take in self and some queries And it's going to return get query sets The filter and this case will just say name equals or I contains Equals to query

And since we've created this manager I just need to add it or appended to my object manager my normal manager so this of course is just adding it to You know model objects that all also you know where every C A Related to this particular model OK so now I can actually go back into that view And instead of if query what all I actually do is I'm going to change this In I'll say

So the filter will say search query So it's going to actually search the query itself and then from there all filter down well actually we probably want to filter down before we do that search right so let's go back into our model and bring it up one more level and that is I want to either add in the user here or what I want to do is actually have a search filter built in to it so I'll leave this here and all add a custom query set method so we're going to do a restaurant location query set and this is models that query query sets and will do define search again and it takes and self and query and this is our term self dot filter Well it's going to be the same filter is this some go and cut that out pay said here and then this is going to be self out search query So what that means is we are going to have to override the cust the standard query set by just by doing find get query set himself and we're going to return restaurant location query set of self using self to underscore DB So using the same database and this will give us that search so now we can do a search query sent on as so this is going to be

Restaurant location To objects that search and this is going to be pressure on location Objects to all search query or even the same thing that objects that filter something that search So this just adds the DOT search functionality to it this is doing its own you know set itself this is querying down any basically So it's filtered out any queries that OK so let's go back in that view In here and now again we had that original query set was by owner So if query exists then I'll just do Q

S That search that particular cure query we save that refresh in here And we have a little error that error has to do with this restaurant location queries that we just do self model here Slight little error of the refresh same queries coming through but now it's just for this requested user so let's just show you exactly why we did that so if I just did it restaurant locations search that query so its comment this out And say Q

S Equals to Search query that user is now no longer being passed at all so if I refresh in here now I have two items one of them is actually coming from the See if you use or and it's not showing this so maybe it's coming somewhere else so thick in the admin as far as rational locations is concerned we have TALK OF THE see if the user stock goes oh yeah of course it's not showing it on the C If you use or because there's no item they don't actually have the Russian items in there OK So that is the difference between those two so when I change it to being filtered down even further then I can do that now why is it that I actually created this this is so we can have a more Complex look up it's not so I can just have one so what I want to do is add in some more complex look up so Jane D

V Models import Q and You've already seen this before but now I can actually do a kulak up and have multiple things in here so Q location I contains Query And category and all sorts of look up so let's go ahead and just write those different ones so we go ahead and copy this bring it down and see category

And then we can even go one step further and that is the actual restaurant item or items that are related to this so item so let's say item title I contains queries so let's try that out so we save it and we refresh And our terminal up we get a little syntax error here going to get rid of that last pipe line there And we got invalid look up related field to envelop lookup title and that's this right here should probably be name we refresh in there and there it is so Baja fish tacos notice it's showing me a bunch of things here some add one level to this and that is distinct Refresh in that way it's not showing it malt Will times as far as how this is concerned so let's say for instance I want to say Carney And I did a search for CARNEY So Carney aside it is actually on here twice or it's a CARNEY It's not a bowl

And it's not it's filtering down the different it's including the restaurant so that has the item and the restaurant and all the other items that are in there so I can basically say item name contains that but I also might want to say item Contents contains that as well right so whatever Carney Assad is in there still it's a beings But it could beings a search for beings us they'll get the same stuff if I get rid of the search so there's no queue then I got this stuff so let's say for instance I wanted chicken and search for chicken and now I have two places where I actually have chicken if I do a search for Mexican I have multiple places for Mexican you poor beach Again multiple places of course this or this relies on me as a user to write these things correctly but it does make it a lot easier to actually go about searching stuff by creating these custom managers and now that I have this custom manager I can do the search anywhere so going back into my profile view this right here can happen quite literally anywhere you just do Dot search and do you know that I can actually put this in here as like that get rid of query Save that here go back into my model and realistically just say if Kriti

Then return that otherwise return self that's it so if there's a query actually then we will do it if not then we won't so here are that and boom there you go so what does that mean we can actually go back into our model Model manager and just realize that hey this is a quick and easy way to do a search distinct make sure that we don't have multiple the same one so basically if it's the same item it's not going to show it several times where this potentially could show it several times because it might match several different things multiple times or several different items of this right so if Chicken is in the name as well as in the category as well as in the contents then it's going to show it multiple times by doing distinct means that on any query set it will not show it multiplies OK So this is now a lot more robust so if I called up a friend said hey give me something from AAA That friend can look on my actual site or I can just send them This link and say this is what I like from AAA Then they can just pick me out something from there and a like any of those things so that's it for the user search feature if you have questions on how we went about building this and the fun process behind it please let us know I will say of course you can absolutely one hundred percent make these links quick little for this particular user so I'll just show you a very quick example of that Just so you know that if on any of these contents so when I had a iterated through before

This is how I'd have to do it so for Ingredient in item get contents I could use a equals two and it's going to be in this particular path so the request path And then I do question mark equals to Q Plus and that's going to be whatever this item is so I'll just say item or I N G and

Say that refresh and now if I click on any of these it's going to do a search for it so I click on that And that should be and question mark equals to not Plus So try that again I could look at does a search for that Black beans Carney saw that I didn't actually create that one correctly so I might want to edit that please go ahead and do that menu items had one long one that says black beans click on that and much as a comma here it save Refresh back in here And now it's so

Freighted So I click on this and each one is now searchable that is well that's actually a little bit nicer of a user interface so let's go ahead and say that and there we go and I can actually do the same idea with a location Might as well And this does that search for us In just a more robust fashion And of course it's just on that particular user's stuff so it's not like it's going to go everywhere and just that one page so I hate Coast Maysa Meskin it does all of that so Mexican is not actually showing up so why is that so it's going to our model again into our query location I contains that query

We can also do location I exact That query and it looks like Mexican is still like coming up so let's go into our restaurant itself with its location Which is print out the query or a quick Oh I know why it's not location it's rather it's category And you need to print that out refresh the category I contains and we already have category so we want category I sacked

So in my in my contain it or it might be exactly that so that's another thing that you might consider And it seems like it's NOT want to work so it's to Mexican as lowercase through a quick or go click on my skin again Oh I know why there's a little space before it slight little error in how we rendered it so What we want to do is trim out that space on our actual model so what we will do is on Query is we want to get rid of extra space and it's real simple as we will say query Equals to query strip

Say that and then we refresh and their account now it's working OK so there was a little bit of a space there which makes you no no sense if I had spaces here and did cheese it should still give me that that example right so it's incorrect up here but as far as the actual look up is concerned it should work correctly so we go to Mexico and coast made You part beach so now show me all of those things and I may get some to me as a user to make these as robust as possible so if you have any questions on what we did here and how we did it obviously let us know otherwise let's keep going So since we have the ability to see users I can look at any individual user I actually want the ability to follow this user that is I want to be able to access them quickly and like as if there is a tab like I have restaurants I want to have a user's tab or followers tab or following tab And I also want to have the ability to see when ever they post a new item or update an item I want to be OC

Either one of those things so what I need to do is set up a way to actually follow any particular user now to do this I have to create a new model and I'm to put it inside of profile profiles itself so what this is going to do is it's going to be an individual profile for every single user the nice thing about this too is we're going to actually be talking about a few different foreign key things that will come up using this sort of process so the first thing is it's going to be the main user Whoever it is it's their profile Saul do models Top Model or sorry models not foreign key as a default now of course we need to get the user model itself so we do from Django dot com import settings and then we'll do user he goes to Settings dot off he user model It will be clear as to why I did it this way versus writing settings that author user model here so we go ahead and put the user here and then I want to get the followers so I'll say followers And I also want to get following so who I'm following so following So this is unique for every single individual user how many followers they have and how many they're following Now of course this will not account for blocking people from following you it's that we're not really going to cover that that's getting a lot more advanced than what we need instead we're going to do is just allow anyone to follow us because the information we're giving is what's really not that personal and honestly you have control over whether or not it's going to be public so there is still that aspect as well and when I say public it means also to your followers of course right so if it's going to be displayed on the site other than in your own page you can adjust how those things are seen that way so now we've got these foreign keys and we've got a few problems with them number one being if you remember back when we did a reverse relationship I could do something like user profile sent to all right that would allow me to get that model but I have three foreign keys so I can't exactly do it like this with all the so we need something to replace this on each one or at least on two of them but then further foreign key I don't actually want to have a huge set

I want to have one item one profile per user so whether the user is created or whether the profiles created I warmly want to have one for each of them so that's easy we just make a new type of foreign key called a one to one Field and what this does is it changes from user profile that's set to just user profile and this will actually allow you to grab that user profile reverse much like you do profile user you can also use user profile and that gives us that one to one relationship now followers This is also only allowing one item I mean yeah I can have users in all of these but what I really want is a many to many field you'll see what this does in a second but I just wanted to point out the fundamental differences between these so you can see them in action so the user we're going to add something called a related name and also a user followers And will add another related name here and we'll call Following Well we can actually leave it as followers and following so then what we can do is users followers users following so that's exactly how it should work I'm also going to allow these to be blanks of blank being true with many to many fields you don't actually have to have know in here one to one fields you could but basically if you write knoll equals a true it will just say you don't have to do that OK cool so that is our profile now we can add a bunch of other stuff like our default timestamp and updated which you may want to do

And you also might want to have something called activated right so like during the registration process you might want to have it here activated And models dot movie and Field will talk about registration soon but also a default equals to fall so it's it's this profiles not activated by default in our in change that is simply by having them sign up online now the next thing is the user profile now I actually want to create a profile for my user model and I want to do it after they are created so once it's actually created so go ahead and go to from Jango dot DV Dot models that signals

Import post save so after the user is saved or created we want to actually create or at least make sure that this profile exists so we're going to do define post save the user receiver and it's going to take an sender instance created is a boolean args and keyword ours and say if created Profile equals to profile and then is created equal to profile that objects get or create user equals to instance so that's what's going to be passed and that's it And then we'll disconnect it will do post save connect and the receiver function And the sender is the user model so this user right here or settings not off the user model again if you had a custom user model this would still work so all of this is how we're actually going to create our profile and then I'll make an ST

R Function it's not required but it is nice to have and we were turned south a user to user name OK So again of course that's going to the actual user itself and the user name there so we save this and since in based on PIO actually already have profiles in there what I want to do now is remember Gratian so pit Python shoppy Why make migrations And then Python Misha P Y migrate All right so I still have the server running so let's go ahead and jump into the admin and take a look While I have to actually add it into the missiles go back into our profiles app and it in salute from about models import profile and site register

Profile OK so we save that refreshing here now got profiles now of course my users my current existing users don't actually have profiles yet so let's just go ahead and create a random user so we'll just do random underscore Arend best test And we'll just give it a whatever password doesn't really matter and I hate saving continue it brings me to this user cool I go back into home I go to profiles and I got random dot test here so this is showing me this user's profile followers following right and it's giving me different users so since we have this somebody go ahead and say followers are see if the NJ Michel three and then following just three and I'll give them activated just just to show an example so now we're going to do is jump into the python shell so do Python manage the shell and we're actually going to get this information based off of the user so as we've done before I'm going to go ahead and do from chain go dot com trip off import get user model and the user of course is get user model User to objects all this gives me all of these different users of course so I'll just say A

BC It goes to that last user so user to objects dot last and that's going to be of course a random test user so if I do ABC

Dot profile all actually good Instance of this profile if I did ABC Restaurant location set that all what should I get well I just created this user and all I did was check their profile so I didn't really create new original locations so I get an empty query set now is this user actually has or does this user have followers and are they following people so to get that information going to A

BC To profile the followers And all so it's as a mini related manager so we can actually treat it as a crease set and do all and as we see it comes back as a query set of users so this is those followers right there and then following Oh it's a do is just change that to following and then all these are the users that are following this particular user OK cool so that is showing me my youth my profile followers as well as the ones that I'm following Now what if I went to in created another user some to go ahead and go to see a female and all create a user followers being judgmental three and random test and then following just random test all activated saving continue and now I've got this profile and of course if I try to create another profile for any user that already has one it's going to give me this error that's a big part of the one to one field validation testing so now what I'm going to show you is since I have this user profile what I want to do is get the reverse relationship that is I want to see how many what these users what this user is in side followers and what they are inside of following so what I do is

What I'm saying is I want to get the reverse relationship I want to get this stuff so user done followers not all which is A B C user followers That all so ABC Being the user and then followers all now this is where this user A

BC Is inside of the followers portion right so that means that AB

C Actually follows CFE Right so in another way to think about this is if A

BC Was the request user this is going to give an error but if the ABC

Was the requests user and we wanted to know everyone they were following we would say requests request that user followers but all so this is who they are following so the related name of followers is actually not that good so let's change just to something different and say is following will save that we're going to close out of the shell And we'll do Python manage to make migrations and then Python is not PY Migrates And now I'm actually going to go ahead and write some of these shell commands in here I'll just come in and do shell commands stop by just so us can see them it will do from Django to contravene off import get the user model will say user he has to get user model and from here all say the random

Equals the user the objects the last And now I want to see the my my followers are random not profile followers but all Who I follow He goes to random dot Is follower I think that's what we call it or sorry it says is the following

To all so who I fall Oh is this right so these are my followers because it's associated to my profile who I follow as in the reverse relationship as in I am one of the followers of other users that's how you do it right so technically we don't actually want this following portion or we don't necessarily need to have it because this command is the same Well if we saved it correctly it would be is the same as a friend profile that following All Those would be the same again if you saved this correctly now you might not save it correctly so they might not be the same so really that field itself I don't actually need this is the following field this is who I follow these these are the ones I'm following and then this is who's following me so back on my model I don't even need that field anymore and that is the related name so my following are who I'm following this is how you get it

Hopefully that makes sense it's going to be a lot more sense once we actually put it into action and this is where the foreign keys start giving a little bit complicated and this is where you'll need to practice it both in the shell as well as inside of views and models so what I recommend you do is play around with different many to many fields as well as one to one I think one of one's fairly straightforward it's many to many that gets a little murky and the reason that it gets murky is because many to many means many users can have many followers and many followers can have many users so it's many to many where one user can only have one profile and then that means one profile can only have one user that's kind of how it works OK So that's it for you The model portion will go head in and make those migrations again with getting rid of the actual following model or rather the following field and that's where we're going to go ahead and leave it off with one exception I will come back in here and make a profile for my Jamie three user and the followers I'm just going to say is everyone and I'll just say activated And then go so I now have a bunch of different profiles and I'm following their get OK and I will say just one more little default thing that I might end up doing is when my users created maybe I want by default to have them following some sort of like user that I want them to follow so just a default user or I can have multiple users but basically what I'm doing is I'm to say the Default User Profile equals to profile objects that get it and it's to be user underscore underscore ideally equals to some ID's So in this case I'm saying one as in that first user that main user created of course you could use a user underscore underscore user name as well and set some username of course you would want to have I exactly all those other things like we've already talked about but this will get that profile and since we're creating those profiles as default I don't need to do get or create I could as a safe guard is to get or create and then just do zero because it actually returns back a couple but basically this is the default user profile and from here I can just add and the instance or the user that was created in this receiver function to the followers of this user so if I do followers add

And it's instance so of course that the user that was created because that's the sender and this is the default user Profile that I'm adding and then I just add that to their list of followers so that means that this newly created user is now following that default profile that's what this does and you might be tempted to do default user profile that saves right so it seems like we saved or change something to that actual instance this is the intuitive thing to do but with many to many you don't need to do that you just add things too many to many like this you can remove things like that so I'm not going to actually remove anything but you don't need to do these two things I'll go ahead and save it to test the Dow will go into home users will add a new user and also a user four and whatever password I hit save this should have created the user's profile so I go into that profile it is there and click on it obviously I don't have any followers yet and if I click on my first user or the user of the idea of one I go ahead and click on the see if the that user has an idea of a profile idea of three but the user id of see if he is one so if I go back into that profile of see if he I do see that I have user four in there as a follower by default now the reverse might also be true too you might actually want to have this particular user added as a follower to does Newt user just to give them some boost of users right in the easy way to do that would be profile Followers ad and this is going to be that Default User Profile dot user Right so we get that default user profile and then we can add it this way as well so we can do that and what if I did and and just a number so let's say for instance two and we save that now Credit to the user

Add new user user five Whatever password It saves we go home go to profiles I don't see the error happen I going to user five now now I have to default user followers right so that might be a good thing to do as well you fall so in by default and then you have followers by default especially if it's your service right you definitely might want to do that to encourage this sort of behavior so they're not just in T All around and they have some content to look at right off the bat so that's how we're going to leave it we don't need to save or remove methods anymore just something to point out but that's essential e doing followers and following so now we need do is route those of course into a view of some kind to enable that to happen even further So now we need to do is create a follow button so when I click on follow it's going to actually allow me to be one of the followers for this in fact it will be a follow toggle So first we'll just have it as a follow button and it will stay the same thing and then we will adjust it as we need so what to do here is going to create a template tag filter after I create this in point so the in point is going to be called class profile follow

Toggle And we want to take in a view and just really just handle post so it's going to get self request args and keyword arguments And we're going to print out requests request data And we're going to return Let's do a P

Response or just a redirect so we can actually import from short That's a redirect and we're going to redirect it to user Three OK so that's what we're doing first so this is profile toggle and I want to print out the request data or rather print out request post as well so now that we have this in point I'm going at it into my you or else And I'm just going to go ahead and put it in here and as follow In fact let's actually put it into our main site here and we'll do

Main Site Urals of course from profiles Views import profile follow toggle and I'm just going to come in here and say you're ill are carrots follow Dollar Sign profile follow toggle as you and will say name equals to follow OK so we can also just say profile follow as our in point that's fine too But now that we've got that I'm going to go ahead and jump in to my template specifically for any given user So here's my user template and I'm to make a new snippet for my profiles and we'll just make it a new folder snippet

And inside of that we'll make it as our follow form him out so in here will do form class equals two form method equals to post action equals two Well we want the action to go to our newly created follow your will so back in its form will say you will follow Close at that form and with the input type equals to hit in the name equals User name value equals two Well I'll say if you username then all you put in that username And then else I'll just put in Jane Mitchell three

As a default And then close off that inputs and then we'll just put a button here and put a class B T and B Theon defaults or let's say B T M primary

And then we will say follow OK So we've got our follow form and inside of a user now we've got a user to user name here Next to that I'll just say follow so put a P tag in here and we'll just do include profiles That's nip it Slash follow underscore form to HTML And we can use an argument and we'll say with username equals to user username OK so this will be evaluated in allow that form to come in we close off that P

Tag we look here Let's go ahead and make sure a service running up we've got a view is not defined so back into profiles we need to import view And we refresh and there it is so now we've got this button for follow I hit enter it takes me to that your L So it's not actually going to the right place So let's go ahead and save the profile view let's go back and look at that form we've got our method being post let's add our C S R F token here because we left that out click on it now and then it's still saying that so I want to refresh and here I click on it and now it's saying to us GI

Does not have data and that's why Because it's not that kind of request so going back into that view we'll go ahead and get rid of this Cancel refresh and here it follow now it doesn't go anywhere it actually redirects me to the user so if I actually went to use or see a theme and it follows can take me to that other user right OK And when I print that stuff out I see the data that's coming through so this is actually the user that is coming through here so that means is I can run some toggle here and now I'm not going to actually handle the logic in here instead we're going to do is I'm going to use the requested user and I'm allowed them to actually do the request here so a couple things that I also want to add in is that user required mixin So go into our views for our item list or menu items and we'll add in that user required mix in Right below the get user model So log in required here since we did the log in required there in our follow form and have it there as well so if request that user is authenticated

And if OK so we save that and if we check it again on a different page see that it's no longer there very good so now what I want to do is I want to check if this user Is can be toggled right so looking back let's close out some of these things that we don't need open right now And we don't need to save changes that OK So we've got the follow form our models and that's it so what I want to do is have the ability to just check that The user is authenticated so let's try this again will say user to toggle and It's going to be request

To post Duckett and this is username so we say that again and we'll print out just user to toggle And I'll comment out the request post Refresh and here I hit follow terminal it only shows me that user I go to the other see if he user ahead follow and it only shows me that user OK good so it's actually show me the correct information but now what I need to do is actually need to get my profile itself and handle that so we actually have an import of the profile because we haven't had to use it so ado from the models import profile And now actually want to get the profile for that user so profile the objects to get user name I exact equals to user to toggle And this should actually get it if it doesn't get it then we will have to raise some sort of error but I'll worry about that in a second so profile equals to that and we have a requested user so me is the request to user and now I just need to check if or basically just to toggle this particular user so one way I can do it is profile dot

We want to see followers all that's going to give me all the followers also if user in those Then we'll just go ahead and remove them remove that user so this will be remove and use or otherwise will add it so if it's in there will will remove it if it's not in there will add it OK so now that we've got this we have a way to actually toggle it we will clean this up in just a moment use In our model but for now and leave it as that and then on that view for that particular user I want to see if this user is actually being followed so we'll see it if user

If user so this is the now no longer it's the requested user it's now the user that's actually being displayed so this is displaying user in request that user profile followers Not all No print the C's will hear that and will say it is following But that's not correct right remember how we did that reverse relationship so this should actually be is following To all so if this user is in the requests and user is following will say that

Refreshing here it doesn't seem like it is let's go ahead and look at that profile so I'm actually logged in as J Mitchell three so that probably doesn't help I'll go ahead and go to see if he will hit follow and it's as cannot resolve username into field OK good so that means our view needs to be updated and this should be a user underscore underscore username tried again hit review there we go go back to that see if the user and it's still not showing it so let's go in here request that user is following it all so we'll go ahead and display what that is here Refresh and it's showing profiles it's not actually showing this user so you go ahead and file them again and now that user's profile is actually in side of that following so if user dots profile is in there then it will show up right that's it so if the User Profile is in the is following because that's giving us back a user profile query said it's not given as back a user queerer set so now that we've got that out of the way We can actually put in side of

This call here we can do the same sort of idea As a past argument but of course we won't want that logic being on The View itself we'd want the logic coming through here will say is following equals to true we want something more along those lines so then in the follow form will say is following them will do a follow Otherwise We'll say follow And we can also do this same idea inside of the class is is following will say BT in default and follow BT in primary

OK So that means that inside of that view where rendering our user ID She mail we need to handle this logic for some is following variable so we come down here get context data the user context of course is coming from the actual user itself right so we're actually getting the user items in all that right so that's the context that's getting that user object so what that means is we can say if user profile in Self requests that user is following It all and will say is following is true Is following is false And then in our context

Is following equals to his following Ok back into the user view now we can pass that in as an argument instead of saying true we say is following is coming through there we can get rid of this we save it refresh and here we got a Else clause coming through so let's go back into our form if else and if Not is but if we go Refresh announces and follow I click on follow and it says that user and follow right so back there it's now working but the redirect is not so let's go back into this redirect And this should go back to our particular user so instead of user name here we use an F Statement and just get our profile So again this is the user to talk also this is the not the requested user but the user to toggle and it's going to profile the user and username

So we save that and refresh and follow follow See if we on follow follow and of course we can check to ensure that that's who they're following so here are the users they're following so the user five and follow Refreshen here Wrong wrong user actually we want to go into J Mitchell three because that's who I'm logged in is four and five are not there follow five refresh in five

Oh yeah of course we actually go to that particular user user five Jim Mitchell three that's the one I'm logged in as a refresh and follow come back in here it's no longer there refresh follow And there it is OK so the following portion is working this logic though should actually exist inside of the model so So let's go back in the model and we'll say class profile manager model's top manager and we say define toggle user or toggle follow and it's going to take an self the user to toggle and the the user to add or user to follow So the profile user And the user to toggle

OK So let's go back into view because we have that logic are ready And I'll go ahead Inc copy this and the user to toggle so the profile user or let's call that actually the requested user And user to toggle which would be the user name to toggle In this case so we get the profile username The user now is requested user and then will return whether or not it's been followed so has followed equals to false or is following rather as following equals to false and then only if it's on true then will say true And will return back is following and they were going to add this into our objects so objects equals to profile manager

OK so now it's all logged locked and loaded ready to go so back into our view all I need to do here now is get rid of this And say profile Objects check it for sorry toggle We called it toggle follow And this is going to be the requested user so that's a request to a user and then the user to toggle

Right so the user name to toggle actually so let's call it user name To toggle And what I actually want to do back here is a return back also the profile of that particular user so I returned back to things So back in my view also a profile underscore and is following equals to that particular call so we can actually print out the is following and then a redirect will still work because we are still sending back that profile and since I can get rid of all of these comments Save it and will refresh in here on follow this user so as bool object is not iterable So let's look at it views post line eighteen

And it's possible that I didn't say this side profile manager will continue again and that's what happened so they were built so it's now actually following these particular users or unfollowing them as needed OK so to improve this you would use something called Ajax or asynchronous loading this is synchronous And so when I click on the button the page actually reloads when I do that if you're interested in doing that check out our tweet me project it shows you how to do it using J Query that it's a fairly straight for an easy way to do it but now that we have this method here we can actually change any profile at any time and since we have this form we can also use that anywhere that we need to pass a user ID right so all I did here was pass in that particular user name and whether or not this particular user was following that user so we could update this to have some more logic in here and whether or not they're following but by default we're just going to say that they're not and we're actually also going to have on here the only place we will actually follow users is right on their user profile not anywhere else so that's literally the only Place to do it which makes our functionality a lot easier to handle So now we have these following and followers we want to change our home page to actually show their particular items and that something will do in the next one if any questions let us know let's keep going But now what I want to do is actually show updates or any new items that are created from the people that I follow on my home page so that means I need to adjust or change how this portion works so to do that we're going to jump into our views dot Pyatt here and it's kind of like a list for you right so it's kind of like we're actually going to be getting this and fact and these item list view and detail of you we actually probably want to have the log in required mix ins on here because again it's about seeing what you have a user so it's a little bit different than what we did on our profile views for the profile detail right so we actually got some context data related to that particular user and we are still doing that but we want to do it based off of the views for my request of user the people I'm actually following in my request of users so let's go ahead and write out our class hero call it home view and it's going to take in a view some to import view from our generic And the first thing is web to go ahead and get the GET request is going to be the argument for using or excuse me the method we're using and it's going to return so get also takes in request and it's going to turn render

Request and we did homed out a shemale Forssell event and will put into context and this is going to be if not request user is authenticated That's what is going to give Otherwise we'll do some other stuff and we're going to return We'll call it menus feed or home Desh feed and that will actually be our users feed so user equals to request that user and is following he goes to this is is following profiles not is following users so we're going to say user dot is following it all and so those are the user profiles that we're following now we can still get the user in there by simply grabbing out those users themselves so instead will say is following user ID

's and I'm actually a get out of the user ID's by iterating through them by simply doing four X Users ID four X In user profiles all if you don't know what I did here that's just simply for X

and User is following some iterating through using a variable of X and Then I'm just appending to a List X Dot user ID So some lists List dot pins and then X

User to ID So that's that's essentially what I'm doing right there and the reason for this is so I can use these user ID's in a different way and that is getting these items so QS It goes to items on objects that filter and now I want to get these items based off of the user I

D's So if I look back at the item model I have a user here so that's the user and will go ahead and say user underscore underscore ID's or ID excuse me N Is

Following user ID's So this is actually getting All of those items related to whether or not I'm following them and then the last thing would be whether or not it's public that was the other portion of this so we'll just say public equals to true So this is the query set now and I can come in here and say Q

S QS And these are items query sets they're not the restaurants themselves so we can actually copy or use from the items query set itself so many go ahead and just copy how this works and go in here and do home call it home to feed HT

ML We're going to paste in the object list here and again will extend extends they studied him L And will do block contents

In BLOCK OK so if we look back in our view our object list is actually will call this object list to fit with the variable names here so the query set is that we can go ahead and save it this home feed should now actually work we have to actually add it in to our Your else I'm to go ahead and grab this home view and do from men used the use import home view now and will change this to home view Get rid of that save it and will refresh here and there we go so we actually are seeing recent items or a bunch of items is not necessarily recent items so what I want to do then is I'm going to just say ORDER BY updated and we're going to limit it out let's let's just a limited out to three just to illustrate the purpose here so we've got our three items here but it's not necessarily showing our users so we're definitely to have to kind of update and change how the This looks in fact I would rather this look a little bit more like our menu items that we or the restaurant list that we already have looked at and that's going to be our profile a detail view so it's go back and look at that user profile detail so we want to look a little bit more like this OK so many go ahead and copy that and going back into the home feed We're going to adjust this a little bit so it's going object on named this is going to be the item name itself right Item Name and we don't need this for loop here because we have the actual item But what we do need is the contents itself so this is object to get context because it's OPIC name so now we would do underneath that I'm going to say object dot rest dot location I'm actually in the Get rid of the search feature on here we don't need it on here or we probably want to have it on here instead I'll just do object at rest title

And do something a little bit more like that we're still showing the restaurant but it's not necessarily allowing us to do the search quite yet and it's showing us contents below it but before object a name I also might want to put object Tot user user name so we can actually see that and we get rid of these list items some to get rid of this empty call Save it refresh and here and there we go so we are seeing our rest our food but we're not actually seen the restaurant current correctly so rest should be probably a restaurant So all these should be O

BJ To restaurant category there you go refresh and now it's showing me the people I'm following so let's go ahead and look for someone else so Do you I'm following myself obviously but I want to do other ones user five is RT

Following so let's go back and he had been an homage to change some of these items from my user to other users so I just go and save it Save it Ever Go go back into that home page will click on home and there we go so we actually can see the user's stuff but refresh and user five it's actually not showing me any users because I still need to add in those restaurant locations as well so rational location Not actually sure if these are going to work correctly but the point is that now we actually have a way to follow and we can see their recent items so we still might want to clean this up a little bit and that is why I might want to add in some users Your else stuff so in here Let's put the user in here and I'll just put a treath

In that's going to be related to that username And that's when you so object to user to user name is one way to do it or we could just use your Rel and then grab A keyword arguments for it so let's cut this out And I believe will say username equals of that Let's go and look at your else for that particular user and it's profiles

Profiles detail say that refresh and there are now we can click on that user so this is actually giving us that user your L So what that means is all of those other links can also be back to that so instead of request path we can just add in that user's profile as the way to get there and that's true pretty much All of these so let's go ahead and just add them back so Hoops not just a just a tress And come through here

On each one We'll just have to change the query on it A If there were go And change the Q

Item To what that item is Title There we go object A name is the name of the item itself so if I click on chronic talk owes it's going to take me to that user and look for chronic tacos again it's not necessarily working right now because those users didn't actually add in that restaurant so let's go ahead and do it I'll do restaurants and rational location user five will see chronic tacos And Newport Beach Category Mexican in

It save and then go back into Our items menu items User five the new chronic talk goes the one right below it will hit save refresh and here I click on chronic talk owes that it brings me to that particular item cool so I actually want it to look similar to that in the sense that when I go into my view here I'm a get rid of oh here we go so let's get rid of this fact let's get rid of all that UL class stuff And I'll just put bold height of names still and in this case it's going to be object not name Get rid of this UL class here we say that and we go back home and now we seek

Honey it's not a bowl With A real so now it is actually giving us the objects specifically for this user and then if I click on this user stuff I can see the things that they like so that's really all that it is it's not a whole lot different For a lot of different types of feeds this is simply how it may work now in this case I'm doing it where this is searching only inside of that user right instead of searching the whole site and searching that user particular in particular which is good so this is now it that's our feed that showing our followers this is exactly the way we may want it to be the one exception is I'm to make it a little bit smaller and I'm going to say text center and then call them small offset three we refresh that and that is now our feed so above it also H one and user following feed And

Let's put this in here Take the row out actually and put it outside of the for loop the for loop does they do not need to have their own row so they will go user following feed and we click on any of these users we can unfold them or follow them it's completely up to you OK so now that we've got this let's go ahead and just test out a change so I'm going to act like I'm user well and sort of acting like let's go ahead and log in as so I'm a user five I'm going to make their password something I can remember Red thumb ahead to test it so let's go ahead and open up a new page here we're going to go ahead and go into log in and user five We log in it takes us to hear actually want to take us to the home page but here's a user that we're following we're following the See if you use or so what to do is menu items to make a new restaurant here and I'll call it blaze pizza location Newport Beach category pizza Only the slug awful save it menu items add a new menu item place pizza and I will say she pizza

She is Sauce Do It's on public save flip back into my other user refreshing here and what do you know cheese pizza is the most recent item cheese pizza with cheese sauce and know where it is all that stuff I refresh in here only go ahead and take it off from being public all had save refresh and it goes away bring it back to me in public save comes back perfect it's working so that is our following feed this is you know it's not styled necessarily exactly how you may want but it is at least giving us a better idea of how this portion works the last thing we really need to do to make this functional is a registration process and that's going to take a couple videos to actually make happen so stay with us it's going to show you how to do custom registration and this will work on any Django project not just this one but it's kind of important to actually make this a fully functioning service itself so we'll see you in the next one Now we've saved the best for last and no no I'm not talking about registration I'm talking about learning from other people's code yes we will be doing registration but we're really doing it off of something we actually already teach as its own series but we're not doing that to get you to

Watch that series although I obviously do recommend that you do that we're doing it so you can take everything that you learned here and really just start to parse and understand the code better by looking at other people's code so go ahead and jump in to this your L or Right here and this going to take you to our jingle user model unleashed course now this was pretty easy version one point ten although I think I'm almost positive that everything in here will still work for General one point eleven and beyond Intel major things happen or major changes happen so if we reference this in your will to understand a little bit more about Django user models a lesion in fact you can extend the user model you can create custom user models and you can do all sorts of things here but notice we even talk about user registration in there but again we're here to more of just parse the code apart so let's go and click on SRC and One click on accounts and then we're going to go into views specifically for our register of you know it's the register view here is using a function may see you there is actually no class space few here so we're going to be doing a base of a class a few because if we look here it's fairly straightforward as to how this is going to work we have a form we've got this like form valid method actually I'm a zoom in a little bit and we have return requests and all this stuff so let's go ahead and jump in and look at that form so easily logon form we've already done that we're not going to go off of that user creation form the user creation form is the user registration form this is the form all actually be using So let's go ahead and jump in two profiles I'm still going to stick it in profiles because we don't have an app called accounts of course you could totally do that if you wanted but we already have something set up pretty much to hang

And all the user registration and also since we have this user profile we might want to do some stuff related to the user profile in the registration process especially because we don't have our own custom off which is something that our custom user model which is something that this takes into account we aren't taking that into account OK so let's go ahead and go in two profiles or we're going to make a new file called forms So again it's inside a profiles forms up by go ahead and save it and just copy this code right here user creation form I'm actually to call it instead of user creation form just register form because that's really what it is now a couple things that we need to import and that's the user model here that's for sure something will need to do and then on this save portion we will override that as well all explain all that stuff once we get there but this is the user model so let's go ahead an import from Jane go import forms first to get that forms right here and then from Jane Doe dot contraband dot off import get user model and we'll set a user He goes to get user model OK So registration form is a create few We are actually creating our user here so that's what this form is for it's using model form or crating a brand new user so then in our views we are going to go ahead and import this so from forms import register form we're going to make a new class here we're going to call it Register view what a concept and it's going to take in a create view we've seen all this stuff in action so create View Form class will be register form Template name will give my own template name here and I'm going to

Files registered RTC M L And then we're also going to do a new success your L and I'm going to just go ahead and go into well let's just go to the home page because we're going to want to send an e-mail saying hey you need it verify this sort of Christian so many go and copy this and bring it into our route your L's we're going to important in there luckily we've already imported something else already sure views here and above log in or below log in either one we're going to go ahead and do register

Register view as View and register go ahead and say that and since we have created our form as well as our view we need to create our template now so profiles templates we're going to make register HTML Now granted we could put it in with our log in stuff well where was our log and stuff so if we actually look at our you or else we've got a log in view here

Our logon stuff is inside of registration so maybe that's actually where we're going to put it so let's actually do that will do registered on HTML I just want it right next to logon because that makes the most sense so I'm to go ahead and copy all of this and paste it in here for register

And we'll leave it in as this if the user is authenticated We want to well I'll get rid of that as well Came and we'll also hear of this so there's the red shirt on H T M L M to get rid of it from the profiles and then go back in here and we'll just do registration Register and we've got a richer view now OK let's make sure a service running like that is go to register And when our registration view is here I had log in it creates some stuff for me so I want to actually make

This ridge view a little bit better so will do did class equals two column small six column small offset three Just to bring it into the center wall sees the class text center and then we'll put an H one tag called register There we go Let's put that in of here and then we'll put a peak lasted you need to A trip if

You will and log in Did you need to log in close off at a tag course this year all action is actually going to register the value should the register Refresh in here and there we go so now we have a register will put that did you need to log in but load the form instead of above it There we go OK So now we're going to go ahead and create our username so just the AB

C One two three ABC Want you three G

Mail dot com new password new password you know what register passwords don't match OK so where is that coming from let's look back at the code that we compete so we've got to reassure form I actually created my two custom fields here right so password one and password to those actually aren't fields on the user model so I actually added some fields to my model form that's what happened there obviously you didn't see me do it if you don't watch out of the video but that is what's going on with this code next thing we have a clean password too so this is actually doing a method to check to make sure the passwords match and if they don't match we get a validation errors saying pets passwords don't match that's what we saw right here and then finally we have this method called C So since there's a model of associated and it's a model form save on the form actually It does happen and that's what's happening here so assuming that it was all ready for validating everything it's going to go ahead and save it and it saves the password or the provided password in a hash format so on the user model there's something called set password this is actually how you save the password to a user you don't actually say user password equals to password or whatever the password is you use this method called set path set password with that raw password that's how you do it that's secure that's what you do and then finally we see this user is active equals to falls so basically when we register a user we want to make sure that they confirm their email so what I'm going to do is actually do it after this user is created why is that and that has everything to do with the fact that after a user is created we have a profile so we can actually do send Activation e-mail on that profile because if you remember the profile by default looking in the profile model by default activated is false so you actually want to do two levels of is active the profile as well as the user model so let's go back into that form and that's what's going on here so obviously I need to make a method called send X Activation e-mail on this profile model so do define sin activation email self and we'll just say pass for now so I actually do need to send an activation email here and I will just go ahead and print out activating Or activation email whatever so let's go ahead and refresh near password still that match so I'll just use a whatever password here and I'll hit register

Now what I should see is in my terminal I should see something saying activated it doesn't seem like it happened so that's probably because I didn't save everything so let's just go ahead in try again go to register And I'll just say ABC One A

BC One G-Mail dot com some password and I hit register OK AB

C One is in there and now for a look in here still not showing me the activation so we look in here look at users and these users are actually coming through So it should actually send this activation email so let's go ahead and print out User Profile make sure everything saved on both places User Profile OK

Looks like we have some syntax error here Let's try again to cut down the server and we got an unexpected indent up right here this little little spelling error there OK Refresh and again we're going to register that user And AB

C One to ABC One to

Notice I didn't use a proper email address you can test it out yourself but it would run an error it register and we look back in our terminal now it's showing A B C one two and activation so it's showing the things that need to be activated so going back into that form it's printing that user profile and it's actually printing out the activation portion so that means that that is going to work and if you were using something called celery you could you could delay this you could do a delay method on here that would actually delay this to being sent some other time which is something that would be also very cool for you to learn about which we do teach for sure anyway so now that we've got this sin activation we will have to actually update and change it as we see fit So the main thing here though is when we look at this registration form We've really just copied it from a nother form and that form itself goes off of the user model so it's actually going off of some of the user defaults so if I want to change any of those huge defaults I could but one of the things that I don't have on here is if I say A B C One two three G

Mail again And use a different user name if I hit register With all the things it allows me to register but if I go in my users I actually don't want to have the same user or the same email ever working right I want those to be unique now without doing any huge customisations here what I can actually do is do clean email and it's going to be self and what we're going to get is email it was a self cleaned data docket email and then we're going to get the user objects so it's a user into objects that filter email I exact equals to email if QS

Thought exists They will do for res forms thought validation error Cannot use this email It's already registered And they will return email OK so let's try that again with register

OK So ABC One two three four whatever AB

C One two three G Mail some password And uses e-mail it's already registered right so I just improved that code just a little bit to make sure that we have unique usernames and emails and of course we want to do this from the get go not allow users to have this ability at some point And so go ahead and just delete those users so go ahead and say that

And the other thing that I want to do is if this user is authenticated already on that view then I want to redirect him back to saying like hey you're already authenticated So this is override function called dispatch and it's going to take an self args Q and Ours and we'll just do the Basically if self-taught request to user is authenticated Then we're going to return it redirect to log in Otherwise we're going to return super register of you self dispatch Args Q What are

Make sure redirect is in there it is so now we go to register hoops we have spelling error is off to K Did I ever go refresh and they know redirects right so we go ahead and log in Really it would be a lockout to do it but we don't have a log view so now if I register it takes an admin so I go to admin logon now And we go back to register and now a let me register so let's actually take this to log out OK so we refresh there that's all working fine now and since we're doing that let's go ahead and look for a log out of view there is not a LOG OUT view on Jane you know as a default or at least there might be so let's go ahead and do Django log out view and we click on it here And just search logging out view again and we do have a class based log a few very good so that means I can

That I think it's going to be very much the same to our logon view so go into your L's I'll go ahead and do a log out of you And we'll grab this URL Pasted below

Log out if you log out Out save that take a look at our log out view And it's taking me here logged out so let's go ahead and log in Log in and then register Takes me to my log out of view and it's actually logging me out so there would be different ways on how we could go about doing this so that log out

redirect should actually take me to my log in page so it's go back into our base so based on HTML So by default it's actually log in to sell so log out your redirects will take it to logon and what this is going to do is when I actually go to that log pages you're going to redirect me there and I can also do the same thing with when I log in

Also after I log in so after I log in it will take me to the home page now instead of that accounts profiles thing so we're going to go in and say that it's going to take a look now if I go to log out it takes me to the log in page so go ahead and log in Log out there go so register Now it's come up to you if you want it to do that that is if you want it to redirect to log out so you can't register as a new user I'm just don't leave that off but I just wanted to show you that there is a way to go about doing that and that also provided us a way to have a log out method in here OK so now we still need to do in this is what we'll do next is actually come in here and make an activation email that would send Out a custom activation and then set up a UR

L To handle it we're still going to be copying other people's code to do this so there's really not a whole lot we need to do but just understanding that we still want to send out if you have any questions let us know if those keep going So now we're going to send an activation email using an activation key so what that means is when we actually do this sin activation email we're going to generate a new key and then send an email for it so basically it's going to be a little bit different than what's on this other code but I need to make some changes to his profile and say activation key and we'll do equals two models And I'll get a char field max length Of one twenty

And point he goes to true nor equal to true So go ahead and say that and I'm just going to run Python managed up he why make migrations and then Python is up he why migrate OK So the first thing when I do this send activation email I want to check if self activated then basically what I need to do is pass it's already activated I don't need to do anything otherwise we're going to generate the activation key so self activation key equals to some key will have to generate it so Gen key here And then self that save And then finally we're going to have to send mail or basically send some e-mail here and we're going to return that object so I'll just say sent mail and will return sent mail whatever that is so for now I'll just say falls

Yes OK So basically if not cell phone activated then we'll go through and do that so let's go ahead and generate this key now I could generate the key when this profile is created like I could do a post save receiver function or I could just do it when I need to and that's actually what I want to do is I'm to actually create the activation when it's necessary although those might go in together but in this case I'm going to go ahead and just do it here so to create this activation key I want to make a new Ural and so urs has been a utility function so you tools up by inside of my profiles app and I want to jump back into this code into there Or the other sites you tilt up I am is going to copy this in and this is going to generate some code forces just a random code generator in fact if you want to see this as a blog post it is also available on our blog Inside of code let's say generator or I think we called it short code generator nope let's call it code or slogan So slug random string generator in Python so here is roughly the exact same thing in fact it is the same thing that's a random like if you look at it it's exactly the same so this random code is going to just give us some random code that we can compare against with our actual project so when we go back in here that's basically going to give us this random activation key and we're going to save it to that so let's go ahead and important that code it will do from Dot you Tilse import

I believe it was called code generator And we'll just say code generator Equal to that Now I think we actually have this is a random code generator we do it's actually inside of a restaurant you tills So you actually might want to create something like this unique slug generator for this random code generator because it's roughly the same but it's just going to be doing the random string generator process right since we want to make sure it's unique still but it's not something we go into because it's not necessary now OK So now I've got this I've got this random code generator and then we'll do send mail that's something we still have to do but let's go ahead and try out the registration process or go to register AB

C Once you three I don't think this is a user anymore and then G-Mail dot com We hit register and now if we look at that profile so it's logged in as our user profile A B C one two three and here's our random activation key so this generator this code is actually really way too short so let's make it like twenty five or fifty let's make it fit No it's make it twenty five OK thirty five thirty five five so me get rid of this and actually delete this user altogether And reregister this user

Register And ABC One to three he she want to three G

Mail Whatever password Go back into the admin take a look at this user and then go so we've got a little bit more of Unicode I mean it's probably a lot less likely that this code is going to be able to be copied or generated any way it So it's most likely unique again we can put in safeguards to make sure it is unique but in this case I'm not going to do the The next portion is actually Cindy out this activation email so this is going to have to come in and we're going to have to set up our e-mail so set up e-mail

To a quick search for that nope just e-mail and configure e-mail ingenue Here's a blog post specifically for it some to go ahead and just copy this right here go into our based on a shemale go to the top and we'll put in our e-mail stuff right here so that's all of our e-mail related things you might also want to put your admin stuff in here so like if there's errors on your server that's all your stuff so just go ahead and change these settings based off of whatever your user setting is so like for example if you want to use G Mail which is one of them that we also have on here you can just use the G Mail settings and they would look something like That those are the G Mail settings and that's what will actually stick on there OK So now that we've got e-mail set up let's go ahead and send mail so let's do a quick search for sendmail and we've got configure email or Test team e-mail and Jane Doe with sendmail and notice the set ups already there and then what we can do is import this sendmail feature so instead of our models here

We're going to add in sendmail So since mail is this so we'll do sendmail and go off of these things only just copy this real quick Subject equals to activate Email Or activate accounts rather and then your from email which we got from what we pasted over the fall from email their

Mess RIDGE So the message would actually be an F String in say activate your account here and then we need some Your L But for now I'll just do so activation Key So this is going here and then we'll have our recipient list or the recipients that we needed to go to which is of course our user and do self that user dot e-mail right so that's who we're sending it to if we want to as an H

TML Message we could do that I would just basically put it in here as this so like a PT

A Type of thing And then we use sendmail with all of those things so I can again copy Everything from that little guide that we have and this is how you do it Quick little way to send our messages and this will respond with some sort of thing now I don't actually have my email set up so I'm just going to comment this portion out and instead to print H

TML Message we'll save that and let's go ahead and register a new user In another

And want a G Mail dot com Password continue since mail is not defined Because that sent Mail equals to false but we should see the H

TML Message even though that was incorrect here is that he's to message activate your count here and that's some unique code that's correct so we need to take it one step further and we'll have to give it a path equaling to the reversed call so reverse and then we'll have to do some stuff for that so we'll have to actually have a UR

L Page to handle that activation So inside of our views we're going to copy again going back to this code we're going into views here and we want to find the activation user activate user view that's this right here so we'll go ahead and pay set in in this case I'm actually not going to go off of there I'm not going to change the function based view at all I'll just go off there or go back to being profile key is not key but rather activation key And there we go so a couple things here activation profile so it's going to be this if not activated So activation objects remember we have

This right here so if it's not activated then we'll go ahead and grab first off grab the user is going to be related to the profile so act object or profile this should just say profile And we'll just call this QS Just changing things around a little bit Chris said exists and the count is equal to one the profile equals to that single one user is equal to profile dot user And now user is active equals to true and user save and then finally profile to activated equals to true profile activation key he has to false profile that save or it goes to none rather and this should be right here this should be profile OK I'll go through step by step on this just a moment we'll put in the h will just use redirect instead of response redirect

OK so here we go what's happening is a code is passed through an argument we look for the query set with that code From our profile objects because that's where we set it and then we look to see if it exists if it does and it's only one item meaning we don't have multiple that it's going to go ahead and grab that first profile and it's going to soon that it's not activated simming that it's not activated it's going to go through and activate the user and then make sure the profile is done also and then it redirects it to our log in OK so now that we have got that let's go ahead and look at their your L's into their actual user account in your L's they have this activated here some to go ahead and copy this and we're going to bring this into our your L's and register Pop in right there and this is activate code activate user view well have to bring that embattled go ahead insist to activate the user or rather just activate Let's bring in that view And that is from our profiles again back to the user view we've got some regular expression we're going to just assume that that works so to go ahead and save it now back into my model I'm going to go ahead and import something called reverse which we've seen before with the get absolute Your L Method so Django dot

Coord your ill result for us import reverse And now the path is going to be equal to reverse of activates and keyword args equals to code and self activation key OK there is the path so now these are going to be path instead and we're going to go ahead and print out that HTM

L Message I think everything should be working fine I don't have any errors here and I may go back in and register a new user new Use Or User A G

Mail dot com some password I hit register Look in my terminal and here is activate your account obviously this would have our entire URL

There we would want to put the entire one but basically let's go ahead and paste it in here I'll hit enter and it looks like it took me back to log in let's go to profiles into that new user activated activation key user And new user active whereas The AO User is not active so if I tried to log in as a zero user go ahead and just log out real quick here

And with no It's going to say you know active right or it Well maybe I have the password incorrect so let's just change it real quick OK let's try that again And it's still not letting me log in and it's probably because that user is not active so let's change them to be an active or had save

And they're now it actually logs in so it's actually working we have registration done we have and we have it done from using other code so here's what I challenge you to do is actually go through this general user models and least without watching the series and see if you can change everything we've done so far to the code that's here and even make sense of it so that's it for this one if you have any questions let us know otherwise we have one more step that we're going to do and that is getting this actually on line Thanks for watching So now it's time to bring our project live now when I see Live I mean literally making it a real project much like what you see on movie piccy dot com In fact that's what we've been building right but we'll see here too is some changes that we didn't actually make in the video I actually talk about this a little bit more in the next one but really it's our last final challenge to you and that is not only to watch it and to go live and using her Roku you can actually go live for free as soon you have an account with them and here's the little guy that we actually go through and a video that is associated on our youtube channel as well as this guides if you click on Watch the video there it is right there so that is something that you're difficult to want to do but that little challenge I left you is actually getting and learning more about the code that really we wrote to make it look like this so we purposely left out a bunch of this stuff so you'll have to go back to get hub and learn more so that's what the last video or the last assignment is to go live and once you do please leave a comment on our You Tube channel whether it's in the discussion area itself or on the video itself or yeah even if you're watching a conference version at home leave it here somewhere just leave it somewhere that we can see it you can tweet us you can put on Instagram whatever you want to do we would just love to take a look at your project thanks so much for watching and I can't wait to see you in the next one Her good job you did it you are now at the end so I still have a challenge left for you we left it looking a certain way but online on our kit it's brand new code there's a ton of different code that's on there that we did not on the video in the reason is I want you guys to get in the habit of reading other people's code so I left it with Any comments I left it with a bunch of new upgrades that you can pull and I want you to try and make sense of what it is and what's going on I think we gave you enough tools to really know the things that are going on so I really think that you'll be able to do that and that's my last challenge for you here and try Django one point eleven now the next thing we're going to want to do after this is Chika joints dot com slash projects and check out any of our other jingle projects there so you can build on your knowledge here that is away so you can learn more but you're going to learn the most by actually putting this into action and then maybe even helping somebody else go through this series right so one of the biggest advantages to watching things on video is you can share with a friend and be like hey go through this all help you through because I'm a little bit further on that is a really good way for you to get much better at Django and of course reading the documentation is also recommended even if you can be tried at times there is a lot of really really good information in there thank you for watching and I hope to see you in the next one