Tip:
Highlight text to annotate it
X
hello
now we are going to see how a simple Django application works
here in my home folder there is a Django project called “djangofun”
it’s pretty easy to create
a project using Django because we just use
a Python script that creates all the
needed files
urls.py, settings.py,
manage.py
in settings.py we’ve got
various variables
here is my email and
here is the
templates dir
in the templates dir we
have files
that are
templates :)
for example, border
it’s my main template it contains the HTML structure
and it contains blocks
the first block is “title”, {% block title %}
and we can replace it by anything we need
here is
another block
and here is “content” and “footer”
as you can see, it’s empty, but we expect that
we have these blocks in the other templates, otherwise
it will not be displayed
so
let’s see another template
this is one of the templates my functions use
so, in this template we import the main template
and we replace {% block title %}
by this
and we replace {% block content %}
we put here some text with this
variable
variables are from
context
you’ll see
one other template, it’s (originally spoken “one another” is not correct)
easy, too
but it uses the
jungle template sandwich (Wanted to keep the original interpretation of the YouTube speech recognition system. Of course, I actually said: Django template language)
for example this, ifequal
so, if
offset is 1
we print this text. Otherwise, we print this text.
So,
how it works
here
we have an application called “helloworld” and in “helloworld” we have
file views.py
in this file we have two functions
one of them uses this template, “current_time”, and
one of them uses
this template, “time_with_offset”
and
when it
renders the template, it gives the context
here the context contains only one
variable and here it contains two variables
offset and “dt”,
datetime
and of course it imports
all it needs
interesting file is urls.py in the root
of the project
here we import the functions we need
it’s from
djangofun.helloworld.views
it’s
pretty easy to create an
application
in a Django project. We just use
a Python script
this script automatically creates
all the needed files
so anyway
when the user asks time/
the web server root, slash, time
we use this function
and if he asks time, slash, plus,
slash, one or two digits
we use this function, hours_ahead, and
the function receives this number
as the second argument, because, as you can see,
it’s in the round brackets
so, the function
of hours_ahead receives this number
as the second argument
because the first argument
is always “request”
so let’s see how it works
here is the Django development server
pretty easy
you just type python manage.py runserver and it works :)
it’s all you need
so, let’s see
here we have “Page not found”. Why? because
it’s not in
the urls.py
but if we ask
server for /time/,
it works
and it
displays
the time
and we can also use this
and see the same time
tomorrow
or for example
the day after tomorrow
or after one hour
see, this is
pluralization, we don’t
print
digit 1, hours
we print “one hour”
pluralization
and
this works, but
this doesn’t, because we accept
only one or two digits
it doesn’t work, also
Django also recommends to
turn DEBUG to
False if it’s production, but it’s development
So this is how Django works. If we save some file,
Django development server
reloads, revalidating the models
and so on