Try Django : Learn and Master the Python Programming Web Framework | Justin Mitchel | Skillshare

Try Django : Learn and Master the Python Programming Web Framework

Justin Mitchel, Coder, Teacher, Entrepreneur

Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
29 Lessons (2h 55m)
    • 1. Skillshare Welcome

      1:47
    • 2. 1 The Original Welcome

      2:26
    • 3. 2 Step 1

      3:58
    • 4. 3 Setup your Virtual Environment for Django

      9:47
    • 5. 4 Create a Blank Django Project

      4:11
    • 6. 5 Setup Your Code Text Editor

      3:30
    • 7. 6 Settings

      7:24
    • 8. 7 Built In Components

      4:14
    • 9. 8 Your First App Component

      8:40
    • 10. 9 Create Product Objects in the Python Shell

      3:49
    • 11. 10 New Model Fields

      6:37
    • 12. 11 Change a Model

      6:37
    • 13. 12 Default Homepage to Custom Homepage

      5:28
    • 14. 13 URL Routing and Requests

      5:37
    • 15. 14 Django Templates

      6:32
    • 16. 15 Django Templating Engine Basics

      7:10
    • 17. 16 Include Template Tag

      2:50
    • 18. 17 Rendering Context in a Template

      6:33
    • 19. 18 For Loop in a Template

      3:41
    • 20. 19 Using Conditions in a Template

      5:17
    • 21. 20 Template Tags and Filters

      6:55
    • 22. 21 Render Data from the Database with a Model

      10:56
    • 23. 22 How Django Templates Load with Apps

      7:13
    • 24. 23 Django Model Forms

      7:37
    • 25. 24 Raw HTML Form

      11:27
    • 26. 25 Pure Django Form

      10:11
    • 27. 26 Form Widgets

      6:03
    • 28. 27 Form Validation Methods

      7:48
    • 29. Thank you!

      0:58
26 students are watching this class

About This Class

Django is a web framework written in Python. In this class, you'll be building and learning about Django bit by bit. 

What is Django?

The idea of Django is to build something powerful fast. We believe it's approachable enough for beginners with Python yet powerful enough for pros.

A few notable sites that are using (or have used) Django as a primary backend:

  • Instagram
  • Pinterest
  • Nasa

Django is legit. Start learning it in this class!

What is Python?

Python is a programming language that does all kinds of amazing things! You can:

  • Create web applications Using frameworks like Django and Flask make it easy to launch web applications. Even Pinterest and Instagram use Django!
  • Create animations! Pixar has used Python to create their animations
  • Launch Rockets! SpaceX and NASA have both used Python in the rocket launching technology
  • Artificial Intelligence Frameworks like Tensorflow, Theano, PyTorch are written in Python to create amazing ai-based projects. Keras is another high level library that makes using Python with Deep Learning/ AI even better!
  • Data Science Doing reach data analysis using Python is super common and made more simple with projects like Pandas, Numpy, Jupyter Notebooks (iPython Notebooks), matplotlib, scikit learn, scipy, and many others.
  • Data Scraping Parsing data from the internet is a key to many projects. Using Python makes this process simple and straight forward.
  • Internet of Things Python is quickly becoming a key language for creating IoT projects. You can use a device like a Raspberry Pi to control sensors, motors, and more all using Python. It's great.

As you see, Python is great for a wide range of projects... and yet, it's great for beginners because of it's clean syntax and much more. Are you interested in starting? Check out 30 Days of Python for more.

What is a Web Framework?

A framework is a shortcut to making things happen because it includes a lot of features that many projects need. Web frameworks are no different, let's think about an example:

Your app needs to remember user data like login, username, email, and such. How do you design this? Well, you could build it from scratch or you can use a framework like Django.

Web frameworks, like Django, often handle user login, storing data, and many other things that are required for building a functional web application. This saves a LOT of development time because it removes work in building things that nearly all web applications have.

In short, frameworks save us time by leveraging the expertise of a large group of developers.

Transcripts

1. Skillshare Welcome: Hey there. And welcome to try Django. I've actually added this course to skill share because I want to help you guys learn how to build Web applications using python and the general Web framework. Now, Python is a programming language that is very powerful. And if you're not familiar with it, check out our course on 30 days of Python. And for the rest of us who are ready to jump into Django do realize that this series is also available on YouTube. So put in both places. But the nice thing about watching here on skill share is number one. You gotta watch it where you watch other courses, probably. And number two, you don't see any ads. So you, as a skill share member, get to see all of this stuff ad free, and that's the big benefit here. But do you realize that? Yeah, I ask you to subscribe. And of course, I would love to have you on YouTube as well. But for those of you who aren't ready to jump over there, it's gonna be right here, and I will absolutely update it as we go forward. Now, if you want to see something in particular. Please let me know in the comments because I'm building this for you. I want to help students or beginners in Django to come toe, learn and love Django as much as I do. It's been a big part of my life the last five years, probably more than that. But it's been a big part of my life, and I really hope that it helps. Or this course helps you gain an appreciation for Django, but also for writing software and building weather vacations because it's so ingrained and how things are these days. Anyways, I could talk for hours for that instead of talking. Let's let's get some action going and start class. Thank you. 2. 1 The Original Welcome: Hey, and welcome to the try, Django. Siri's My goal for this one is to help you learn and Master Django like I want you to go as far as you possibly want to go. And understanding the jangle Web framework you see in the way we're gonna do this in this one is really Start from the beginning, we're gonna start with absolute basics and then we're gonna work our way up introducing new concepts along the way. And I'm actually not going to be building a full project. So instead, what I'm gonna do is just jump into individual concepts, put them into a practical use case in a sort of project, and then eventually you'll have a very good understanding of how to build riel Web applications using Django. Of course, I do recommend that you learn from entire projects and I have a bunch of them both right here on YouTube, on joints. If he dot com slash YouTube as well as on our website joint tv dot com, we have aton of stuff there from building real things with Django. But this series is all about getting the absolute basics to even advanced level things bit by bit, and we're gonna be talking about those things not necessarily in order, but based on what you guys say and what I've found to be very useful. So let me know in the comments of what you might want to see. So if it's anything in particular that is just not clear to you, this Siri's hopefully will help answer that for you as it relates to Django in some cases will do third party packages. In some cases, we will not. So I'm not really gonna be going over anything other than just the core stuff in Django. That's kind of the goal. My name is Justin. I'm gonna be taking you through this. So I do encourage you to subscribe to get everything that's joined. Cfe dot com slash YouTube or join on our website on joint. If he dot com this series will be in both places and it will be for free so you can absolutely see and learn so much. Friant. And I'm super excited about it because I really like this direction versus making you do an entire project to learn the concepts. I do still love that, but I think Sometimes you might need to do just the concepts, and that's what is all about. Thanks again for watching Look forward to seeing you in the series. 3. 2 Step 1: If you're learning Django for the first time, it's an incredibly exciting experience. At least it was for me because once I was able to actually build a Web application with a database attached to, it was like, Oh, this is so cool Now I think that you might be just as excited. And perhaps you've already done that with other Web applications. Or perhaps this is the first time your programming. Either way, the most frustrating part is at the beginning, not so much because of the programming language, but often because of how to actually set up your system. And since it's so frustrating over years, we were find the way to set up your system, depending on what you're on. And that's part of the frustration is like you're gonna see me working a lot in a Mac so Mac OS environment, and if you're on Windows, you're like, Hey, why isn't it the same? I mean, actually, using python and actually using Django is the same on both systems. Because Python is python and Django is jangle. The commands to get there might differ slightly, but realistically it's the same. Over years we've refined the installation process, and all of that can be found on joint safety dot com slash from zero. So if you just do that Step one that set up process, you'll be ready for the rest of the Siri's. You don't have to go on to any other step other than setting up your system before you jump there. I did want to mention one other thing, and that is the code. So we have all of our code on get hub or joints. If he dot com slash get hub to shortcut it, you're gonna look for the try Django repository. So so join tv dot com Such get help will take you here, and then if you go into repositories, you'll see all kinds of him. And in fact, if you type out, try Django. You'll see multiple there as well. I want you to ignore all of the ones that have numbers in them and just go to the one try Django, right? So there's a link right here if you want to go directly there, but this code will 100% help you because when you hit roadblocks, which gonna want to do is take the code that you've been writing and take a look at Get Hub and make sure that what you've been writing is the same as what we've been doing in the videos. That part is critical. And then the last thing is, when in doubt, consult the documentation. Jangles documentation is very well written, and there's just so much there that you can learn that we won't necessarily cover because they give additional context. Or they give specifics to whatever use case you have for the technology. And then the last thing is, Google is your friend. You know you can use Google to do a search for something that you're not familiar with, and oftentimes that will bring up stack overflow dot com. Stack Overflow has all of these questions from people for all sorts of programming languages, including python, including Django, including JavaScript, all sorts of things in there, and you just do a quick search. You can search for just Django, and you can learn a lot just from the address from going toe like votes. I mean, does Django scale? That's an interesting question to take a look at, and then what is know what is blank? I mean, this right here is a great learning resource as well. So what I'm gonna assume for the next video and all the other future videos is that you have your system set up and ready to work with Django and Python. The versions that we're gonna be using will be discussed in the next one because they will change over time. At the end of the day, I really want you to stick with whatever version is in the video. Because as a beginner, that is critical. Thanks so much for watching. We'll see you next time. 4. 3 Setup your Virtual Environment for Django: All right, So in this one, we're gonna be creating a brand new virtual environment and install Django. If you haven't done these things before, regardless of the version, definitely. Stop now and make sure you go back and do the set up process. No, I absolutely want you to have a fresh virtual environment in a fresh Django install, not only just to get the practice of it, but also to make sure that we're all starting from the exact same spot. So if you open up your terminal window or if you're on windows, your power shell or command prompt. But hopefully you're using power shellfire on Windows, Lennox and Mac users. Juster terminal is fine. So if we type out Python V and Python three Dash V, this is what I get. You might already have Python three in there. So if you see Python 3.6 point five or 3.6 point six right here, you're in good shape. If you see this, then you're gonna have to do an extra step. Okay? Just keep that in mind. I believe that open, and I'm just gonna put it over to the side a little bit and break it down. So we can just keep that in mind while we're doing this. OK, so I've got another terminal window open now. You absolutely don't have to do this, but I'm gonna leave it there just in case. So there's a few different ways on how we can create a virtual environment and install Django. I'm going to show you the way that I'm going to do it so initially, gonna serve you the final way. And if that part works for you, great. You can go to the next portion, which would be installing Django, which maybe we should just do all of that at first. So I'm gonna show you exactly how to do it. Now, I first of all want to keep this virtual environment in one development area. So for me, when I open it up and list everything out, this is what I see. Yours might be a little different. Years might be the same. You might see this dead folder if you don't see it just to make their Dev right. So, in my case, already have it there. So I get this error. I'm gonna just cd into my dad folder, right. This is where I keep all of my development projects. So it here, I'm gonna go ahead and make a directory called Trying Django, we're a CD into that directory and then we're gonna create are virtually envy Before it created, I'm just gonna hit virtually envy and make sure I don't see any errors. I see that there's all sorts of options I could do if I type out ABC there is an air. So if you see an error, that means you need to install your virtual environment all over again. Okay, so I cleared everything out, But if I do P w d I see exactly where I am. I'm inside of that folder. If I list everything out, there's nothing in there. Now all I'm gonna do is virtually envy. Dash P python three. Enter again. Remember how I said if you had Python Dash V and Python three was here, you could just omit this portion right here. Right? So if you see Python 3.6 right there, you can admit that portion and just leave it as virtually envy period we had enter. That creates a virtual environment inside of this directory so I can activate it with source been slash activate and then install Django. So Pip install Django. And my version of Django is going to be 2.0 point seven. So Django equals equals 2.0 point seven. That should also be your version of jingle. If you're sticking with me on this, use that version of jingle. I honestly don't care if 2.8 was 2.0 point eight is out. Use 2.0 point seven will upgrade things later. And if you stick with me on this, you will absolutely upgrade with us. Promise? Absolutely. Promise. Okay, so you hit. Enter and I'm gonna let that run. And for those of you on windows, your activate, you might remember, is different. Just slightly different. Well, that installs. I'm gonna just go ahead and break it down a little bit on the open up another terminal window and simulate reactivating this virtual environment. So list everything out. I cd into death, and then I see the Inter try, Django, I'm back into that virtual environment so I could do source bein slash Activate in a Mac environment in a Windows environment Of course, it's scripts, US activity. And then you can also run. Deactivate. Okay, so deactivate just ends the virtual environment. Cool. So what is the purpose of having a virtual environment if you don't already know? It is a simple is doing. Pip. Freeze! You see all this stuff? I see. Django, 1.10 point four. If I activate it. Source bending, activate and do Pip. Freeze! All I see is Django 2.0 point seven and P Y T c. That's it. So it keeps thes requirements separate. And when it comes to Python and it's projects like Django, you want him? Absolutely. Make sure that that is done. Okay, so we have everything installed. If if this part was done, you're ready to go, You could move onto the next one if you had some issues or you want to see other ways of starting a virtual environment Stick with me. So noticed that obviously we had all this stuff installed, all that's working, so going forward, just just watch, just for illustration purposes. You know, I'm assuming that up to this point, if you have it working, then you're done. If you don't have it working. Watch this next part rewind and then install the things you need to install because I'm going to go pretty fast, but still explain what's going on. Okay, so I'm gonna close out all of my terminal windows and act like I'm starting from zero. Okay, so I jump into to my terminal window I go into I'm gonna go ahead and make a new debt folder called Deaf, too. I'm a CD into deaf, too. And list everything out. Nothing's in there so much, I'm gonna show you three different ways and creating a virtual environment. The 1st 1 is just type out virtually envy and then the name that you want to give it, so ve envy. That is a way to do it Now, this is irregardless of the python version. It's gonna go off of whatever the systems default version is. So I type that out. In my case, it's version 2.7, but that is a way to start a virtual environment. Now, if I want to start it based off of a specific version of Python, I would do virtually NV V and V dash P python for free. Also, it's called V and V two. This gives me a version of Python and there's Python three. So that assumes that if I type out python three, it actually gives me a python three. But if you type out Python three and that doesn't work, this won't work. So what happens then? Well, I can just do which python three Or, in other words, find the location where Python three is installed. Right? So which Python three will not work if python three doesn't work, right, So every once in a while you install python in your system and it's just installed somewhere else. And this command itself doesn't work. But wherever that is actually installed on any system and you do something like this, you actually paste in the path to that and hit enter. That should actually open up Python 34 U. S. So then that means that the final way to actually start a virtual environment is virtually envy. The e N v three p, and then the path to that python three I had enter. And then we'll actually start that virtual environment as well. Now, one other thing I will mention is we can also just make the directory that we want to call it. So I want to make a virtual environment and a brand new directory. This is it. I go ahead and enter a CD into it, and then I could do virtually envy and then period instead of the name and then whatever version of Python I may want, right? So the order of these things doesn't matter that much. How I'm running those commands in comparison in what I was doing before. But that's a few different ways on how you can create a virtual environment. Now, in my case, I'm actually gonna go ahead and give rid of that depth to folder. I I really don't need it, But it was all about illustration purposes for getting all of this stuff going. Okay, so, uh, I'll CD back into my original project and just go ahead and leave it at that. Now, if any of this was confusing and you're lost, we watched the video and do this multiple times because having a basic understanding of starting a new project installing it, you're gonna probably do this fairly often. If you're only here to learn some of the basics of Django. And you don't plan on making very many projects, which, with Django you may be won't need to do it this way. Now, one more note I will say is perhaps you're like a I don't care about a virtual environment . I don't care about doing all these things. We'll do that at your own risk, right? So you're gonna run into issues if you don't use a virtual environment. And yes, I am not using Kanda Anacondas, a package that's MAWR for, like, data science related things. Jango can use a lot of those same packages. In my experience, Anaconda and Django don't work that well. But realistically, if you're using Anaconda, you're probably already familiar with that system and you're probably not going to use virtual environments. However, I will say for this entire series, I would recommend they use a virtual environment just to get used to it, just to get used to how Web developers often use the development environment for Django. All right, so that's a lot of set up stuff, right? I can say that we won't ever do set of things again with Django. But for now, that's it. Thanks so much for watching in the next one will actually get started with genuine thanks so much 5. 4 Create a Blank Django Project: Now it's time to create a Django project. See, I've got my brand new clean terminal window open because I want to get in the habit of knowing how to reactivate a virtual environment. So I come in here and a CD into my debt folder, where I'm holding everything I see the into my Try Jingle folder, where my virtual environment route is. Anna. Just run? Well, no, I don't run that virtually envy. Commanding more. We go ahead and run. Source. Been such Activate, Right? So that's Lennox and Mac users for you. Windows users. It's just scripts slash activity. So if you are in here, you can go ahead and do pit freeze, and you'll likely see this version of Django that's the version were using. That's the version I recommend that you use as well. So I'm gonna just leave that open right over here so we can always see it, right? Just a nice little reminder. Hey, that's the version of Django I'm gonna be using. All right now, let's go ahead and do that all over again. It's really easy, right? This, this Hopefully, this is really, really easy. If it's not really easy I'm going too fast. Maybe you just put it on a slower speed. I know I talked really fast, but I try to get it so beginners can keep up. But also so advanced users like more advanced web developers that are just learning, Django. So all of you can get something out of this. That's really the goal. It's a little bit of ambitious to achieve, but anyways, okay, so now we're in this virtual environment, and I have this command, Django Dash admin. So, Django dish admin is a command that will allow us to do all sorts of things with Django. But the main one is creating our jingle project. So Django Desh and men creates projects and whatever our project name is going to be now, you could do this right off the bat inside of that virtual environment. But what I like to do is create an SRC folder, as in the source folder off the project. And then I go in there, right? So if I go back into my virtual environment, I see that it we have these things in here, right? So I've got the four. The four things related to the virtual environment. And then I have my SRC folder. So I change into that SRC folder in Just Do Django Dash and Men Start project and then my project name. What am I gonna call it? Well, to be original. I'm gonna call it Try Django. That's the name of my virtual environment as well. I realize that. But that's OK. You can name your virtual environment and your jingle project the same because the virtual environment is on Lee for your system. The project yourself will be other places, right? So, like, you'll be on Kid Hub and there won't be anything related to our virtual environment. So I'm insert this project. Try Django, and I just put a period at the end because I am in that SRC folder I hit Enter and A List Things Out I see managed up I and my try Django configuration folder that has the name off . You know, try Django because that's the name of the Django configuration. And if I run Python managed up here, why start or excuse me? Run server? What I should see is it's saying something like this. Yes, it's giving me some warnings. We don't don't worry about that yet, but it is giving me a web address that I could go to. So if I open up my Web browser angle there Hey, congratulations. You have Jingle working now if you're not familiar with Jingle 2.0, and this is new to you. This is a new landing page that they've created. It looks a lot cleaner. I like it much better than the old one for sure. So we now have jingle working. It's ready to go. So it's all sorts of things that we can do with it. But before actually jump in to Django itself, What I want to do is start working with some sort of text editor somewhere that I can write the code that is not necessarily inside of the terminal because then get super tedious for especially for beginners. It gets very, very tedious to write code inside of the terminal. So we're gonna do something different. The next one will talk about that, so stay with us 6. 5 Setup Your Code Text Editor: and this one we're gonna download a code text editor. So writing our code is just a lot more user friendly than just typing all of the commands into the terminal or power shell. Right, So we're gonna be using sublime text, but another one a another popular one is called pie charmer P Y charm. So sublime text is 100% free to download and use. If you really like it, you can just go ahead and buy it. I am not affiliated to either one of these. I just know that they're both very popular choices to write out your code. And that's all you're really doing here is you're just writing out your code in these things. They do have some features to make them even more user friendly. But I'm not really gonna go into that right now. All I'm gonna say is, just go ahead and download this and get it up and running. Once you do, just open it up and you'll see something like this, right? So you can have your window open, make make your sidebar open and you'll see tumbling page just like this. So all we really need to do is add our project into it. So to do that, you just go into Project add folder to project, and then we want to navigate toe wherever our project is. Now. In my case, I goto the hard drive users, my user, the dead folder Because that's where the terminal opens by default is right in here and then try Django, I'm gonna go ahead and open. This brings in all sorts of things in here, right? It brings in virtual environment related stuff on it also brings in my SRC folder, which has my managed up high and all sorts of Django related things. Right. So this is one of those advantages of using the text editor. It just makes it really clean and easy to see what's going on with any given code. And it also gives you these numbers or these lines of code, right? So I can say, Hey, look at line 10 and you already can go boom line 10. Okay, cool. So that's another aspect of using a code editor that I mean, there's so many things to it that I'm just not gonna go into now because I could spend probably an hour just going over the set of process of sublime text. Maybe not that long, but anyway, so we now have this folder in here, and we're pretty much ready to use this project. But what I want to do is just save the project inside of sublime text in here and just save it as try, Django, This workspace thing, that's an extra piece on our testing. So don't worry about that. I save it in here. I've got my sublime project. Of course, if you close out sublime text and you are in your project itself and you have something like this like you're Hey, I'm navigating through back to where it waas. You know, you could just double click on this and hopefully will open up sublime. If it doesn't, then just, you know, do the thing that you need to do, which is get info and just say, make sure that you have open with, you know, sublime text on there. If you're on windows, it's right click and doing the same sort of thing where you just want to make sure that is opens up. But the nice thing is that it just cuts to right where all of my code is. And I can open up all the different files that I need and make edits and changes to them. Sublime takes is really cool. Really like them. I wish they would sponsor these videos. They don't, But I really like the product. So go check it out and download it because we're gonna be using a lot. It's not required, but it is highly recommended. See in the next one. 7. 6 Settings: Let's talk about settings dot pie. This is absolutely a basic thing that you'll and abusing a lot. It has a lot of implications for your project altogether, so we'll just go sort of line by line as to what's going on here. Of course, you can read more on the docks because there is mawr that is going on behind the scenes and just what will mention first and foremost We, of course, import the OS because Django works on any given operating system Windows, Mac and Linux. All of you guys see the exact same thing. If you've done what we've done to this point, so you see, we import Os and then we have this variable named based dir that's set to this long string of what the heck is going on. All this does is gives us the path off where Managed up high is but more specifically, the folder that is holding managed up high, which in our case, is SRC. So it's that folder right there. Okay, so how I know this is Well, I know python Well, I know jingle well, but how you can figure out where that directory is, that's this right here. So baster just gives you, in my case, that right? So your user might be different. And if you're on Windows, it's gonna be different for sure that the slashes will be in a different direction. So the nice thing here is we know that jangle knows where it ISS in the system. That's that's important, right? So we can do things relative to Django inside the entire project. That's pretty cool. And this baster shows us that right at the beginning, Um, and you know, you could print out what the baster is, too. If you were so inclined, you can print that out and just run the server again. Let's go ahead and run that server again. And what do you know? It actually prints out that directory for us, right? Just like I said, that's where it is. That's what it is. Cool. Next thing is the secret key. Every jingle project has a secret key that's associated to it. They're always unique to that project. Or at least they should be unique to that project. Um, and you don't want to make it, you know, public in production, because it it could possibly lead to security leaks, and you don't want that. So just doing a couple changes is good. Okay, next thing is the bug. This is something that's very useful while you're learning, or while you're developing both things when you bring it into a live server alive production environment. It's a real website with real people, real strangers using it. You turn that to false, and that has some implications later allowed hosts like domain names that are allowed not gonna get into that right now installed apse. This is a cornerstone of Django, um, installed. And there's a bunch of them installed by default, which would go over later. But this is where you're gonna build your abs. Like, you know, if you have a blond that you're creating, you would put it in installed APS. If you have products, a list of products they have, you would put installed APS, that is, I mean, very core to Django and think of amps more in terms of components than APS, Right? So, like APS has taken on a new meaning like your mobile phone has a bunch of APS. It's not like that. It's more of like little pieces of the Greater Django project itself. Middleware, this is, I mean, there's a lot of things going on here, but it has to do with your requests and how requests are handled and also how securities handled and stuff like that. There's something will go over later. It's definitely Mawr oven advanced topic, but it's nice because it allows us to know that there's a lot of security features built in as we can see with some of the ones that are already there. Root your elcom. This is something will definitely cover for sure, too. This is how Django knows how to route any given your l you know, So, like, my server is running right now, So if I take a look at that running server, if I go to slash you know, whatever this is your else like. So those things are automatically routed by default by Django. Something that's really cool is a built in feature in there. The next thing is templates. Uh, you know, like Jingo renders out html templates. We're gonna go over this a lot, but basically like where to restore them. How are they rendered? How do they work? All that stuff we go over later, but it's essentially the HTML page, then gets rendered in Django. It's really cool. It's very useful, and it's definitely a common topic that will go over. The next thing is the Ws G. I application. This is how your server works. So the server goes through and uses this setting that's here. In some cases, you change it. Another case says you just leave it as is next thing databases jangle maps to databases. Really, Really well, So my scale postcards SQL and a few others as well, very easily maps to it. You just change your back end here where it's located and some of the other settings. You can go on the docks to see all of that. But by default, it has a secret, like three database already there. As you might see right there, that's pretty cool. Next thing is we have password validators. This just validates that passwords are good. Oh, or at least good to the current standards of what jingles found. We have some internationalization stuff. Must skip that for now and then. Finally, static files like where do you store your images? Your javascript on your CSS like, where do you store those things? Static files or something will absolutely talk about as well. But but Settings is kind of controlling all this, right? So it's a pretty fundamental to how all of our jangle project is running, and that's in our main configuration. I mean, that's it. That's it for setting. So I mean, we will talk and use these things a lot. Mawr. But what I did want to mention is one last thing. One actual practical thing is is that that database thing? So we have this error here and to run our database, we can run python managed up. Ey migrate. So what this does is it actually sinks our settings, whatever settings we have with our jangle project, and whatever APS we have, we're gonna go over this again for sure. But all this is is this right here? So we've got DB. That's egalite three. I just said, Hey, database, make sure you and jangle are cooked up and you're ready to start working. So if I actually changed this db two, I could run my great again. And what do you know? Django actually creates a brand new database for me Now this is pretty much only true with sequel light. If you had my swell or postcards SQL, you would have to create those databases themselves. But for us, we can just do whatever it like. As far as the database is concerned, I'm to go with the default of db dot Siegel light three. Go and delete that file. Now we have a better understanding of how settings work. Let's go ahead and create our first app. See in the next one. Make sure you subscribe to get everything on joint. Cfe dot com slash YouTube Could be doing so much more jangle stuff. See you next time. 8. 7 Built In Components: Now it's time to talk about one of the key components of Django, and that is APS. You should think of amps as components or pieces of the bigger jangle project. So the little bits and pieces that can hold a good amount of code, But they're not, like acts like on your mobile phone, right? So if you jump in to the code itself, we see that we already have some amps installed by default. Right? So this is also where you put third party APS as well as your own, right? Um, that's pretty cool. So let's take a look at the default or built in ones that we can look at. The very 1st 1 is called admin. And if I go into my project and type out admin, I'll see something like this, this Django administration. So I already have a user name and password, Phil, then because I used jingle all the time. But you probably won't see anything in here yet, you know, And actually, the question should be is like, how do I actually get in here? Well, I'm gonna go ahead and jump in my terminal notice that I have two terminals windows open. I have one that's running the server and then one that's just in the root of the Jingle project. Also known as What Managed a pious. You can see how I got there, right there. So there's a couple things that definitely need to happen. First of all, I want to make sure that I wouldn't run. Python managed up. He Why migrate? I see something like this, right? I have a few APS on there that are built in Those were there by default. Um, I don't want to see errors here eventually, but you definitely want Make sure that migrate is done because we want to create our first user. And then is python managed up here? Why create super User? That one command will allow us to create a user that has access to the and men. This is not a regular user. This is just the ultimate super user, right? So the user that you'll in abusing in my case, I'm gonna use the user name of cfe gonna leave a the email address empty, and then I'm gonna type my password. Now the password typing doesn't actually show up on. And also, when you're developing. When you're testing in your learning, you can use whatever passer of you want here. It doesn't have to be that secure. It has to be secure when you go live. When you build a real project, that's when you really want to be secure. So I've got my user name here, and my password, in my case, is just learn code, all lower case and I can actually log in to this admin. Okay, So what I did here was actually created user that's actually in the database. Jenko did all that stuff for me. I didn't have to do anything really cool. So that makes things super easy. Super user friendly. And that same action would happen regardless of the database. Pretty cool. So we actually just talked about the first portion or 1st 2 portions of built in components or built in installed APS, right? That's off an admin off. Is that user right? So I actually created that user. I created a super user, And then admin is what I just logged into, right? So I can click on users and Aiken Seymour about this off user. I can come in here and say Justin Mitchell, right and hello at team cfe dot com. And what do you know? That is my user now? I could save it. I can do all sorts of things in here inside of the admin. We'll talk more about the admin in the future, but it's really cool. I can also delete users. I can go back and create new ones. You know, there's a lot of built in things about the M in that it really nice. But this is all about the apse, right? The next few things. Well, they're not relevant to us just yet. So what we want to talk about is actually creating our own custom maps. So we've done some of the basics. Now let's do a custom app of our own and have it inside of the admin. When I say custom app, I mean our own data. It's our own structure of data. Let's see what that looks like in the next one. Make sure you subscribe to get everything gonna be doing so much more on Jango. So joins. If he dot com slash YouTube. Thanks for watching. See you next time 9. 8 Your First App Component: that last one. We saw some of the built in features of Jang Go's project, right. We saw some of these components, also known as APS, that allowed us to have a user and an admin. Those two things are phenomenal. They're really easy to use and very user friendly. But what we want to do, and the purpose of using Django is to build our own APS, our own components. Now let's not confuse apse with, like, what's on your mobile phone? It's much more about just little pieces of this greater whole that is your Web application . So let's go ahead and jump into the root of your general project. I'm going to use that term a lot. The roots of the Jingle Project is referring to manage up high. So where Managed up High is assuming that you have your virtual environment activated and that's where you want to be? Whenever I say roots of the jangle project, I'm gonna assume that that's the case. So I'm navigated there and of course, this is where it's actually located on. My system might be a little different for you. If you don't want to get there, just go back a few videos in this Siri's. It's linked below. Make sure that you watch some of the basic stuff there. Anyways, let's go ahead and create our own custom app. So maybe you python ends up here, why start app and then whatever one wanted name the app. Now I'm gonna go ahead and name it products, and you could do the same thing by pressing up and renaming it to Blawg. Or you could do it too profiles. Or you could do it. Teoh cart, Right, So I can name it all sorts of things, right? And honestly, those four names actually fit with, like, an e commerce project, right? And if we look back into the code itself, you see that? Hey, I've now have all of this new code inside of the name of those different APs that I just created. Now, this does show us something that we could work towards, like we get totally build an entire project that does all of these things. But also what it should illustrate to you is that each one of these APs should do one thing and one thing really well, like the products app should really just do product related things, not cart related things that should be separate. That should be in its own cart app. Ah, and we'll get to what that means later. But the idea here is that your app should be pretty narrow and focus once it starts to get wide. That's when you start to bring it into another app. And as you see, it's really, really easy to create nap. So there's really no reason to not do it other than perhaps laziness. And I want to avoid that in the long run for you. That's why I'm telling you about it now. Okay, so I want to show you how to use an app in the way of storing data. Okay. The abs are really good for storing data and mapping. What data you want to store to your database. So that means I'm gonna go ahead and delete some of these other acts that I just created because we just simply don't need them because we're still learning. Right? So I delayed it, deleted these other ones, and now I only have products. So opening up models up. I will talk about the other files later, but for now, just models. DuPuy. I want to store a product. Right. So I want my back end to have memory off a product that I created. How do I do that? Well, I write a class called Product, and in this class, I wanted to have various attributes to it. Right. So I want to say that it has a title. I want to say that it has a description and, you know, perhaps that's it for now. Maybe I just want titled The Scripture. Maybe I want price to. Okay, so that's a title description and Price. Now, I want these mapped to the database. So how do you actually do that? Well, in Django, it's actually fairly straightforward. We use something called model fields so models dot we just type out miles dot It's already imported by default. And I'm gonna just use text field for each one of these. For now, we'll talk about more advanced fields later, but let's just use text field on each one, okay? And then my product itself, the actual class that I'm using here I needed to inherit from the default Django class of model. Okay, so this means that it's gonna get a lot of features that we absolutely need to make this work that we just won't go into just yet. That's getting more advanced. But here is a very, very simple model called Product. This will map to the database, and we'll see that in just a moment. So since I created a model stop I and I created this app. I need to add the app in the settings installed APS and that's really simple. I just put my own here and just write out products. The name of the app that I created. Right? So that's the folder here. So I've got products I haven't sold amps. Should go ahead and put a comma after it. And now what do I do? I, of course, make sure that I saved my setting. Stop I and models up high. And now what? I can run is this thing called make migration? So python managed up. He Why make migrations and then python man shop? Ey, my great. Okay, So those commands you're definitely going along. Remember soap I thought managed up. He Why make migrations And then python managed up here. Why migrate? Okay, so the first time I did it, it made some changes. The second time I did it, it did do anything, right? So I want to run these in in conjunction with each other every single time I change models that pot. So let's go ahead and add in another field here, and I'm just going to say active, okay? Lets just do summary instead of description. Right. So we got description and summary, those two might go hand in. And But now I've made a change to the model. I saved it. And now I want to run make migrations again. It's gonna ask me for basically a default, and I'm just gonna go ahead and say to for now, we'll get into that later. But I'm gonna go ahead and say default and say, This is cool, Exclamation mark. OK, so I run make migrations again. And then I wore on my great again. Now why the heck that I show you all that well, the main reason is to remember that we always run, run, make migrations and my great when we make changes to models up high anytime, anyplace, anywhere, that is super super important. Cool. So we've got this model now, and I want to take a look at this model inside of the admin. So I do here is going to end up high and do from dot models import product. So this is a what's called a relative import. It's importing the product class from the models DuPuy, and it's relative because And men DuPuy and models up high are on the same, you know, directory. They're in the same module, so I can actually do that Relative import. And all I do here is an men dot sight register and product. We say that and with our servers still running, I go back into my project. My Django admin. I now see this new thing here called products, and I can add a new product, new description, some price and the summary. Hey, I've got my default in there and saying, This is cool. This is awesome. An exclamation mark had saved, and there we go. We've now created a new product and it saved in the database. And that's the That's really the core and the basics of it all right, there a basic model saved in the database. Now I could use this over and over again to save all kinds of data in the database. This is not a great model. I will say that like this is pretty limited in scope on how it is. We'll get into more advanced features of that later. But for now, that's pretty cool. That's all we have to do for a model. That's it. So we still need to do is see how to do this in the python shell that is actually just using python commands to save some stuff that something will do in the next one. So make sure you subscribe to get everything, Otherwise we will see you next time. 10. 9 Create Product Objects in the Python Shell: Now what we want to do is just use the python shell to create new products. So I used the admin before. Now we want to use the python shell. So to do this, I want to make sure that I'm in the root of my jangle project. You know, we're managed up. I is, and I'm gonna run Python matters up. How p y shell. So when you do manage DePuy shell, that means that all of the Django projects stuff will work inside of a python interpreter. So I had enter. It looks like a normal python interpreter. But it's not because I can do from products the models import product, right? So I can do these kinds of imports like an import classes, just like we sort of did in the admit, right? I did a relative import in the admin. This is closer to an absolute import inside of the show. So I go ahead and enter here, and I can do product that objects that all this is a built in jingle command will get over this sometime in the future. But for now, if I enter, I seeing that there's only one thing in here, right? There's only one item. And that's because I only saved one in the video. That's all I did. You might have done it otherwise. You might see a lot more there, and that's cool. If you have, that means that you're experimenting. That's awesome. But for me, what I want to see is Maura. I want to actually create them right here in the shell. I wanna have the ability to do those commands. So it's simple. We just do product of objects that create. And then we want to create new ones inside of this command itself. Inside of create itself again. This is built into Django. This is default by Django standards Front. So, in models, we look at the things that are required for this product. We have four fields that are required so I can pass those four fields in here as title new products to write and description as another one right price being, you know, whatever price and then finally summary being sweet. OK, notice each one. Each argument in here is a string itself, right? So I did that on purpose. They're all strings, Andi. That's how I can create a new product ahead Inter. It creates it. I could press up and inter again. It's creating new A new products. Right? And if I did that product dot objects that all again, what I see here is a list also known as query set again. We'll get into that later, but it shows us all of those new products created. Assuming we still have our server running, which I do, you can close it out with control. See? But if I just go ahead and make sure that my servers running, I can come back into my admin. Right. So you log in here. I mean, you see this, you gonna products? What do you know? I've got all those products in there. So that's another way to actually create things in your database just by using the command instead of clicking through and go into the form and doing all that pretty pretty awesome. So we still have a lot more to do. Obviously we want to see this in another form. But before I jump into another form, I want to see, actually, how doe I realistically create a model that isn't so convoluted like I shouldn't actually pass a string for price. It should be in the actual decimal, right, And perhaps the title shouldn't be this long text area. Maybe it should just be one short field. So there's definitely things that I need to change to this product model to make it better , that something will do in the next one by adding new fields, stay with him and make sure you subscribe to get everything because I want to be doing a lot more jingle stuff on joint. Tiffy dot com slash youtube Thanks for watching. 11. 10 New Model Fields: So now what I'm gonna do is actually start over on these models. I really don't like what I did here. So in order for me to start over, I can delete all the files in the Migrations folder and just leave in it in there. That's fine. Also, delete pie cash if you have it there. And then also, I'm gonna delete my sequelae database. Okay, So back into my models, I want to change these fields for something different. What I just did was something you'll do often while you're learning right, you'll delete those migrations and then you'll delete that data base. Sure, you might lose some data, but that's no big deal. That's part of the learning process on, and that's actually really, really important to do so with these fields. I want to transition them into being something more realistic to what they are. So let's go ahead and look at the reference in the docks themselves. So jangle project dot com Look for the model fields field types. You can just do a quick Google search for this, and you'll see the reference for all the different field types and what we actually used so far was just one of them. As you see, I'm scrolling. There's a lot of them. So I used just this text field. That's one of the very many that we have in here. So let's go ahead and use a few different ones. First of all, the title field should be limited to how long it is. So I'm gonna change it to HR Field and I'll add in something called Max length and I'll make it at most 120 characters. That's still a fairly long title, but that's what I wanted to be. And when you use a char field, you have to use max length, so max length equals two required. Well, um, I'm gonna leave it out for just a moment and save it, and now I'm gonna go and try and run my immigrations, writes Ogle Python managed up you. Why make migrations? I get an air, says Char. Field must define a max length attributes I just mentioned that was required, but luckily Django will tell us if something's required. Once I save that some of my errors go away, right description takes field. That actually makes sense. But what if our product. We don't necessarily want to have a description. Then I can say blank equals two. True. And I can also say no legal. Is that true? We'll explain these differences between those two in just a moment. Price? Well, sure price field Be well, should be a text field. Or should it be? Well, let's see if there's a decimal field. Hey, what do you know? There's a decimal field and afloat field on a stick with decimal field, and this is where the docks come in and make things a lot easier for us. This will show us what's required. Okay, so what's in here is required, so these two things are required right here, whereas if we go down a little bit, it says duration field. Not going to use that, But there's nothing in there that's required. We've got email field that has a requirement, but it's already in there. It's already built in, so this will take some time getting used to. But that's essentially what's going on with the docks, right? But it also shows you inside of the docks that it has those required arguments. In other words, let's go back in here and change this. Just a decimal field. Let's save it. I save it. It's gonna run these errors, right? It shows me these errors. It tells me exactly what it is, but so does the docks. Right. So the doc says back digits and decimal places. So let's go ahead and add those for a product. Decimal place is going to be to like, you know, it's not gonna be three decimal places. You don't have three decimal places of sense. And then max digits, as in the number of digits this will allow, like 1000 digits is actually a lot for a decimal number, right? So just do that in your mouth and your head. What's 10,000 digits in dollars? That's that's a lot right. Cool. So I now have a little bit more robust fields. Summary. I can leave it as a text field as well. Maybe I want summary to be in their by default. Maybe I wanted for sure be in there, but I'm gonna go ahead and get rid of that default. I'll explain the default thing in just a moment, most likely in the next video. But I will explain what's going on with that? You're shortly. Okay, so I save this. And now what I need to do is actually run my migrations again. Remember, every time you change miles up high, you have to run. Python managed up you? Why make migrations and then python Manish up! Ey, migrate! Now! I deleted the data base. So that also means that I need to run, create super user all over again. And I'm gonna leave it in as CFE type in that password of Lauren code and that we go. My server is still running through all of that. My server still running a refreshing here on the Django admin. I still have to go back in, log in, and I see that I actually don't have any products anymore. But if I go to add a product, the layout of it has already changed. The title can't be some long title. I can say new product and description. Aiken. Right? Stuff if I wanted to. Price has now a number item in here, so I can actually write numbers 2099 I can add a summary some new summary exclamation mark I had saved. What do you know, not a whole lot different. Okay with any mind, Let's just see it in the shell to so Python managed up here. Why Shell? And we'll just go ahead and do from products. Don Models Import product. This is the exact same thing. It's just going to the location. So where that product is, we can enter product of objects that create title eagles to newer title price. Now we can put actual a decimal number in here. No string. So 2 39 99 then summary. Now a string awesome sauce is awesome. We had enter. It looks like it created it. No problems. We go back in our products. In the admin still running. We see that we have a new product in here. All of that stuff was saved. So it's bringing those two things into conjunction with each other. So we're already starting to save stuff. We already hopefully see how easy it is to map data in Django. There's definitely a lot more stuff that we can go over here and we will so make sure you subscribe to get everything will see you next time 12. 11 Change a Model: this one. I'm gonna make a new change to our model without deleting the data base. So or the migrations for that matter. So that means that we want to actually know a little bit more about what's going on with both of those things. So when we run, make migrations and migrate, it sinks the database with our model. But if we add a field to our model, the database doesn't know about that field. So we have to make sure it knows and make migrations and migrate often allows that to happen, but it also introduces a whole set of, like, question marks like the field that is being added into the database. What about all of the previous things were that were in that database? So let's let's see what I mean. Right. So if I jump into the admin, I see that I have these four fields here, right? But if in my models, I decided to say featured field right and I wanted to use, let's say models dot boolean field. Okay, so I'm now trying to add this Boolean field, but this particular objects, this saved object has no idea about this field right all future ones might, but this particular one doesn't. So if I went ahead and save this and went to Python managed up here, why make migrations right? So you do that by default, you get this error says a non knowable field. I did mention that I would say what these things meant. That's what this is, right? So if I said no equals the true here, this air would not have happened because it would have said all of those old values just leave them empty in the database. They could be empty. That's fine. No worries. But I don't want to do that. Instead, what I want to do is I actually want a value for those old items in the database. It could be one item. It could be 10,000. Jingle actually doesn't know how many items that are in the database that haven't been sent yet again. It could be zero items. Now they're what jingles going off of is this migrations folder here. It's going off of this initial value notice this initial one right here, it's checking against what's in here for this model. It saying, Hey, the new feel that you're adding this new field right here isn't in this initial description , it's being added to it. So we're gonna assume that the database needs to know about what's happening there. So we need to tell the database what default things we should do. Now you can do that. There's several actions you can do here, right in the code. You could say no legal is the true or you get set a default. Or did you? Both right. So you could have a default in there, right? So no legal is that true or default value? In this case since is a Boolean field, which means either true or false, you could say default. He goes to true. So you could absolutely hard, right? Right. Those and the code Or you get this error and you can provide a one off default for all of the previous things that were in there. And I just say one and literally say, True, Right? So this means that everything that's in the database, it's gonna go through all of them that's already currently in the database, and it's going to save those things as having a field of featured with the value of true, as opposed to false I had. True. There we go. Now that make migrations. What that ended up doing is it developed a file for us? A migrations file and this is it right here. The database still doesn't actually know about anything. All right? He doesn't know about this field Steel. Still, because we didn't run Migrate, so I can actually come back into my jingle project. I get this thing. No such column. He goes to that, right? So that's an error that you might see quite often. I know you will, actually, because people forget about this. So we want to run Python managed up people. I my great because we always run, make migrations and then migrate. And then there we go. Now we have a change actually made. And as we see, all of our old objects that are stored in the database have featured as true, all of the new ones. There is no default in there. Cool. So that's awesome. What that also means is then, in my models, this summary. What if I wanted that to not be required anymore? Well, I can say blank equals two True, but let's go ahead and say Knoll equals two false and see what happens here. What did I do? It made changes to the models. So what do I have to do? I have to run Python managed up you? Why make migrations and then Python and Stumpy? Why, my great cool. So now that I've got that, I come back in here, I refresh summary is no longer bold, and I can now leave it empty in the database. So if I change blank equals two falls, save it again. Run, make migrations and again migrate. I could go back in here and refresh and notice. Now it says that it's bold. So if I had saving continue, it'll give me this field is required. So all this is doing is blank has to do with how the field is rendered null has to do with the database. So if the blank is false meaning that it's required, then it will render as required. That has nothing to do with the database, as we see with these two things here. But no being true or false means that the database can be no or empty in the database. Cool. So that is changing some items in the model. This used to be very, very hard, and even if you feel like it's a little challenging, doing this several times breaking it, the leading, the migrations full, the migrations in here already deleting the data base, running all of that stuff all over again. Doing that several times will really get you comfortable with models. I promise you it will and then try to explain it to somebody tried to explain to a friend make a video, try to explain all of the things that I just did. Go back and reference. Um, read the docks. Whatever you need to do toe really better understand how this basic stuff of models works. So there's definitely more stuff we can do in models, but I'm going to kind of leave it as is. There's other things that jingle does really well that we want to start talking about, and that has to do with views and your l's. So make sure you subscribe to get everything. Otherwise, I'll see you next time 13. 12 Default Homepage to Custom Homepage: So we want to change this default home page to our own custom home page. How do we go about doing that? What? We do this by creating a class or a function based view. So a view means that it's gonna handle our request. We'll get into all of that later. But right now, what we want to do is just illustrate the purpose that we're doing. So I created a new app called Pages. I added that pages to settings installed apps. You want to get in the habit of doing that whenever you create new amps, But you should know how to do that by now. That's why I didn't do it. Okay, So inside of used up high, we see that there's nothing in here. This is where gonna be creating all sorts of things for your pages. Think of use as a place that handle your various web pages. Just think of it that way. Um, and we're gonna do this using either functions or classes written in python. So the 1st 1 I'm gonna do is just a home page, and basically it's just very simple function. It's home. You give it a name whatever name you want, I would probably say that mostly you'll probably add view to it like you're not gonna call it home page without view, right? So I'll get into view and all that stuff later. But let's just call it home view, okay? And then it's going to return something. We wanted to return some HTML. So let's say, for instance, I wanted to say H one. Hello world. Now, if you're familiar with Python, you'll know that this is just a very simple python function. It's actually nothing in here is related to Django, so we need to change it to be related to Django. Now, this string right here has HTML in it. This is a string of HTML code. It's not actually HTML code. We'll talk about that later, but this is definitely useful if we want to just have one string of HTML code for some reason. But how do you actually make this functional? To do that we use from django dot http import http response and a CP response. We can just call with that string of HTML code just like that, we're really close to making this an actual functional view. or functional page. So I'm gonna go ahead and add ARDS and keyword arcs in here. If you're not familiar with arts and cured our eggs, I definitely recommend that. You look that up on python. This is Python specific. If you're not, don't worry about it. It's okay. You can look it up. But for now, we could just play around this. Okay, so I've now created a function. All this function does is returned back Some HTML code that says hello world, but it returns back in something called on a CB response. Okay, so let's actually make this work. I'm gonna save it. And I'm gonna hope for the best refresh here. It doesn't work. Well, what if I try different page like ABC? That also doesn't work. Says Page not found. And it says something about you. RL's Well, this is where we actually rap in our your else. This is where your else comes in. And when we went over the settings we did mentioned about the Urals right here. So you're Els are in. Try geo dot your l's or otherwise the settings module or the settings folder inside of your l's. This configuration folder has settings and Urals in there, and it has one that we've already been using, which is an men. But that's the only path it actually has. So now what I want to do is actually import that view to work with my Urals. Now you already have some of this stuff in here, right? So this is actually written for us so I can go off of what they have written in a similar fashion by saying from pages right so pages would take place of my app import views. And then I can quite literally add this Ural pattern like it says in the code or in the comments there. And I didn't call it home, but it said it called it home view. Right, so there we go. I'm just going off of what they've given me home view and home view. We say that and let's go back to our home page. Hello, World Cool. It's actually working. There's a couple of things that I don't particularly think is a good idea with how this is written. Is that what if I wanted to use the product views then and I went from products import views. Things get confusing. So don't do it this way. Go Views, import, home view, import the actual view that you want to use and then just put that down there. Okay, so we say that. And when you know the same exact response Wow, that's fairly easy to work with. Not a whole lot going on here. Now there's definitely Mawr that when you talk about within, views themselves as well as you are all patterns. So stay with us. Make sure you subscribe to get everything, and we'll see you next time. 14. 13 URL Routing and Requests: Let's talk about your L patterns and how this portion works. In conjunction with our view, you're all patterns. You noticed that I had this empty string that's in this path. So if I actually copy this and pace it down here and say home and put a slash at the very end of it and save it, I can actually go into home. And that same thing will come out where if I use a different page like ABC that won't show up. And the page not found is because we haven't routed a path to that view, right? So let's say, for instance, I want to call this contact and you know we can. We can actually ignore this name stuff for now. But if I want to call this contact, sure, it could actually go to that same home page view. But really, you would probably want to have another one called Contact View and again we'll do ours and cure guards and then we'll return. Http response. And then we'll just say h one and contact page and close off that H one tag and then we would import that as well. Just like that and there we go. So that would give us that contact page pretty useful. Now what's actually happening inside of this path? Right. So the your L when you go to a jingle page, right? So we go to a jingle page right here, and Django knows that something is being requested. That's different. So the first thing that's being requested is just this home page. And then if I go to another page like contact, that's a new request, and it's getting that idea, whatever's in there and Django smart enough to know Hey, that you RL's being requested. Look in this, your else dot pie or that main configuration that we have set up by default is gonna look for that your l and then with a matching your l. It's gonna look for a view that is made to handle that. Your l right. So we already had those views that were made to handle it. You know, we can have as many views as we want. It's completely up to you, and in fact, your project might have a lot of use rights, like social about whatever. Like all of those things you can absolutely start playing around with, and I recommend that you do. I recommend that you take this and play around in the sense that hey, how does this actually work? And how can I make all of my own pages Now? There is a critical thing that is missing here, but I'm not gonna get into that just yet. What I will say is that remember when we go to a your l were requesting something from that you're well, it's kind of like knocking on the front door of somebody's address. I mean, it's not a whole lot different in that, except this is digital. Were going there, were knocking. We're asking for something or were requesting something. In this case, we're requesting a Web page that's at that. You're well. So what's actually happening is on these views. We have ours and cured ours to just capture everything. So if I actually printed out, let's go ahead and print out arts and cured arcs and go back to that home page and look into our terminal we seen here are the things that are being passed. We have this ws g I request. Um, I'm requesting something, so that means that it actually is an argument that's coming through here. I can say request and then ours. Keyword arts. Right, so each one has requests coming in here by default. So if I go back, refresh now my arms and cured ours are empty, and my request is absolutely in there so I can print out that request and we can see it. So what is this request do exactly? Well, there's a lot of even things that it can do. But one of them that's important for us is request user. So this is where that authentication stuff comes in. This is where logging and your users come in. But this is also how our views can access that. So if I refresh in here, I can see that Hey, my CFE user is logged in. If I opened up an incognito window or just another browser or logged out, I would see that it's an anonymous user. It's not somebody that's actually law again. So both things are actually valid, and they are really good to know, because then I can do all sorts of stuff with this request user. Now that's a little bit more advanced. So before we get there. I want to make this basic Web page or these basic application work better for us, like so far we have this HCV response, and it's rendering honest html, and that's great and all. But, you know, you might be familiar with HTML. Hopefully you have some background. Html just having a Chuan one being rendered. That's not very good. Like noticed. Like my head is empty. It's just age one. That's the only thing that's coming back. So now is actually a really good time to stop talk about the Django template ing engine so we can override this issue response with a another sort of thing called Django template ing that something will do in the next one. So make sure you subscribe to get everything, otherwise, we'll see you next time. 15. 14 Django Templates: quick recap. We go to a Eurail. It's like were knocking on someone's door requesting something The server or Jingo returns a response to us. That is the process that always happens with correctly configured jingle projects. Now, when this happens, Django recognizes what your L is being requested. Or you know that Web address is being requested. It breaks it apart, figures out what function is going to respond to it, right? And with that function, we want to respond with some sort of http response. The basic default one. Is this just a TV response and we can pass in, whether it's html itself or just a string, it doesn't matter. We can return 80 response, and we could even return it being nothing like it could be absolutely empty in here, and it would still work. So this is still a valid page. But what we want to do is, instead of writing in strings here, we want to use some built in shortcuts that Django has, and you may have noticed that shortcuts dot import render was in their by default. So when you create a new app that's in there, Okay, so I want to actually be able to use this itself. So instead of returning HCV response, what I want to do is return render, and it's gonna take in a few arguments. One is request the request argument that's being passed. One is gonna be a template name, Let's say, for instance, home dot html and the last one is context. Or for now, we'll just do an empty dictionary. We will talk about context later. So that's how you would be able to return some sort of HD email template or HTML document either way. So how do you actually find? How do you actually find this like, where is that located? If I say this and refreshing here, I get this error template does not exist. Well, it certainly doesn't. We haven't actually created it right, and it's looking for it throughout our site, but it doesn't actually know where to go. So when you create a location for it and that's what we'll do now, inside of our SRC folder in the root of the General Project, we're gonna make a new folder in here called templates. This folder is gonna hold all of our templates as we see fit so inside of there, we're gonna go ahead and do home die. HTML making that new file noticed templates is online with pages, products and that Django configuration folder as well as managed up I So I put a home down html in there, and I'm just a h one. Hello, world. Close off H one and then do a P tag. This is a template. Close off that, Peter. Okay, so we say that it seems like things are looking good. We made a templates folder home. Daddy Chanels in there. We want to render it out. Save it. Let's go back. We re running. Template does not exist still. Well, this because we haven't actually set up Django to know where these templates are. I mean, I did name it templates, but that's an arbitrary name. That directory is an arbitrary name. It is a best practice name, but it is still I just made it up. Okay, So in settings that pie, then remember how I said we were gonna come back to templates? If you watch that video, that's this. So we want to actually put in sign of our ders or are directories where this directory location is now. I could hard coat it to your path to templates. Let's do that. So let's go ahead and say pwd here is my normal project here, right? So that's to the root of the jangle project. All I need to do is add templates here, as in the name of that folder it saved and refreshing here. What do you know? Our templates are looking good. They're actually in there. But if I sent this to you on your computer, it's not gonna work unless your user is CFE. So what we need to do is pretty much copy what we did here. That's actually fairly straightforward. But with some minor exceptions, basically, I want to make it os independent. I want to make it allow it for me to send this code to you and it still work. So it's B. O s path that joined Baster and then templates. So I'm just joining that base directory where Managed Up High is with the templates directory that I just created, refreshing there it's still working that templates still showing up. So what this allows me to do then, is to make multiple pages like about dot html contact dot html and so on. And then I can just quite literally bring in all this code here and say contact and about And then, in my view, just like I did on home view. I could just reach change these about and contact to content to the creek name. So about 10 HTML and contact that each Mel, I say that refresh. Everything's looking okay to go toe about Hey, looking cool. So it's rendering HTML notice that it doesn't have dot html here, right? So it's not actually like rendering out that raw HTML file. It's it's actually going through Django and Jang Go's rendering out that each to melt. That's pretty cool. So if you've worked in Web design before, you'll know that that dot html being gone makes thes Urals so much cleaner. And it's it's a lot easier to navigate and share, and it also makes it look more advance, in my opinion, okay, so now we have a way to render out html documents, but you might see that there is a lot of repetitive nous that could happen in these documents, so we need to talk about some basic inheritance stuff with Django templates that something will do in the next one. So make sure you subscribe to get everything joins. If he dot com slash YouTube My name is Justin. I'm really looking forward to seeing you again next time. 16. 15 Django Templating Engine Basics: time to talk a little bit more about the Django Tim plating engine that we are actually already using, but we want to learn more about it. So if I look at something like this, we've got to curly brackets request dot user to more curly brackets. This is something that Django does by default, and it actually will render out that relative information. So we actually refresh in our home page, and I see that CFE user that's my user name. And if I open an incognito window, I see anonymous user that's expected. So that's part of what's built into the template ing engine we can. We can use the user model in it, right, and I can also say DOT is authenticated, as in is logged in and it'll say, true or false, depending on if they're logged in right. So there's definitely a lot of things we can do there, which we'll talk about a lot mawr soon, so make sure you subscribe to get everything. But what you also should notice is that our views are rendered out three different HTML pages that actually might share attributes across each one. Let's say, like a navigation bar or more importantly, some metadata like, you know, if we inspect the element here and we saw, like, you know, the title tag or the description tag or other metadata that we might need to implement in here or some CSS data, I mean, there's a lot of things that each one of those pages would actually be able to use. So this is where template inheritance comes in and where it's really useful for us. So what I want to do is create a route page, the page that all these other pages are gonna borrow from. So go ahead and call it based on HTML, based on your HTML is convention. That's how you're gonna want to call it so and you're gonna want to make it a actual HTML document. So, doc, type html and then html close off html and then body and body, and then we want to do ahead tank here. And in this case, just to show that this is actually working, we'll go ahead and put a title tang and say coding for entrepreneurs is doing Try Django. Hey, Okay, not really that clever, but it is something. So we've got our head tag. Our body tag. We have some html in here. We've got some stuff. And now what? What I want to do? Well, each one of these pages. I want to also have all of this stuff. The exact same stuff. How do I do that? Well, what I can do is come inside of this base. That html curly brackets, percent block content percent, curly brackets, percent curly rackets, block or curly brackets, percent block for brother in block. So this syntax might look a little weird to you, but basically, it's saying like, Hey, whatever's in between here, whatever comes in here, let's just replace it. So replace me. Okay, so I'm saying use these blocks these exact same blocks on any given page. So let's say, for instance, this. I'm gonna go ahead and cut this out and say just like that. So I've got a block here, and that's the content that's gonna be It's essentially gonna be copied here, brought in right here. And all I have to do to make all that happen is extends based on HTML. Again, these curly brackets percent signs. It definitely can get a little confusing. So make sure that you are testing this on your own to make sure it's working. Okay, so I've got now a new thing for home. Done. HTML. Back in a home looking my title tag. If I refresh and now changed to coding fresh mirrors is doing try Django so that title tag actually did change. And it's based off of this, based on HTML. So let's do that again on about inside of about curly brackets, percent extends, they start html. Okay, so this is now pulling from based on html. If I saved it just like that and went into that about Paige, it just has replaced me. Ah, so it's rendering out this default page. That's all it's doing. So in order for me to actually replace whatever block is in there, I have to use it. I do block content, curly brackets. So curly brackets percent block. That is the jangle related stuff. Content is a variable that I created, and then we do in block to close it off. You might also see in block content. That's okay too. So we say that and we refresh and that is now changed. The title has changed So we now have template inheritance. And then, of course, that final thing would be true with contact as well. So again we do extends based on HTML and then block content and then finally and block just like that. So the content that I'm actually using is right here. But now each one of those pages is uniform in Django. This makes a lot more sense when I use something like a nab our So let's just go ahead and I'm just going to use H one or H one tag just to show this is a knave are not to get into too much of the HTML here. But there's a nab our It's on my about page. It's on my contact page. It's on my home page. It's definitely the same thing all across. And that's how this inheritance actually works on. Guy will say that, you know, if you change this to block content, um, main or something like that, and I say that every single one of those pages would change because I'm not actually rendering or I'm not replacing the correct block and each one of those pages. Instead, I'm replacing nothing so they just don't do anything, which is really cool. That actually makes things a lot easier for us in the long run. But this might be a little tricky to get your head around on how this actually works. So what I do also recommend that you pause the video or stop now and change it to another, uh, content area and do several blocks in there to see how all of that works to make sure that you have a better understanding of how this inheritance is because it definitely is a little bit trickier than just rendering out in HTML page blank html page. But it is important, so we will still dio more stuff on genuine templates. But that's it for now. If you have any questions, please let me know. Otherwise we'll see you next time. 17. 16 Include Template Tag: So what inheritance allows us to do is to remove redundant code that we need in many places what we're gonna talk about this one is the include template tag. So we can do this more of the same just on a little bit more of a different level. So what I mean by include is sometimes you'll have a knave bar or a navigation bar that you might wanna have in multiple places. So to allow this to be showing up in multiple places or allowing me to edit just that now bar not having to go into the base every time I can actually come into my templates. And let's just make a brand new one called the knave bar dot html And this itself will just be the NAM bars. I have anything else. It's not really gonna be doing template inheritance. Instead, what we're gonna do is have a knave bar of some kind, right? So I just wrote in, have and then did a u l. And here and, you know, did some knave are related things I'm not gonna make a real nab are really But let's go ahead and just a brand. So We've got a simple damn bar with just brand analysts say contact and about. Okay, so we saved that and go back into base dot html. Let's get rid of this. This is an Anbar thing, right? So we just want to go ahead and use. Include, So again, curly brackets, percent sign, include and then the path or where that that actual html document lives. It will say simply, just knave. Bardot html, you know. So this is very, very similar. Not very similar. It is identical to our views that we created, right. So whatever we put here would be very the very thing we put here. So that same path, we'll get into that mawr later. But this is how we can include an external template into any of our templates. So we say that and refresh. And there we go. So we now have a nab are granted. It's not a very good one, but it isn't. And bar that will show up on all of our pages. Cool. So this the reason you do this is once your project gets complicated, Once you have like, Ah, big old project, you're gonna want to use this include because then, if I need to make a slight change to my nah bar, I could just jump in to nab our dot html. Pretty cool, very useful. So now what we want to do is actually talk about context. So, like, how do I actually use things beyond just that request user that we've seen before? So I want to actually add in some new data that's coming from other places, so I'm not hard coating it every time, but rather it's coming from somewhere else. That's what we'll do in the next one. So make sure you subscribe to get everything and thanks for watching. See you next time. 18. 17 Rendering Context in a Template: so the main purpose to use templates and Django is not so much for inheritance, including and granted. It does all things well, and you could make a pretty cool website. But what we really want is data from the bag, and we want data from the database that's being rendered out inside of one of these templates. We want to see that we kind of teased it a little bit with that request that user a few videos ago. But But I want to actually go a little bit further and say, You know, maybe I won't change the title of the page or I want to have different content in their based off of a different user. Now we're gonna work towards that. But the initial thing that we have to understand is what's called template context. That's what we're gonna cover in this one. So let's go ahead and jump into our views, and I leaved are left out this dictionary, right? So it's an empty dictionary right now, and that's actually where we can pass in context to our template, because what Jingo does is it takes our template, and the template context mashes those things together, renders it and then sins bank just raw HTML to the browser. We'll see what that means in just a second, but that's essentially what's happening. It takes the template. It takes some context. Mashers those things together, turns it into regular HTML, and then the user sees that. Okay, so what do I mean by context? Exactly what context could be any data type, Right? So let's go in our let's say are about view, right? So let's make a dictionary just a standard old Python dictionary and say my underscore context equal. Sue a dictionary, right? So if I grab my context here and put it in and replace that empty one week of the same stuff going on right now Okay, so I said that I wanted to change my title. So let's go ahead and just have some arbitrary key, meaning I'm making up this key and we'll say my text and we'll say, this is about me for us. Whatever. Okay, so this is now my context, All of this stuff is arbitrary. I made it all up, as you probably could guess, but we've got a key value pair here, so it's just a standard dictionary with a key as a string. That's a standard thing in Python. And then I can use a string as the value. Or I could use something else like number. Or rather, let's say something more like my number. And we could dio 123 right so I can use strings. I can use numbers. I can use other dictionaries, which all that you play around with the data types yourself. That's not really what's important. What's important is how we actually bring this into context into this about Paige. So since I've got this context, I have this dictionary in here. I've created a custom one for myself. I want to bring this into that template and take a look at that template. So I have two different key items here. So let's go into about about that. Each email. And let's also make sure our project is open on that page as well. So we'll go to about here and there we go sort in that project that's rendering out this template and how actually render out any sort of item that I passed in as context I would normally normally bring in my you know, something like a P tag. So general html here and then whatever context I want to use. So we used these curly brackets twice. So curly bracket, curly bracket or braces space. We'll put something in the middle there and the curly bracket curly bracket, and then we're done. Okay, So what is it that I need to go in the middle here? So this is called a context of variable, right? So context variable. Meaning, what variables did we pass into our context? And what can we use we already seen? Request that users. So that is something that's already built in there. Right? But how do I actually access the things that I passed through? Well, hopefully you have some intuition about this. If not, it's OK, you'll develop it. We have these two variables. So our keys become the template variables that are inside of any given context that we've has. So now what I've done, as I said, Hey, Django, we've got this about die html page and then we have this context. I want you to mash those things together and produce something that I wouldn't expect. So in that context, back in the nature, Mel, We can say my underscore text and that right there should render out this key and that value, right? So it's taking this key. That's what we used. And it's gonna render out this value. We could do that same thing with my number. So back into about we can say my text underscore or rather, comma curly brackets, curly brackets or braces opening close sublime takes does it for us. But you know, if you're using a different text editor, you want to make sure that it's done this way and they will say my number. We say that we go back into our about page. We refresh it. Oh, we're not actually getting anything. Why is that? Well, it's actually possible that I didn't save my about view context, so I need to make sure that always saving everything right? So, I mean, if it's empty, that means it didn't pass through. It means that the context was empty. It means that this stuff wasn't actually set. It's a pretty cool error to see right off the bat. It's not really an error. It's actually a feature. But if we refreshing here now, we see that data coming through. So that added simplest form is template context, and it's again just a really quick thing. We make a dictionary of the template context that we want pass, and then we pass that. Now one of the things that you might be wondering is saying like my list and how do we actually work through with a list? So if I said 123 you know, in some numbers or strings, it actually doesn't matter. How do I actually look at this list? Right? So I come in here and I say, my list, I render out that list Well, that's nice and all, but that's not how I want to see it. I want to see it in maybe an actual HTML list item, right? Perhaps that would be a little bit better. So this gets a little bit more complex, and that gives us the discussion of what's coming next. So make sure you subscribe to get everything. Thanks for watching. See you guys next time 19. 18 For Loop in a Template: We left it off where we passed in some template context off a list, and we want to see that actually rendered out. We want to actually look at what that list is in a different format, right? So the foreman I want to see is something like this. So let's do A UL inside of our template and do a list item. So item one and then close off that list item list item item to Okay, So how would we go about doing this? There's there's a couple ways that you could do it, but the main way, let me say a couple of ways that you could do it one of the wrong way. Let's do the wrong way. First, the wrong way would be to come in here into your view and render out the list as different variables. Some of the words saying like four item in some list. So let's just do another list here and then doing my context and something like this item a equals two item. Granted, this is not the greatest cause. It's only gonna end with that, but essentially, you don't want to do this. You don't wanna go this route, but we are starting to move towards what we want to do in the template. And that is we want to create a four loop within the template so the template themselves can have this site sort of flow. We can actually go into a template, and we could run a loop on any given context objects or constant context variable. So to do that, we would just go ahead and do curly brackets or braces percent four. And in some arbitrary variable name. I'll go ahead and say my sub item in my list and then curly brackets and with templates, it is different than Python. It's closer to how like HTML would work. So if you open a for loop, you must close a four loop. So there we go. We have a way to actually look through this stuff, and then all I have to do is put a UL on the outside of it and then a ally on the inside of it. And then we can just use those curly brackets again to braces and do myself item. And we saved that. And what this will do is actually iterated through every single item in my list, assuming that there's stuff in that list. So if I refresh in there now, we see those two new things, right? So we actually see these things that are rated out and they're showing up here. And there are other additional features to looping in here, one of them being like four loop dot counter, and this allows us to see what iteration number we are currently. So if I refresh, I see that it goes 123 right and granted, In my view, if I were toe change the order or change the values and even have you know ABC Strings like that stuff like that, all of that will come through. It's pretty nice. I mean, it allows us to do all sorts of things inside of our template. That's based off of what's coming through our view. Now Don't get me wrong. We still aren't seeing data from the database like we're still hard coding all of this stuff, like I do want to work towards where I can actually see data from the database. But there's still a few more things that I want to do with templates and views before we get there, so make sure you subscribe to get everything and I hope to see you next time. Thanks so much for watching. 20. 19 Using Conditions in a Template: So what we want talk about are conditions in a template that is an, if else statement inside of our template itself. Now, by and large, you want to make sure that your logic is already handled, in your view, that son of a general rule thumb. I'll give into that a lot more later when we get more advanced. But basically, if you're going to be doing a lot of if else statements, you're gonna want to do those in the view and change your context that you're rendering in the template. You don't want the template to do too much of that work. But there are cases where you will want your template to do it, and I'm gonna show you a very basic example off that. But I'm also giving to give you a little bit of a warning with your variable naming with templates. Okay, so the first thing is, when you have your context here and you render it out and let's say, for instance, you do have a list like this, we're gonna want to be very selective about how we rendered that inside of our four loop sort of the words. If I changed, Let's say, for instance, my text to being a B C and then inside of my about. I have a B C here and then I change my variable in my for loop as ABC, things get a little hairy and they might render fine, right? So it renders out fine. But then, when I actually want to do a condition such as if ABC equals 2312 you know, if that's the case, then I might want to rethink on how, actually named my variables because they are. I mean, then this is already looking tricky, like I'm already unsure of what's going on. So let's keep this in as my text and my text. Something like that is frustrating when it happens when you don't catch it. But it is something that you'll gain the skill of overtime. I promise you, it's just about recognizing how your name variables and this is true and python and general right? So sometimes you might name a variable one thing, and then it's rendering of something else. It's also true with functions and classes. I mean, I've seen it happen a lot, and it's happening me so much so I just want to bring your attention to that because of what we're doing right now. Okay, So going back into this about page, I have this condition here, right? So I actually created one, and it's really simple. We just call an if statement. So percent, if some variable or some thing that would be able to compare to another thing or more simply, if something is true. So you could also pass in a context variable in here. So back, in my view, I could just say, uh, this is true and just set it equal to true, right? So I can I can still use that. So these conditions, what they allow you to do is just evaluate whether or not this statement is true and you can use things like equals equals just like I have here. And then inside of that block inside of here, you can render something in particular. So let's go ahead and render that something in here. And you know what? I'm gonna throw in a another little conceptual thing that will take a look at later. Both just say add 22. So I've got a straight arrow also Nunes or a straight line also known as a pipe. And then we just type out ad colon and then whatever number we want to add. So we do that and then we refreshing here, and I've got stuff rendered out so noticed that the third generation goes twice now. This is because I have my f statement, but then I also have the default or what I used to have. Now what we actually want to do is instead of having the default like that, we would just say an else clause in here and bring in that else claws just like this that way that the list can be iterated through. And if ABC is equal to 312 then it's gonna render that out and add 22 to it. So we say that and we refresh, and now it's still only those four items, except that one condition is now met and it's allowing us to do some addition. Now we can also have another condition, right? So if I came in here and wanted another condition, you might be tempted to say something like else if but in Django, it's LF just like that And that's how we would do another condition so we can check that other condition. Let's just say if it's equal to ABC or that single string. So if it's equal to ABC, we can keep that for Luke counter in there if we want. Or we can say this is not the network. ABC There we go. Nice little ways of doing conditions, and you probably are wondering, like, What the heck is this? What's going on here that is actually called a built in template tag filter? So every single one of these items is called A. It's It is a context variable, but it's a template tag itself. It's like rendering something out that's specific to the jangle templates and the template in engine. And all this does is it does a very basic function on that context, but we'll play around with that a little bit. Maurin the next one. So make sure you subscribe to get everything and thanks so much for watching See guys next time 21. 20 Template Tags and Filters: By now, we've used several built in template tags that are provided by Jingo. We've used extends. That's a template tank we've used Block. We've used four like the four Loop we've used. If but this isn't it right, we've actually have a lot more. If you look at the documentation itself, there's a whole list of items that you'll see for built in template tags themselves. You know, we just talked about Block, but there's one for comment. So if you want to write notes, you can write comment as a template tag. You can cycle through things. That's something we haven't actually covered. But it is something that maybe to play around with or if you want to see it in particular, please let us know in the comments. I don't actually use cycle that much anymore. Now explain why in just a moment, but we've seen four loops we've seen include we've seen extends, right, so we have all sorts of items in here that allow us to make our templates that much more valuable. And then there's another thing that I kind of tease that we'll talk about right now, which is filter. So so this is a filter. All it does is take whatever this value is or whatever the template context variable value it. So any of these things can use filters, basically a rule of thumb. If you see two curly rackets, it's a really good chance that you'll be able to use just a pipe and then some filter there . And yes, there's a way to make custom filters as well. So if you have, if you have a need to see that, please let me know in the comments. And here is the built in filter reference, right, so we have all sorts of different values. Notice that you can add numbers or even a string that represents a number. You can add variables. So if you passed in a different context, variable and you wanted to combine them together, you could do that. We could do stuff like slashes or cap. First kept. First is probably a really good one for, Let's Say, for instance, in here we changed my text to being title right and then back into our actual template. We would change this to being titled and let's go ahead and bring it into an H three tag. We put title here and then we would use that filter cap first. And we close off that age three. And all that does is it ensures to us that that first text item is capitalized. So if I said ABC this refreshed and there we go pretty cool. Now, luckily, the documentation gives us a lot of examples of using this, right? So, like, when would I actually use value of centre? Right. So if I wanted to use center, I can honestly say I don't think I've ever used this template tag. That's why the documentation there. But let's go ahead and use center and try it out if I refresh. Oops. We have two extra curly brackets in there. Sorry about that center. It actually doesn't render anything for us because of how templates work. But you know, so I mean the issue, Mel, I think does have extra spacing. No, it doesn't do anything, so I didn't do anything. But what you can do is you can absolutely use two things at once, so I can say cat first, right? So I can filter that to camp first like it does. But then maybe we use the upper case with just upper, so slash upper and all that's gonna do is capitalize the first letter and then uppercase all the other ones, right, so we can stack those filters together, which is kind of nice. Now, I'm not sure exactly when you're gonna be stacking these together like this, but it is possible. Another thing that's possible is something called safe. So when you pass an HTML as a context variable, let's say my html and we said Hello world by default when this is rendered right, So I want to render out some HTML in there when it's rendered it. It's rendered as just plain text, right? So it doesn't actually render the HTML document right, so we can actually use a filter in here called Safe. So we can say that and then you go. It actually renders that html and there's a lot more like truncate characters or title looking. Hey, that's pretty cool. So how about we do that? We've got cat first and then title, so title and title a title it title eyes is the entire title variable, right? So this is the variable, Whatever that is, is the variable. And then once you put those pipes there, it turns into the filter name that it might be cool so we can do time. Until so using dates, stuff that's a little bit more advanced than were at you can change the time. You could change the format of time. You can strip HTML tags, right? So we see here we Scott Strip tanks before it. It's safe. If I did strip tags say that refreshed. Hey, there you go. It's no longer showing what that HTML is, so those air kind of the reverse of each other one actually renders it. The other one just gets rid of it. These air pretty cool, Very useful inside of templates themselves. Slug If I Hey, that's another really cool one. Slug. If I what it's like if I do well, we refreshing here it turns Hello world with the H one tanks in there still into a slug version of it, or like a your l safe version. But I could also do strip tags and then slug if I and hey, what do you know? It removed all those HTML tags and solidified it at the same time, so you don't want to go to the documentation for this whenever you think like Hey, I need to do X, y or Z because they're very specific to just generally what you're using, right. So if you have user generated content and you want their titles show up as a title, then you might do that. But honestly, you might leave it as is. You might not enforce those things, and that's something that's really cool about these temple tags as you get a pick and choose on on when you want to use them. I don't actually think that you'll end up using them that much honestly, because as we see here, I'm showing you that they exist. But often times when you have values like this, you'll do that in the view. Or if you have something like this, also would do that in the view and, in fact, safe should itself be done in the view with something called Mark Safe. But that's actually not something we're gonna cover just yet. That's a little bit more advanced, but it's something worth noting. Okay, so that's it for Jang. Go's built in template tags and filters. If you have questions on those things. Please let us know. Now we're definitely ready to start rendering content from our back in. So the data that actually has been stored in our database we'll do that in the next one. So make sure you subscribe to get everything. And thanks so much for watching. See you next time. 22. 21 Render Data from the Database with a Model: Now it's time to actually render data from our database. But before we do that, we have to just play around in the shell a little bit so I can understand how to actually access that data in the first place before we render it out. So access it. Let's go ahead and jump into our terminal. And I'm in the root of my Jingo project where managed pie is. I do have my server running, but you don't necessarily have to do that. Let's go and jump into Python. Managed up you? Why Shell Also, I'm assuming that you have this exact model, our product model. You can have any model you want, but make sure you have one model on, and I'm gonna go ahead and import that from Franek Stuff Models import product. Okay, so if you're not familiar with how these imports work, might wanna go back a little bit, but basically we're going from that app looking in that model module model module that's a mouthful. And then we're importing product and to get any single product item, we could do product that objects thought get. And then we want to use an I. D like argument here. So I d equals one I had enter and I get back. Whatever that object is now, I can set this to a variable itself. So let's say O b j equals to that. And then in here I could do something like D I r o b j to see all of the different things I can actually hit on this. Right? So there are There are other intuitive ways on how to do this, but this is maybe a natural way. If you're familiar with Python, you would just use de ir to see all of the methods that you might have on here or attributes that you might have on here. One of them is I. D. One of them is price title summary. Right? So a lot of these are unique to my model. Of course, that data is also coming from here, but and a lot of built in things that Django has itself in models that model so realistically, you probably do know BJ title or something along those lines, right? So B j dot title is unique to this model, and it allows me to grab the title that have saved in the database. So now we sort of understand a little bit more on how to actually grab some data. Hopefully, you're wondering, where the heck did I d come from and is Get called. We'll explain that in a little bit, but let's go ahead and exit out of the shell for now. And let's go ahead and create our first view where we're rendering out this actual data. So whatever this data is, so open up my views inside of the product app again. And I mean the reason I'm moving it out of the view of this other one, if you've been following along, has to do with congrats. See? Right, So you want to make sure that everything related to your products is inside of your product app. So if you're rendering out ah, view that's related toe one product that should probably be in your product at there are exceptions to that. But just keep that in mind. So all I'm gonna do here is say deaf, and I'll just call this product detail view without getting into too much explanation. Just stay with the naming conventions that I have so like product underscored detail view. We're gonna show one product. That's what we're calling a detailed view. And I'll go ahead and put in the request here and then returns, render, and it takes request some template name and then some context. So, of course, we have to create our template itself. So let's go ahead and write product slash detail dot html. Right, So I did add a slash. Now, if you've been following me, you haven't seen that yet. So we're gonna talk about that in just a moment, and the next thing is the actual context here. So I'm gonna go ahead and do from dot models doing a relative import here. So from got models, import the product class, and this is where sometimes people get a little bit off. First of all, they might name a function like a class, So don't uppercase functions, keep them lower case. Secondly, you might put something like product here. That's not a good idea. Just make sure that it's explicit to what it iss. So we're looking at a detail of a product. That's why you want to put that product view there. So, just like in the shell, we did product that objects get i d. Eagles toe one. Now there's a really good chance that this was an error for you, and I'll explain why we jump into the and men. So let's go into our admin here and we go into products I actually have to products in here , right? You might have a lot more or you might have zero, but my model actually has some items in here. So if I click on this 1st 1 I see there's a number right up here that is actually the I. D. We can also look at the other product. That's the other I d. 12 But if we look at our model, there is no I d. Here. Well, that has to do with Jang Go's built in features. One of them is that it has an auto incriminating I d, which you can see in the migrations. In the initial file. We see there's this i d. Of auto field, so it automatically creates that for us. We don't have to do anything. Another win for Django. Okay, So how to actually create the context here? Right, So that context is just an empty dictionary by default. And what context? So I want to put in here Well, I could put stuff like title and again that was Obi Jadot title. I can put stuff like description and O b j dot description. You could fill out the rest on whatever you want to show up on the model. This context again goes into my template, but I actually have to create that template. So without getting too to advanced level stuff of where we can locate or load our templates , which I'll talk about literally in the next one, we're gonna make a new folder in this template folder here called Product. And inside of that folder we're gonna make a new file called Detailed on HTML. That's all you have to do. So if you want to have directories inside of your templates, you just make a directory in there and make sure you add that slash Put it the same slash as that way, regardless of what system you're on, it should still work. Okay, cool. So we've got details on HTML and with detail. We want to extend so extends based on html. Of course, this is continuing from previous video so make sure you watch those. And when you block content and we dio just in block go that standard, we've create our template. Let's go ahead and make sure we import our view to our your else. So what are your l's? We're going to go ahead and import the product view. So from products that views imports, product detail view, that's the name of the view, of course. And then finally, I'm just gonna go ahead and make a new path year and we'll call it product, and it just takes end product detailed view. Cool. And I'm just gonna go ahead him doing each one here and say item Okay, so let's go back into our project. Go in a product. Cool. If you don't have this, stop. Go check the code against get hub. Do all that. I I just breeze through a lot of things because we've already done this. But what we haven't done is seeing how to actually render out some objects data. So, like some database objects data in this case, I actually already did it, so you might already intuitively know Hey, I can just do curly brackets title here and then I can do you know, something like P tag description here and so on. Okay, save it. Refreshing. Here, you've got a new product. Looks like I don't have a description, so don't you worry about that. But of course, this is a good opportunity to use a condition. We could say if description, they will just render out the description. Otherwise, we'll say coming soon, or rather probably say something like description coming soon and the real perfect. So again, if description is not none, I mean, you could do all sorts of, like, conditions in here that work in python as well. So those air Python conditions or sorry is not equal like that. In this case, it's not actually equal to done. So we would want to say and description is not equal to an empty string, which it very well could be. And that's what it waas cool little bit Maura about conditions. That's something that's great about watching these tutorials and building something. Really? Okay, so now that we've got that, we've actually rendered out our template. We've rendered out on object. But there is one thing that's really dis event disadvantageous to this method. So if you look back in your view, what I have to do is I have to map my objects context, right? So every thing that's in my objects, meaning every field here, I would have to map it to the context itself. That's okay, but it adds an extra step like meaning If I ever change my model or I wanna have new data in my HTML, I also have to go in my view, and that's a little inefficient. So what we want to do instead is instead of all this, just get rid of that Or let's just commented out. We'll leave it in there for for reference for you guys. So we do content and we say object is O. B. J so that now the context object is object, right? So the context variable is object So inside of our detail here, we could then just do object D title, object description, object out description, object of description. Yes, I will promise that I won't repeat that again. And then object dot price. Right. So now I can actually change my template whenever I want and not really worry too much about this detail. view, right? We got we an error. What happened? Object. A title sent out the name. It is title. So object should be in there Should have rendered. But perhaps we didn't save our view. We didn't. Okay, cool. So this is now rendering that stuff out all from the database. Really cool. And of course, you can play around, change things, do all that. I realized this one was a little bit longer, but it's kind of comprehensive to a few things that we just learned. Thanks so much for watching. Make sure you subscribe to get everything because we're gonna be doing ah, lot more Django tutorials. Thanks a lot. 23. 22 How Django Templates Load with Apps: you might recall that when I said when you create a Jingo app, remember, Call that when I said to create a Django app. When you do that, you want to keep as much about that app inside of the nap or that components directory. Right? When you do this, you make things nice and simple, so you can use that app and other projects. That's often the goal. I mean, if you make a really, really good blawg app, for example, you can reuse that over and over again, or even release it as 1/3 party package that other people can use as well. So if you end up doing that, then that means they're gonna have to do additional configuration for it now. This gives us a really good opportunity to talk about templates in that context. So purposely left this out because I wanted to show you how to render out some data about that app first and then move the templates around. Now inside of our APS, we can actually put a new folder in here called templates. So inside of the sub app, you can have a new folder in here called Templates and you can do things inside of that. We could make a new another new folder in there. That new folder is gonna be the name of our app. So just like in this templates, we did products detail. We would do that same thing here. And in fact, we would name it exactly like our app. In my case, I did product first, So I'm gonna name it to products, and then we will call this product underscored detail dot html. So we've changed a couple things about our template. I will copy and paste that entire template itself. Bring that in because we will see some difference. So I'm gonna just called this one in app template with that title there. All this is for is illustrating the difference and understanding how Django looks up templates themselves. So we say this, and in our view, I'm gonna change this to products detail, not the exact thing, because I want to show you something. You can jump ahead and go to the exact same, but I want to show you something. So if I say that, what's gonna happen is I render this page out and I get an error saying template does not exist. So this is actually pretty important to understand because a lot of times you put temples the wrong place, where you have a spelling error or something like that. This allows me to see what's going on, and we've got this temper template. Loader is showing me how it's actually loading my templates, so the first thing that it looks for is the file system loader. This right here believe they're not comes from one of our settings. So if you go into that main configuration settings and where we actually set up this template dirt, that's what that is, that's all that ISS. So if I had actually written out the path to that that would show up. So if you were on the wrong system, you would see the wrong path here, right? Thus, we make it like this. The next thing is you'll see a few other places where it's looking. It's looking in Django site packages, contract admin, templates, products detail. It's looking inside of the admin app that's built into Django, and then finally, it makes its way down to the actual app. Loader so looks inside of our own app inside of products, templates, products detail. Of course, that's not the actual name. The name that we change. The two was product underscored detail. So if I say that and refresh everything looks good. But let's keep it as product detail for just a moment and just uncover how cool this actually is. So we're gonna actually look at the jangle package itself inside of our project. Do keep in mind that we do not want to change anything in here Were just looking at it for illustration purposes only. So we're going to Lib Python 3.6 site packages. Django, the just the single Django, Not anything else. And then what? I mean, if we trace back what's going on, we see contract admin templates. So Con Trib admin templates. And here are other templates. Actually, Let's look at this admin app itself. This is actually not a whole lot different than what we have. There's certainly more things there than what we have, because we're still doing a lot of basic stuff. But I mean, at some point, you might not ever have a nap with this much stuff, So that's not really the point. The point here is it's the exact same thing. It's the same Django app with the admin or the built in app as it is with what we're doing . That's pretty cool, so you can actually see the templates, and you could edit them here. But you don't need to. In fact, what you would do is if you want to override any of your built in templates or any of the APP templates. If you want to override something like this, you would then just put him in your file system ones. You would put him in here with the exact same path. So let's go ahead and change the view back to being the actual proper one. So it's products, right? Product detail. That's the end all be all that we want. So we see that says in APP template. Now, if I wanted to override this template or any templates for any of these APS, I would come into my template folder, that main directory that I created and I would rename or add the same path that I had before. So gonna rename this to product detail. Okay, so now it's completely changed that refreshing here and There we go. So it's actually showing me the Ogi template, right? And that's how you go about doing this. But do keep in mind that when you're working on your own solo lap or you have a team, chances are really good that you're gonna want to keep it inside of this itself. So I'm actually going to completely delete this folder. I don't actually want you to get confused about it. That's why it's gone. But I just wanted illustrate the purpose of how all of that would work with your templates and how you would load them. And I think it's critical to see it inside of Nap versus any other form, because really everything about this product app at this point is inside of this, with one exception of the your L of actually bringing it into the jangle project yourself. But that's not any different for any of our APS, including the built in one like admin app. If I got rid of this, that app is no longer accessible to Django. I mean, it's still working, but it's no longer accessible to our main project, so that means that is a very reusable app. and that's the theme here. We want to keep that up. So we have a lot more discuss. Make sure you subscribe to get everything because we will be doing so much in Django that you'll become a master after Washington. Syriza, That's the goal is to begin as good as you possibly can. So thanks so much for watching to see you next time. 24. 23 Django Model Forms: Now, let's take a look at the simple power of Django for me. You see, we want to actually be able to allow our users to save data in the database. And they really wouldn't use the admin or the python shell to do that. So this one, we're gonna be breaking down the basics of using jangle model forms. So what does that mean? Let's actually take a look. Now, inside of any given app, I'm gonna go ahead and make a new file here and call it forms. Stop. I do Keep in mind that this works for any model, not just the one we're using. So we're gonna go ahead and do from Django import forms, and they will do from dot models. We're gonna import our model, which, in my case, it's product. Right? And then make a new class in here and I will call this class product model form. Or really, I would call it product form or product. Create form those air a few different names that you might end up using, so we'll just leave it in its product form and we call it forms dot model for and all. We have to do. Here's do class meta model equaling two product and then the fields, like the actual model fields that I wanna have in here, like, you know, which ones of these do we want? Well, let's just do title and description and price. Okay, so title and then description and then price. Okay, so we now have our model for I'll have to do is render this out in a view. So it's going to a view and let's import this so from dot forms import product form. And I'm gonna make a new view specifically for this and just copy this detail view real quick, and we'll call this product create view. I won't worry about the object itself, right? So the context that I want, though, is all just a form equals two product form, and they will just, um, initialize it with request that post or none. That's a worry. If you don't understand what's going on here in a few videos or the next few videos, we're going to go into a lot more depth here, so just just bear with it. So a form dot is valid. That's another thing that we will in a doing. I'll just do form, not safe, Okay? And I'll go ahead and in this form here into my context and make a new one called product creates dot html. So that means I need to make that template itself. And by now, you should absolutely know how to do all of this, right? In a simple way to do it is just copying, of course, are other template. And then in here I need to create a form element or a form HTML element. And I'm also gonna do input type equals to submit and was a value equal to save. Close that off and then inside that will just you formed up as p. So this is a built in method that turns the form that we're passing as context into a actual HTML form rendered out with paragraph tags. We'll take a look at that code in just a moment. But now that we've got this create view, let's actually bring it into our your l's so into your Els import this in and I'll just go ahead and make a new path for it. I'll just call it creates, and it's gonna be the product creative You. Okay. So again, let's make sure everything saved all across the board forms, templates, all that stuff. It's going to look in to this create. And here we go. We've got our form rendered out here with the actual fields that we specified. So just a new product. This is awesome in some price, right? Doesn't really matter. I had saved and to get some error here. Right. So I've got this stuff up here. It's not really an error. Just I didn't actually specify the method that I'm using on the form itself. So thank you. My template. We'll go ahead and say form method equals to post. And we're also gonna add in this CS r f token. Okay, these things we will absolutely discuss when we break this down and go from a bare minimum . I just wanted to show you how to do it first, Hopefully to pique your interest toe, learn a lot more about this. That's the point. Okay, So I refresh in here. Let's get rid of all that stuff in the your l. And I'll just do a new product again. And description. Does it matter? Price told 99 Okay, so I've got this not no constraint failed, right? So I have an error here, and that's because if we look at our model single look, we have this featured and it's required and it's not blink. That's where this error happens. This hair, it happens all the time, which is part of the reason I actually left it there. So to actually overcome this for now, what I'm gonna do is just adding a default of true or a default of false. Either way, that part doesn't matter. But we made some changes to our models. So what do we do? Python managed up. Why make migrations hoops? We want to make sure where the root of our project work. Python Manisha P y is make migrations and python managed up. Ey migrate. Okay, let's try that again and we'll say new product or new course whatever. Doesn't actually matter. Like I said a 1,000,000 times, I go ahead and say that and I cant tell actually anything changed. So let's go into our admin. It's just long in there going our products. What do you know? It looks like I have more products. I've got new course here and then new course here, so I actually saved a few different items of data. So one way to solve that problem, as in clear out what's in there is really simple. We just go back into our view and we re render it just like that so we would just re render the form. So then the context will change an update for us. So let's go ahead. We can literally re submit that same data. We get this error. That's, ah, security problem or not a security problem, but a security feature. So let's go ahead and just add in another one. And some price we could save clears out the form for us. We go into the back end and what you know, we've got a product there. Cool. So we now have a model form. So the next few videos What we're gonna do is really just break this down a lot more in depth. Like how doe I actually build html form from scratch and understand that data. That's where we'll start. And then we'll get more and more complicated to better understand how forms work in general and then better understand how they work with Django because this is certainly not the only way to do it. But it's a fairly easy way, even if it doesn't fuel like it's that easy of a way. I promise there's a fairly easy way to do it, so make sure you subscribe to get everything and I'll see you next time. 25. 24 Raw HTML Form: Now that we've created this product model form and rendered it out inside of our product create, it's time to just take a few steps, bank and do it from scratch as in raw html, and then move up the chain as to how we can get and understand how this model form actually works. So one of the first things that I'm gonna do is actually get rid of the Django related stuff. So those two things are now gone, right? So we have only one html input here on that is to save it. So the first input that I want to actually put in here is input type eagles to text and the name is going to be titled, as in What do we want to call this input? I'm giving it. The input itself is a name so I can use title. I can say my title. Neither one of those actually makes a huge difference which will see why in a moment s So we got input type, text, name, title, and we can also added something called a placeholder where we can just say your title. This is just kind of giving them some information. We'll see what that is in just a minute. Okay? Next thing is in the view itself. I'm gonna go ahead and comment this one out and create a brand new one that Onley renders out empty context and no form related data. Okay, so we've really written re backed up a lot and redid this view. Okay, so we look at this and we look at that view. Let's go ahead and take a look at it inside of create. And we've got your title here with a save button. If I tapout ABC and hit Save, I get this. See? Srf verified failed. Okay. And why is that happening? Well, it actually has to do with our method here. So if I change that method to get what happens, refresh and say ABC it saved It seems like something happened. But what you might have noticed is the your l changed. You see, get methods are what is the default method for a form. So if I just typed out a form like this and rendered it and said something else, it will change the your l. This is actually very similar to like when you want to do a search on your website. But instead of doing a search and going into the nitty gritty of that, what we are gonna do is keep that method of post. Okay, so we say that then refresh. Try ABC again and we get this forbidden yet again. Now, this has to do with security measures that Django has built in. Now, a big part of the reason for this is something called action. So if we type out action and we said it to, let's say a different your well, let's do like slash search and let's go ahead and bring that method of get back and I type in Let's do it refreshing. Here, type in ABC. It saved. Whoa, What happens? It takes me to a different your l as well as a different, you know, also including that data that I actually put it so action then means they can send it to a completely different your l. So what if I did the same thing with, like, Google? So w w dot google dot com slash search. I'm actually gonna get rid of that trailing flash there, and I'm also gonna change the name here to Kew. And then I will just say your search. We say that and we refreshing here. We've got your search and I'll do Django cfe hit save. Hey, what do you know? I actually performed a Google search right there. And what's pretty cool is quoting French voters. Came up a lot. That's awesome. I like seeing that. OK, but what's more important here is understanding that Hey, I just created my own Google search form, which is really nice for one feature. But the other part is, none of this in here is related to Django. So you actually learned a couple things. Number one. The action will send the form to whatever you are l you put there now if we actually changed this method to post just like that and now searched the Django CFE, let's just refreshing here and now search that it's safe. We get, ah, four or five error. The request meant that post is inappropriate for this. Your L. Now that hopefully makes sense. If it doesn't, I'll explain in just a second, but it's pretty cool. We can actually change the action and we can change the method now by default. What happens is the action goes to the current Eurail. So if you do period here, you could see it as an empty string or period. Either way, it's gonna go toe. What? This your l is pretty nice. And then it's going to use that post method. So again we're gonna change this bank to being title and your title. And then we saved that and do ABC I had saved. And again I get that forbid and call once again. So to override, then problem. We do CS r f toke it. Okay, so we save that and we refreshing here confirmed for re submission, we can say yes. Let's try it again and there go So it actually goes away. So the data seems to be going through now. What the heck is get in post now get request is meaning that you go to any your l So when I go to a any eurail on my page, like about contact, whatever you are getting information from that when you want to save information in the back end, we use post like kind of like mailing a letter to somebody. You post it right this is very similar, and we just use two different types of requests to do that. Now what's happening is actually not a whole lot different. Like the back end can treat both kinds of data in the same way which we can see in our actual view itself. So our view itself has those two methods built in. So if we do print request that get and print request dot post, we can actually see what's going on with either one. So with get if I just refresh the page here, I see that I've got this query dictionary that's empty and what I can actually do. It's a, you know, title equals to something, right? So I've got now I have changed my get requests because I changed my your l I can now see in here that I've got some data coming through from there. So this is an actual dictionary that I could say get dot get or, you know, grabbing the actual item key value pair there, and that will print out whatever title I put in here. So if I said yeah, this is a title, it's gonna actually print that out for me in the back end. Pretty cool. So, yes, you can absolutely save this data. I don't recommend doing that. Using git is a unsafe method for saving data. That is why there's a security feature for Post, and that's not unique to Django. It's certainly not unique to, uh, any Web application. It's It's a general rule of thumb two. When you have post, you want to add in some security. Now, the reason why is that? If I was gonna have this title in my your L, then ah, hacker or somebody else could just use a link that changes it. Teoh being something like this and that would quite literally change the title For me, it starts to bring up all sorts of security questions that you may have. So instead of doing that, we use request post, right? So McGiffert of this part of the get call and then inside of my form, I still have it at post so I can get rid of all that and do ABC it save. And now what I see is all of that data coming through pretty cool. So that means the very last part of this is I can actually come in here and say Title equals two requests that post that get title and I'm using dot get for the dictionary itself to grab what's in there, and that's gonna be the title I would use. And then I'm actually not going to save it because I don't want to put all of the information in. But if I did product that objects that create title equals two my new title, right? So my new title and then I had every other fueled in there. I could then go about actually saving that requested Post Data, which is denoted by just printing out that data itself, right and getting rid of these requests methods here. Okay, so it's safe, ABC and save there. We've got our title being printed out. Let's go ahead and say it again and try it one more time. Not refresh ABC. We had submit, and all I see is a B. C, the very first to see none. Now why is it that I see none and the very first is because we don't have any post date up by default? So there's a lot of things that I still need to dive into when it comes to this. But when I just showed you is how you would write a raw HTML form in Django and then, just generally, how you would create the data from that. There's one thing that you could do is say is if request that method equals equals to post , then you can run through and do all of that stuff, and that's okay, So that's asking for the method. Whatever that method is will give you, you know, get post or a few of the other HTML methods. And then you could go ahead and say that. And then that way, when you refresh on that page, it won't say none first like it did right. Instead, it will just Onley render out. ABC does not there because I copied and pasted. But let's just enter a lot and just illustrate the point. I refresh the page again. I get requests there. I do ABC it save. I got my post request and the actual post data cool. So if this is unclear at all, you might want to look into Maura about HTML forms themselves. Please let me know in the comments, but this is really a bad method of saving data is it's really, really poor because we're not validating. If this is good data at all, we're not cleaning this data. So we want to make sure that we do that. And that's another topic that when you discuss and we'll do that in the next one, so make sure you subscribe to get everything. Thanks so much for watching. See you next time. 26. 25 Pure Django Form: Now that we know how to crab raw html form data in Django, it's time to start adapting it to using just a basic Jenga form. So a pure form. So I'm actually going to again comment all of this out, copy it and paste it over. Probably should have copied it first. But, you know, whatever. Okay, So again, I'm gonna go ahead and delete all this because we're basically starting from zero or as if we were starting from zero. Okay, so I've got my view here, and then I want to open up forms again. Just go ahead. Ignore this for now. But it is not a bad reference, so I'll just go ahead and say class raw product form, and it takes informs dot form. So it's not model form, just standard jangle for So with the standard uniform, we have to declare our inputs. So in my case, I've just been using model, but I could go further than and we use forms dot char field. And you know, we can say description and we'll also do form stock, chart field as well. And then we will say price and forms that decimal field. So these should look very similar to our models on debts because they are So we've got a dismal field char field, and we don't actually have text field. So if you try to do text field, it's not gonna work. We'll discuss that later, but we'll just do char field. And honestly, if you wanted some reference for that, you just do jangle form fields and it should give you the field types in here, right? So form fields. All of the various field types are in here, so you can absolutely use any of those. But, you know, text Field is not there. So these all render out a specific look by default. So if I grab this form, bring it back into my view and are bringing into my view, not back into it, we have to render of this four. So to render it out, we create an instance of the form. So we'll say form is that this creates an instance of that form. As you may be familiar with python and how you create instances from a class we had that form into our context. Okay, So again we could say my form here and then have form there. Still, going off that product creates so back in that html time to get rid of this year and just do form dot as p. So this renders it out as paragraph tech. So if we say this and take a look there, let's just refresh that page. I can inspect the element and take a look and see that Hey, we've got our form. We've got an action. We have our see ece r f middleware token that security token. We have some P tags in here and that's as p. There's other ones, like as html No, not as html, but as ally. Or is it you l some like that. There you go so you can rent it out as you l it is html so as HTML doesn't make sense, But as P is a way to render it out, and now we have a form with some data so I can add in whatever like right and hit save. Now, naturally, what just happened was we submitted the data to our form here. Or at least it seemed like we did, but we didn't. We didn't do anything right, so this form itself is just rendering out. It's not actually getting any of that data, and that's why we use request dot post here. So if I say that, just as is and refreshing here, what's just refresh in this page? And I see that now it's giving me these validation errors. These air called validation. Harris. This is awesome. This is built into jangle right away. It's already validating stuff for me. Unlike the HTML, the HTML absolutely was not. But it's asking, like, Why are validation errors even happy happening at this point? Right, so we see that this field is required. But how does Django already know that? Well, it has to do with what I initialize it with, so I have it in as request post. So let's make this work using come a long way. At first, we'll say, if request dot method equals equals to post once again that's built into the request object itself that's being passed. So if the method is equal the post, then that's gonna be my form. I'll actually use that data in my form. In other words, all passing that request post data. As I initialize my four so I also have to initialize the form by default, So I need a good method for it. And that's where this comes in, right? So I render it for the get method so I can see what it looks like. And then with the post method, which is a different type of request, which means that I still have to render it for that different type of request. So we've got this is roughly request that get right, So that's what is passing in by default. But requests I get doesn't have anything, probably. So we'll go ahead and leave that out. And now this will actually handle my form data. So if I save this on a refreshing here and hit save, it's now saying, Please fill out this form and that is based off of my browser and see that it says required there. So if I actually removed that right, so I literally removed it from the code in the elements. You don't have to do this. This is Mawr illustration purpose and hit save it just moves to the next one. So again I remove that and then the final last one, all those requirements just remove it and I had saved Django. Now does the validation for me, right? So I actually showed you away that a user could potentially disrupt some of your raw HTML forms. And that's where Geno security comes in. Not just for this, but also for this, right? So I absolutely need all of those things working. So I had all of those fields required, and this is a way to actually validate it. This does the validation. So there's a method called if my form that is valid, this will actually say, Hey, you have a valid form, you know? Now the data is good. I'll explain the data part in just a second, but now that the data is good, we'll go ahead and print out my form dot Cleaned data. So this would be the data that actually comes through from the form after has been validated. Okay. And then we could also say print else my form dot errors. Okay, so we've got those two different methods. So if it's valid, then it's gonna do that. If it's not valid, then it's gonna print out with those. Errors are oftentimes you won't necessarily need this because the form itself, as we've already seen, renders out the problems that might be related to that form. And that's because of this right here, too. So a lot of built in features going on here that we're just not quite bringing down just yet. So let's go ahead and take a look at this. Let's actually put in some valid data, come back in this create method. I'll say ABC, you know, whatever and whatever I had saved. And nothing that form doesn't rear ender, which will solve that problem in a minute. But if we look at our terminal, we see Hey, here's our clean data. It actually gives us that data and the air list didn't show up, Aziz. Well, because we re submitted the form and there were some errors. Okay, so what is another thing that we could do that you know, would potentially cause some problems? What if I change this price field? If I got inspect element, change it, change the type in the actual html to, um, text. Right. So now I can actually type out text type out text about text I had saved. Hey, it's got to be a number so that's other built in validation that are forms actually have. Right? So from this data, let's actually save an object. Let's actually do something that you would end up doing right. We've got a product that objects that create, and we have some of that data that's coming through, right. We have a dictionary off this data, and it's doing title description and price, right? I named those fields on our form. That way on purpose was to match our model. And that's what you should do as well. Because again, we're working towards this model form and we're doing the long way. We're understanding mawr as to what's going on. Okay, so now that I've got that I've got this created here. Well, what if I actually just passed in this data? So let's go ahead and pass in the clean data here, just like that, create it from that clean data. So let's go back in here and I'll say new title, some description, you know, some price I had save. And again, I get an error here. Right? So what we can do is actually use the two stars here, and that will turn this into arguments that we're gonna pass. So let's try it again. I had saved no errors. What's going on? My back end and I've got a bunch of new products with that exact same name. Cool. So I actually were was able to save some data from my raw Django form. That does some validation for me now. There are parameters and things that weaken set in here. There's definitely a lot more that we can do with these fields, including rendering out how this description works. So we're gonna dive a little bit more into that in the next one. Stay with us. 27. 26 Form Widgets: Now what we're gonna dio is things related to any individual field. Let's say, for instance, you want I feel to not be required or you want to change the label of it or whatever input is in there by default. Those things were right now, so if you take a look at your forms themselves, we can change a lot of things. Very simply. Do you want this field to be required? Well, just type out required true or false, obviously, with the title I wanted to be required. And that's actually the default. So really, you don't have to write out whether or not the default is true. You would just say false. Of course, you might be wondering, How do I know where the defaults are? Of course, the descriptions on the documentation for the core field arguments will tell you what the defaults are like. Required that default is true. It's in here somewhere. The next one is changing your label, so if you want the label to not even appear just making an empty string, it's a very simple way to do it. And like something like price, you can also set an initial value so initial equaling two, Let's say, for instance, 1 99 99 Okay, so that's a simple and easy way to change those three initial arguments to any given one of those fields, but refreshing here. There we go and actually shows us for that so more, more specifically, not necessarily refreshing, but just going back and re rendering the page. This number is actually in here, so there's absolutely mawr things that we can do in here as well, like notice that I don't have any placeholders well, and also the description itself is not rendered out as a text area. So let's do the description first on goal. Change this to being a text area by overriding the default widget by putting widget equals two forms. That text area and that's it. I'm refreshing there. It gives me a text area. You can look up all the various widgets also on the documentation for the forms because there's definitely a lot in there and you might end up changing things like a text area. And perhaps you wanna have even bigger or larger changes to that particular input, and we then just put in some parentheses here attributes equals to another dictionary. So since I'm really overriding a lot of this, I'm just gonna go ahead and separate out some of this so I can more clearly understand what's going on here. First attribute that I might have is class, like, maybe you want to set a new class name here New class, name or multiple classes right to you can also set the rows that you'd have on here. So let's say we did 100 Rose. I refresh. Hey, that is a big form there, right? Of course, that's probably not likely. Maybe you do something more like 20 and have a bigger text area. And of course you could You columns as well. Okay. And of course, by inspect this element here. I see that. Hey, I've got the name here. I have the two classes that I added, and then I haven't I d what if I change the idea here? My dash i d for text area. Refreshing. Here. You make sure it but calm at the end of that. It is a dictionary. I now see them ideas changed. So any different attributes that you want to put on there You absolutely can. And that's a very easy way to do it. Now you can also do it on the title to eggs. So we wanted to say that we want to put placeholder here, so let's go ahead and do forms dot text input. This is the default widget for the char field. So we would just change that to being attributes equals two placeholder and whatever you want. Placeholder be. So your title say that and we refreshed here. What do you know? Your title is showing up. Of course, that placeholder You might also want to have that inside of your text area. So your description and so on. Right? So that is how you override some of the basic things about the form itself. This also changes how the validation works just slightly, at least for the required right. So if I try to save it and I type out some of the other ones, the descriptions no longer required. So it's no longer gonna show up with a price, and the title are required. So if I get rid of the price, it will tell me to fill out this Phil field itself on that of course, is the Web browser doing that? So if I did that little hacky thing where I got rid of the required element on here and hit save again, he gives me that same validation. So that's pretty cool. But it's only giving us a little hint of what validation actually is. So this part is, well, we're actually pretty much identical to this product form at this point. The forms themselves aren't any different in the sense that the model form renders out the same sort of stuff in here. The only difference is how the view handles it. That's it, you might remember. We just did the form itself, and then we just saved that data. This is actually doing roughly that same thing. So now, going forward, we can render out new types of validation for forms. So let's go ahead and do that in the next one 28. 27 Form Validation Methods: Geno has a lot of built in validation for its fields. So when we render out a django field or general form, we actually can submit some data, and Jenga will check to make sure that data matches that built invalidation. So like it was a number in the decimal field, for example. But there's a lot more things that we could do than just what's built in, and they were gonna do it in this one. So if we take a look at the model form, which is now what we're gonna use, I'm going to show you that it's really simple to override the field themselves, right? So if I actually bring this title in over here, I see that this is the same name as that one. So that's actually how you override what comes in by default, kind of a cool little feature to have. So now what hopefully realized and I did mention the last one. But now these forms are actually identical. They have the same three fields. The only thing that's different is how the view handles them. That's it. But we're gonna go back to using that product form itself. So I'm gonna go ahead and comment out all of this, and we'll come back into our original product, create view that we were using. Okay, So what I have here is a shortcut method to what I did up here, right? So that request post and then render these things out. All this does is renders out that form if Post dated comes through otherwise we'll just render out an empty form. That's all that's going on. That right there. Okay, cool. So now that I understand that what I want to do is just take a look further with this is valid related stuff. So let's go ahead and take a look. Make sure my form is coming through a refreshing here, and I've got my price. My field is required. So it's possible that I might need to save a few things here. Let's make sure everything's saved up, gotta forms. There we go, and we refresh our title goes away. And if I go back on the crate, it shows me that I have my placeholder there. So, yes, I could absolutely just copy all of these things in here as well. And once I do that, I have a more robust form in the sense of how I wanted to look and almost how I wanted to work. So so when you do this, you realize that you did change quite a bit. And, yeah, there's absolutely shortcuts to doing this. There's third party packages that do a lot of this stuff for us, which we're not going to talk about yet because it's getting a little bit too advanced. But it said what I'm gonna talk about is like, What if I want my title to be, you know, to contain a certain word, Let's say one of my title to contain C F. E. Right? So that word itself, or that those initials itself, what I need to do here is come in and say, Define clean underscore, then my field name. So whatever feel that I'm gonna be grabbing the validation for this is what I want to do. Clean title, right? So I want to make sure my title has a specific item in there. So he's just a self and we'll do ours and keyword ours. Those are probably not necessary. But oftentimes, if you're not sure when you override something just make sure you put ours and keyword ours . So to get the actual title itself, we do self that cleaned data that get tail. Okay, so this is going to get the default title. This is going to get the post Django form cleaning so Django will clean the form itself initially, and then it's going to trigger this title. In other words, this is not overriding required, for example, that required will absolutely still be in there. And to validate this, we just are gonna go ahead and say, if title, or rather if CFE entitle then will return well, do we want to do it this way? Right? So let's just do the logic first. So if see, if he's in the title, then will return that titled. Whatever the default is otherwise will do forms dot or rather, raise forms that validation error. This is not a valid title. Okay, so we say that and I think look refreshing here. I'll say ABC hit, submit. Hey, this is not a valid title. There is our validation. It's really, really simple. I mean, it's not that advanced. Or that complex, of course, actually wouldn't write it in this format. Whatever do instead is say, if not in that title, then raised the validation and the default returning that title now. The reason for this is so I can have multiple validations if I wanted to. If news is not in the title, not Avella title, right? So ABC Space. We got to not developed Well, let's actually save them. Refresh this. Let's do that again. So it's a ABC I had save I've got. This is not about title, Okay, so right off the bat, it will raise that one. Now, assuming that I put CFE in here like that and it's safe again, it's going to say not about title, so it would have to validate all of these conditions that you might have. Of course, these aren't realistic ones for a title. This would probably be more realistic for like an email. So let's say, for instance, if I did clean email and we did email, I'll throw this email and just second the actual field itself. So let's just go ahead and say if not or rather, if email ends with that you or if not ends with you, they will do raise form stopped validation error. This is not a valid email, and we can return that email. And since I'm doing this, this is a new little feature that you might end up using is you could say email equals two forms dot email field. It's done the model form. It's certainly not on the model, so refreshing. Here. I got my email field in there. I say abc at gmail dot com. I had saved not a valid email, so it's pretty cool. Validation is fairly simple, and then you can run. You know, any sorts of validation things on here Now I will say that doing validation is not on Lee on a form, but since we've been working in these forms, it's a really good way to introduce validation. As a concept, you can absolutely do validation on the fields for a model as well. But the idea for validation works whether it's a model form or just a standard Django for and what you should see. Or hopefully, this pattern that you're noticing is that if I commented this out and change this to a form , the form itself will render the exact same. All of the validation will run into the same all of that. But of course, the one caveat is how the actual form would work. In the view, it is slightly different. So if you have any questions on validation, please let me know otherwise. Make sure you subscribe to get everything. We're gonna be doing so much more jangle stuff and stay with us. 29. Thank you!: Hey there. Thanks for watching the try, Django. Siri's. We covered all lot in this and hopefully have a really solid foundation on how to build Web applications with Django. Now, see, the trick here is to then start to put it into a real project. And hopefully you've been trying to do that all along. But realistically, now that is the tricky part. Is taking all this knowledge and applying it to something riel. And that's really the main thing that I hope you do from here on out is continue to make projects and possibly make them with Django. Anyways, my name is Justin Mitchell. I hope to see you again. Make sure that you follow me right here on skill share. If you want to see more of my courses, I will be doing mawr based off of what you guys ask for again.