hi there Serdar Yegulalp here for Info world at IDG today I'm going to
demonstrate setting up and working with a very simple web application written in
the Django framework for Python Django is one of pythons most popular web
frameworks it offers just about everything you'd need to put together a
powerful interactive website its biggest selling point is that it includes much
of the low-level plumbing and wiring you'd normally have to cobble together
yourself things like user accounts and permissions or database schema
management Django provides waits to do all these things if not automatically
then at least with minimal hassle to start off on our Django journey I've
created an empty project folder with a Python virtual environment set up an
activated in it Django is a complex application so it's best to confine it
to a virtual environment to keep it from cluttering up your default Python
installation to install Django you pip install it the way you would any other
Python package pip install Django to confirm it's been installed correctly
type Python - M Django version this should return the current version of
Django installed when you start with Django you'll first create what's called
a project this is an organizational unit that contains settings and database
configuration information for one or more apps or sites within it this way
you can have multiple sites that share common information and tooling but you
can always just create a project with only one site in it which is what we're
gonna do here to do this we use Django's built-in command-line tools so let's get
project started django - admins start project my site you'll notice we now
have a new directory named my site when we run start project it populates the
Django project directory in this case my site with some scaffolding for a new
project among the pieces included as managed by a generated script that lets
you run management functions on the site let's use that to create the first app
within the site called my app pi manage pi start app my app you'll notice we now
have another folder within the project named my app now before we can go any
further we have to perform what's called a database migration this is a piece of
internal management used by Django to make sure that the database used by
Django internally is consistent with the definition
we have for it we're going to go into detail about how this works later for
now we're just going to run what's called the migration script this will
make everything consistent so Python managed pi migrate now at this point we
can actually fire up our Django site and see what's there to do this we'll use
manage pies run server command PI manage PI run server if we navigate to the
site's default page we'll get a placeholder well that's fine
we don't have anything our own in there but at least we know this Django project
is working properly now we can start customizing the app we've created first
we're going to create a simple view and URL pattern a view is a function that
returns output to the browser interacting with the site views for an
application are stored in the view stop PI file inside that Applications folder
we're not going to do anything too fancy at first we just want to say hello world
when someone comes to the root of our application most view functions contain
a lot more logic than what you see here they also can call other functions or do
pretty much anything you want there just Python functions after all but for now
we're just going to keep it simple a URL pattern Maps a View function to a URL so
the functions triggered when someone goes to that URL the URLs for an
application are stored in the URLs PI file for that application note that we
have to add the URLs to invoke our function in two places in the app itself
and also in the larger enclosing project the project can just inherit all of the
apps URL settings so we only have to make one project level URL entry per app
this will save us some time now when we run the server and navigate to our newly
mapped URL we'll see the hello world response watch what happens when we go
to the project root nothing is mapped to it directly so we'll just get this error
page we could always map this app to the root of the site if we wanted to but I'm
just going to leave it like this for now next we're going to explore some of the
database management functions in Django a database in a Django project does more
than just provide data to the apps that use it it's also how Django is built in
user management system works for the sake of this demo we'll only be using
the administrative user that's setup by default but the users managed through
Django can be part of any app that you write
Django can work with a variety of common databases like MySQL or Postgres here
we're using SQLite included with Python which is perfect for demo project like
this and even works well for low traffic sites that don't experience a lot of
concurrency first we need to register our app with the project to use its
database to do this we just edit settings PI in the project next we have
to create what's called a model for our site a model describes the database
objects used in the site the tables the fields and the tables and so on right
now we're just going to have one table book with a couple of fields the name of
a book and the data was produced one more thing we'll need to do is register
the tables we've created with the Django admin system this way we can manage the
table directly in the admin page for our Django project to do this we just need
to edit admin dot PI for the application and insert a reference to our newly
created tables before we can do anything with our newly declared model though we
have to create what's called a migration this is a description of how to change
the database we have now which doesn't have any models in it to match the
models defined in our app and we have to do that in a way that doesn't break
everything else fortunately we don't have to write this ourselves Django does
it for us with the make migrations command in manage PI Python manage PI
make migrations my app when you run this you should see a new directory in my app
migrations with a file in it ending in 0 0 0 1
each migration each set of changes that we make to the database is recorded in a
separate sequentially numbered file so we can migrate smoothly between schema
changes now to apply these changes we use the sequel migrate function in
managed PI Python managed by sequel migrate my app 0 0 0 1 now
finally we apply all of the migration changes that we've specified Python
managed PI migrate my app now both the database and our description of it are
completely up to date with each other now we're going to make use of the data
in a couple of key ways first we're going to create an administrative user
for our site which is going to be stored in the database and which we will use to
perform administrative functions like adding data to the database itself to
create the admin user we'll use the manage tool Python managed
PI create super user you'll provide a username and password the latter of
which will not be displayed as you type it now with that user created we can go
to the project's admin screen log in as that user and modify our database to add
some data
once we've added a few records we can now change our application to use data
from the database in an interactive way
first we're going to add a route that takes in some data by way of the URL in
this case the idea of a book that we want to look for then we're going to
look up the book in question and return it to details in a webpage you'll notice
that the URL mapping we're creating for this looks different an item in brackets
is essentially a variable retrieved from that part of the URL the int means what
we retrieved should be interpreted as an integer and not as a string by default
variables returned from a URL portion or string each variable we harvest from the
URL gets passed along to the function associated with it in this case book ID
we then use the book object we've created in models to look up the book by
way of that ID and then use that to return data about it back to the user
one of the thing worth demonstrating here we don't have to Jam all of our
output back to the user into a string we can create a template a more freeform
way to generate a webpage and return the data to the user including the data
we've retrieved from the database templates in Django have very little
logic in them they're used mainly to display information we've already
processed but we're not doing very much processing here we're just displaying
the book information so this template is going to be pretty straightforward once
we've created the template we place it in the templates directory in our app
then we rewrite our function to use the render built-in in Django to take the
template and render it with the data that we're passing along to it in this
case the book object that we've looked up
what we've covered here is only the very basics of Django there's still a great
deal more than I can cover in the space of a single video but you should now
have some idea of how a Django project is put together maintained and organized
thanks for watching this video if you have questions please add a comment
below don't forget to subscribe to The IDG Tech Talk channel on YouTube and for
more Python tips be sure to follow us on Facebook YouTube and info worldcom