Welcome back, everyone, in this lecture, we're going to start off with the basics of creating a view,
particularly with a function and then connecting it to the URLs that file so we can see it in our example
website.
And then we'll keep building on complexity and functionality to this particular website as we go on
through the rest of the lectures.
Right now, let's just focus on the basics, creating a view and then connecting it to an actual root
on our page.
I'm going to head over to the Visual Studio code now or here I am at Visual Studio Code, and we're
going to be using the my site and first app that we created in that short little exercise.
So we're going to do is open up first app and then inside a first app, I'm going to right click create
a new file.
And call it your LS that pay.
And this will control the URLs that are associated with views inside of first app and then later on
will connect it to the project level URLs that pie.
Sometimes the easiest way to tell whether or not you're in the custom function or, excuse me, custom
app URLs that pie file versus the project level one is recall.
The project level one will always have this helpful info already filled out for you.
And it's also already going to have the pattern for the admin.
We're going to be talking about the built in admin panel in a further section in the course.
But just keep in mind, that's one easy way to tell in case you get confused of what current URLs that
profile you're in.
So what we're going to do right now is we're going to focus on creating a very simple view inside of
views that pie, so I open up views that pie.
This is inside my first app.
And let's create a very simple view that's just going to return back a HTP response that confirms we're
on this page.
This is technically just review since we've already done this before we say from.
Django Dot.
Let me zoom in here a little bit here for you guys.
Here we go from Django Dot http dot response.
Imports and we're going to import HTP response, and then later, we're going to import other things
as well.
Then I'm going to create the simplicity of possible just simple underscore view it takes in a request.
And then we return.
Each to the response, and let's just call this a simple view.
There we go.
And let me actually quit what's going on here in the terminal.
There you go.
OK, so things to note here is we have a function based view and it's called simple view.
Then we connect it to the URLs, the profile that exists inside the application.
So I come to your URLs, that pie, which is inside my application.
And then in order to work with this, I say from.
Jingo the earl's import path, and I also need to connect it to the view, so I say from dot import
views where that means just look in the same folder you're currently in because you are all that pine
views appear both under first app.
Next, I create a list called URL Patterns, and it should always be called this.
And then inside this list is where we begin to add those paths and recall we discussed this function.
We have path it takes in that route should be a string.
Then it takes in a view that should be a something from views.
That pie right now is just going to be a function from it.
And then later on, we can add in keyword arguments and we can actually name that.
We'll discuss that later on.
But for right now, we're just going to use a path, and I set this as an empty string.
The reason I set this as an empty string right now is because in my project level, URLs that pi file,
that's where I'm actually going to define what these views are going to fall under.
So essentially, what's going to happen is I'll leave this as an empty string and then I'll connect
it to views, stop simple view, and then I save those changes.
Then I go to my project level URL configuration and then add in another path here.
So say path and then we define this as first app or really whatever you want to attach to these.
And this is going to be the route.
And then what I need to do is just connect it to include the URL for pie.
And then that means I need to import include.
And then inside a path, I say include.
And then I'm going to type out first underscore app that your URLs.
OK, so that's technically everything we need to do, but let me review what's actually happening here.
So it starts off in views that pie, that's probably easiest part to understand.
It's just a function you call the function whenever you want.
It takes in a request for now and then returns some sort of HTP response.
Simple view later on.
In the future, this will actually connect to a template HTML file, and we'll be sending back specific
HTML files.
And then later on, we'll actually use Jinja templating to insert stuff into this HTML file.
But for right now, we're just dealing with a very simple HTP response that's going to display the text
on the page.
Simple view.
Now you may be wondering, where does this simple view actually show up on the website?
So what web page is this going to?
And what's sometimes a little confusing at first is if we just leave it blank, you might think it's
going to the homepage, but that's actually not true.
These URL patterns are going to be added to whatever was defined on a project level and on a project
level.
We defined it as first app.
So that means when you go back to your app, all these URL patterns are going to have first app attached
to them before you add in whatever is here on the path.
So if I said here in the path.
Simple view, then, this would show up at, let's just say, your domain.
Com Whatever it happens to be called forward slash first step and then forward slash.
Simple.
View.
If I were to take this away.
And leave that as just a simple string, then it's just going to show up at domain the comps first.
So let's actually try this.
I'm going to run the server and we'll experiment with displaying this on different URL routes.
So going to come back to you, jingle lectures, I'm going to CD into my site and then I run Python.
Manage that pie, run the server.
OK, so that is running running on Jenga four.
And let me bring in my browser here.
So recall from the last section, if you just go to one two seven zero zero 8000, you should see this
debug 404 error.
And the reason for that is because it's actually telling you quite clearly there was only two URL patterns
to find that the project level URLs that PY file now was the admin and the first app.
So this empty path didn't match any of these.
But let's go to first app and see what happens.
We're going to say forward slash first app enter.
And now we have simple view as expected.
So we're going to do is experiment with this a bit.
Recall, I mention that we have URL patterns here that then is defined on top of whatever is included
with this project level.
So that means if I come back here and say this path is now simple, view and save those changes, Django
should automatically restart for you upon saving those changes going to come back here.
Now I'm going to refresh this and note now I get Hey, page not found for this.
And then first up, and here's what's really cool it begins to define Hey, I only found admin and then
I only found first app.
And within that first app, I only found simple view.
So that current path, this first app doesn't match any of these.
So if I go to first app forward slash simple underscore view and enter, then I see simple view.
So hopefully this helps you with not just understanding your URLs and how they actually connect to the
roots for the views, but also this super useful 404 debug page.
Now, later on, we'll discuss how to not show this to the user.
You're seeing this because we have technically debug is equal to true inside settings, not pie.
Later on, we would change that to false, and it's going to display a standard for a four page or some
actual specific core.
For that, we define using a template.
But we should keep debug right now in order to understand what's going on for our website so we can
see right now because I change this and you are supposed to be simple view.
I basically broke that connection to just first app.
Now you may be wondering, Well, I want something to show on my actual home page right here, which
would eventually be replaced with domain BCom.
Whatever your website happens to be, how could I actually do that?
There's lots of different ways you could do that.
Sometimes one of the easiest ways is we go to the project level.
It actually tells you, Hey, if you have some function based view, you can just import it from views
and then set path use home name is called home.
So what I could do is inside a my site level is create and you've used our pie file and use that just
to define the home view.
Or I could even create that function in here and make that of you kind of depends on what approach you
want to take.
So let me just show you what that would look like in the simplest case of a function view.
The absolute simplest case would be to even just define the home function inside you, where else that
pie again, kind of unusual, but I just want you to really make sure you understand how to use function
views or importing from your configuration.
Classmates views will come later.
But the key thing to understand is how does path function actually work?
So we're going to this practice this inside of your old pie.
I'll create.
Something called home view takes in a request, and then it's going to return and the type of response
that says.
Homepage now, the one thing I have to make sure is I actually import the response and I have imported
it already from Django, the HP response import HTP response.
So now I have home view here.
How would I connect that inside of your patterns?
Pass the video and see if you can actually figure this out right now.
It's going to be slightly different than what we've been doing with include.
And if you're able to figure this out, it means you're able to basically read and understand these
directions.
So try your best.
OK, I'll show you how to do it now.
We're going to connect it by adding in.
A new path.
I'm going to leave it blank, so this will be at the index and the recall, I need to connect it to
the view.
Now I don't need to import this view.
It's right here in the same file.
So simply just going to say home underscore view.
Hopefully you're able to figure that out and then try to do views that home view because this is all
thin.
You are all stock pie.
And if you come back here, you could also give it a name, but that's actually not necessary.
So we're just going to save this.
And then let's go back to our homepage here and now we're going to refresh.
And now we see home page.
So again, this is all being done within the URLs.
That pie file a little unusual, but it's definitely common for just helpfully starting up a website
really quickly and making sure your home page lands to just return something inside you.
Stop pie.
But clearly, I could have made another separate maybe indexed that pie file at home pie file or used
a pie file that could have been on my site for project level things.
Again, just doing it here.
Let's go ahead and try following the exercise of doing it through views.
So I will delete it here.
I will come back to my site or create a new file.
Called views that pie.
And now this is kind of project level views, and I'm going to have a very simple one.
I'm going to copy and paste this line.
Save that.
Going to post it in here.
And then we'll say the F.
Home view takes in a request.
And then return that HIV response.
And this will be on view.
I'm going to rename it home for you instead of home page to make sure it's working.
And then I need to import this file into your stockpile.
So I'll come back to Earth up high and then I can pretty much almost follow exactly what's going on
here.
So we say from my app import views, but here I can just say from dot import views and then this home
view will now just be views, dots, home view.
Let's make sure that matches up with what I wrote here.
This is the home view.
I should be able to run this, save this, have it reload.
And then I'm going to come back to my website, refresh this and now I see home view.
OK, so clearly this should now help your understanding of connecting views through imports.
The one part where it can get tricky is you have a lot of files that end up sharing similar names.
You get a bunch of used up files, you get a bunch of URLs that pi files and sometimes a lot of debugging.
It's just, Oh, I import from the right place, or am I calling the right file?
So that does take a little bit of time to adjust.
And that's why separating things out into multiple applications is sometimes really helpful.
OK, so I have been able to create my first app and now I understand these neural patterns within that.
Then again, if I delete this, then this will just show up within the home for the first app.
So now if I come here and say forward slash first app, enter I.
Now I get a simple view.
That's the very basics of URLs, routing and views.
We'll talk about dynamic views and more complex views like View Logic in the next lecture.
I'll see you there.