Your first Django project!

Part of this chapter is based on tutorials by Geek Girls Carrots (https://github.com/ggcarrots/django-carrots).

Parts of this chapter are based on the django-marcador tutorial licensed under the Creative Commons Attribution-ShareAlike 4.0 International License. The django-marcador tutorial is copyrighted by Markus Zapke-GrΓΌndemann et al.

We're going to create a small blog!

The first step is to start a new Django project. Basically, this means that we'll run some scripts provided by Django that will create the skeleton of a Django project for us. This is just a bunch of directories and files that we will use later.

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.

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:

β”œβ”€β”€ manage.py
β”œβ”€β”€ mysite
β”‚   β”œβ”€β”€ asgi.py
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ settings.py
β”‚   β”œβ”€β”€ urls.py
β”‚   └── wsgi.py
└── requirements.txt

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.

The settings.py file contains the configuration of your website.

Remember when we talked about a mail carrier checking where to deliver a letter? urls.py file contains a list of patterns used by urlresolver.

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).

In settings.py, find the line that contains TIME_ZONE and modify it to choose your own timezone. For example:

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 = 'en-us'

Since 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='PASTE_YOUR_KEY_HERE_BETWEEN_THE_QUOTES'
    

And we're done! Time to start the web server and see if our website is working!

Starting the web server

Since you are using Glitch, the web server will restart automatically every time you change a file. You can also disable this behaviour and force a manual restart :

Glitch.com terminal

$ refresh

Now you need to check that your website is running. Open your browser (Firefox, Chrome, Safari, Internet Explorer or whatever you use) and enter this address:

https://name-of-your-glitch-project.glitch.me

Congratulations! You've just created your first website and run it using a web server! Isn't that awesome?

Install worked!

Ready for the next step? It's time to create some content!

results matching ""

    No results matching ""