text
stringlengths
1
81
start
float64
0
10.1k
duration
float64
0
24.9
Then here in the template, I'm looping over each one of those flights.
4,053.52
3.3
For each one, printing out a list item where
4,056.82
2.91
I can access properties of that flight.
4,059.73
1.87
Say flight this ID from origin to a particular destination.
4,061.6
7.07
So now, I'll go ahead and go into my terminal.
4,068.67
2.72
Run python manage.py run server, which again,
4,071.39
3.15
is how we run a Django web application.
4,074.54
2.19
And now, if I go to that URL, flash flights this time,
4,076.73
3.21
because that's the URL.
4,079.94
1.2
What I see is exactly what I'd expect to see.
4,081.14
2.19
An unordered list that just so happens to have flight one New York to London
4,083.33
5.58
displayed there.
4,088.91
0.8
It is taking data from my database, and now displaying it inside
4,089.71
3.49
of this template.
4,093.2
0.95
And if I were to add new flights, it would also update on this page as well.
4,094.15
4.249
So if I go ahead and go back, go into the shell, python manage.py shell.
4,098.399
4.221
I'll go from flights.models import star.
4,102.62
3.569
Let's go ahead and--
4,106.189
2.34
well, all right, let's add a flight from Shanghai to Paris, for example.
4,108.529
3.04
Well, how do I get the airports for Shanghai and Paris?
4,111.569
3.051
Well, it turns out that if I want to get Shanghai,
4,114.62
2.71
I can say Shanghai equals airport.objects.
4,117.33
5.029
and then I can say--
4,122.359
1.201
if I do airport.objects.all, that gets me all of the airports, for example.
4,123.56
5.259
Oh, and it seems I don't actually have a Shanghai one,
4,128.819
2.511
but I can add one if I wanted to.
4,131.33
1.81
But if I do airport.objects.all, that, again, gives me all of them.
4,133.14
3.99
But if I want to filter my airports list, not get all of the airports
4,137.13
3.17
but just get some of them, I can say airport.objects.filter, and I can say,
4,140.3
5.82
get me all the airports where the city is New York, for example.
4,146.12
6.409
And that is going to go ahead and give me a query set that only
4,152.529
2.921
contains the results that I care about.
4,155.45
2.07
So again, airport.objects.filter lets me constrain the results that come back.
4,157.52
4.56
Not get me all of the airports, but only get me
4,162.08
2.46
airports whose city is New York, for example.
4,164.54
2.7
And it is only giving back one, so I could say .filter city equals New
4,167.24
3.3
York.first, to say, take that query set, and just get me the first and only
4,170.54
4.529
thing in that query set.
4,175.069
1.201
And that gives me airport New York.
4,176.27
2.72
A simplified way of doing the same thing.
4,178.99
1.72
If you know you're only going to get one result back, is I
4,180.71
2.85
can say something like airport.objects.get,
4,183.56
2.1
which will only get one result if it knows that there's only going to be
4,185.66
4.35
one airport with the city of New York.
4,190.01
2.25
That too will return to me New York JFK airport.
4,192.26
4.38
But it will throw an error if ever there's more than one,
4,196.64
2.38
or if there's none, for example.
4,199.02
1.76
So we'll go in and save that inside of JFK,
4,200.78
2.07
and we'll go ahead and create a flight that is going
4,202.85
2.64
from New York to Paris, for example.
4,205.49
2.39
I can do CDG equals airport.objects.get.
4,207.88
3.696
City equals Paris.
4,211.576
1.774
And now, I have this variable CDG, which represents the airport Paris.
4,213.35
4.02
And if I want to create a new flight that goes from New York to Paris,
4,217.37
4.02
I can say F is going to be a flight whose
4,221.39
2.19
origin is JFK whose destination equals CDG and whose duration equals 435.
4,223.58
6.8
And I can save that flight as well.
4,230.38
2.72
And so I've added a new flight.
4,233.1
1.87
And so now, if I run the server, python manage.py,
4,234.97
3.73
run server, refresh the page, I now see that I have two flights.
4,238.7
4.9
One flight that's going from New York to London, one flight that's
4,243.6
2.75
going from New York to Paris.
4,246.35
1.98
But of course, it's going to be pretty annoying if every time I
4,248.33
2.76
want to update the data, adding new data, manipulating the data,
4,251.09
3.18
I need to go into the shell in order to run
4,254.27
2.55
direct commands that are able to add new flights,
4,256.82
2.13
add new airport, so on and so forth.
4,258.95
1.86
What I'd really like to be able to do is just very simply
4,260.81
3
to add it via a web interface.
4,263.81
1.828
Via the web, be able to say, all right, let
4,265.638
1.792
me add a new flight that goes from location 1 to location 2.
4,267.43
3.82
And it's possible, using the information we know now,
4,271.25
2.64
to build a web page that does just this.
4,273.89
2.73
But Django is built on this idea that it doesn't want you, the programmer,
4,276.62
3.84
to have to repeat work that other people have already done.
4,280.46
3
And this process of trying to define models and very quickly be
4,283.46
3.63
able to create and edit and manipulate models
4,287.09
2.07
is so common that Django has already built for us an entire app that is just
4,289.16
5.07
designed for the manipulation of these models,
4,294.23
2.73
and it's known as the Django admin app.
4,296.96
3.06
And this is an app that we've seen traces of already,
4,300.02
2.49
that if we remember that urls.py file from inside of our application.
4,302.51
6.15
We saw that we added a path for our own app,
4,308.66
2.94
but there was already a path given to us by default, /admin,
4,311.6
3.96
that takes us to the admin app as well.
4,315.56
3.37
And so in order to use the admin app, we need
4,318.93
2.54
to create an administrative account inside of our Django web application.
4,321.47
4.29
And the way to do that is via the command line.
4,325.76
2.34
I can run python manage.py create super user.
4,328.1
4.775
It's going to ask me for my name.
4,332.875
1.375
I'll go in and type in my user name, my email address,
4,334.25
3.77
and it's also going to ask for a password.
4,338.02
1.75
I can just make up a password that I would like to use.
4,339.77
2.79
Retype it in just to confirm it.
4,342.56
1.86
And now, Django has created a super user account for me in this web application
4,344.42
5.61
so that I, using these credentials, have the ability
4,350.03
2.91
to visit the web interface for the admin app
4,352.94
2.46
and actually manipulate some of these underlying models.
4,355.4
4.12
So in order to do this, the first thing I need to do is take my models
4,359.52
3.83
and add those models to the admin app.
4,363.35
2.43