What Is Django
What Is Django
When you're building a website, you always need a similar set of components: a way
to handle user authentication (signing up, signing in, signing out), a management
panel for your website, forms, a way to upload files, etc.
Luckily for you, other people long ago noticed that web developers face similar
problems when building a new site, so they teamed up and created frameworks
(Django being one of them) that give you ready-made components to use.
Frameworks exist to save you from having to reinvent the wheel and to help alleviate
some of the overhead when you’re building a new site.
Imagine a mailbox (port) which is monitored for incoming letters (requests). This is
done by a web server. The web server reads the letter and then sends a response with
a webpage. But when you want to send something, you need to have some content.
And Django is something that helps you create the content.
Imagine a mail carrier with a letter. She is walking down the street and checks each
house number against the one on the letter. If it matches, she puts the letter there.
This is how the urlresolver works!
In the view function, all the interesting things are done: we can look at a database to
look for some information. Maybe the user asked to change something in the data?
Like a letter saying, "Please change the description of my job." The view can check if
you are allowed to do that, then update the job description for you and send back a
message: "Done!" Then the view generates a response and Django can send it to the
user's web browser.
The names of some files and directories are very important for Django. You should not
rename the files that we are about to create. Moving them to a different place is also
not a good idea. Django needs to maintain a certain structure to be able to find
important things.
Remember to run everything in the virtualenv. If you don't see a prefix (myvenv) in
your console, you need to activate your virtualenv. We explained how to do that in
the Django installation chapter in the Working with virtualenv part. Typing myvenv\
Scripts\activate on Windows or source myvenv/bin/activate on Mac OS X or Linux will
do this for you.
command-line
(myvenv) ~/djangogirls$ django-admin startproject mysite .
django-admin.py is
a script that will create the directories and files for you. You should
now have a directory structure which looks like this:
djangogirls
├── manage.py
├── mysite
│ ├── asgi.py
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── myvenv
│ └── ...
└── requirements.txt
Note: in your directory structure, you will also see your myvenv directory that we
created before.
manage.py is a script that helps with management of the site. With it we will be able
(amongst other things) to start a web server on our computer without installing
anything else.
Let's ignore the other files for now as we won't change them. The only thing to
remember is not to delete them by accident!
Changing settings
Let's make some changes in mysite/settings.py. Open the file using the code editor
you installed earlier.
Note: Keep in mind that settings.py is a regular file, like any other. You can open it
from inside the code editor, using the "file -> open" menu actions. This should get you
the usual window in which you can navigate to your settings.py file and select it.
Alternatively, you can open the file by navigating to the djangogirls folder on your
desktop and right-clicking on it. Then, select your code editor from the list. Selecting
the editor is important as you might have other programs installed that can open the
file but will not let you edit it.
It would be nice to have the correct time on our website. Go to Wikipedia's list of time
zones and copy your relevant time zone (TZ) (e.g. Europe/Berlin).
mysite/settings.py
TIME_ZONE = 'Europe/Berlin'
A language code consist of the language, e.g. en for English or de for German, and
the country code, e.g. de for Germany or ch for Switzerland. If English is not your
native language, you can add this to change the default buttons and notifications from
Django to be in your language. So you would have "Cancel" button translated into the
language you defined here. Django comes with a lot of prepared translations.
If you want a different language, change the language code by changing the following
line:
mysite/settings.py
LANGUAGE_CODE = 'de-ch'
We'll also need to add a path for static files. (We'll find out all about static files and
CSS later in the tutorial.) Go down to the end of the file, and just underneath
the STATIC_URL entry, add a new one called STATIC_ROOT:
mysite/settings.py
STATIC_URL = '/static/'
mysite/settings.py
ALLOWED_HOSTS = ['127.0.0.1', '.pythonanywhere.com']
Note: If you're using a Chromebook, add this line at the bottom of your settings.py
file: MESSAGE_STORAGE =
'django.contrib.messages.storage.session.SessionStorage'
If you are hosting your project on Glitch.com, let us protect the Django secret key that
needs to remain confidential (otherwise, anyone remixing your project could see it):
First, we are going to create a random secret key. Open the Glitch terminal again, and
type the following command:
command-line
python -c 'from django.core.management.utils import
get_random_secret_key; \
print(get_random_secret_key())'
This should display a long random string, perfect to use as a secret key for your brand
new Django web site. We will now paste this key into a .env file that Glitch will only
show you if you are the owner of the web site.
Create a file .env at the root of your project and add the following property in it:
.env
# Here, inside the single quotes, you can cut and paste the random key
generated above
SECRET='3!0k#7ds5mp^-x$lqs2%le6v97h#@xopab&oj5y7d=hxe511jl'
Then update the Django settings file to inject this secret value and set the Django web
site name:
mysite/settings.py
import os
SECRET_KEY = os.getenv('SECRET')
And a little further down in the same file, we inject the name of your new Glitch
website:
mysite/settings.py
ALLOWED_HOSTS = [os.getenv('PROJECT_DOMAIN') + ".glitch.me"]
Set up a database
There's a lot of different database software that can store data for your
site. We'll use the default one, sqlite3.
This is already set up in this part of your mysite/settings.py file:
mysite/settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
To create a database for our blog, let's run the following in the
console: python manage.py migrate (we need to be in
the djangogirls directory that contains the manage.py file). If that goes
well, you should see something like this:
(myvenv) ~/djangogirls$ python manage.py migrate
And we're done! Time to start the web server and see if our website is working!
Now you need to check that your website is running. Open your browser and
enter this address:
http://127.0.0.1:8000/
If you're using a Chromebook and Cloud9, instead click the URL in the pop-up window
that should have appeared in the upper right corner of the command window where
the web server is running. The URL will look something like:
https://<a bunch of letters and numbers>.vfs.cloud9.us-west-2.amazonaws.com
or on Glitch:
https://name-of-your-glitch-project.glitch.me
Note that a command window can only run one thing at a time, and the command
window you opened earlier is running the web server. As long as the web server is
running and waiting for additional incoming requests, the terminal will accept new text
but will not execute new commands.
To type additional commands while the web server is running, open a new terminal
window and activate your virtualenv -- to review instructions on how to open a second
terminal window, see Introduction to the command line. To stop the web server, switch
back to the window in which it's running and press CTRL+C - Control and C keys
together (on Windows, you might have to press Ctrl+Break).