Now that we have installed django in our virtual environment, it's time to start to develop codes with Django. We are in first_django_project directory and we have activated our virtual environment useing source .venv/Scripts/activate. Let's create main.py and import django there:
import django
django.setup()
At this point the structure of directories is as follows
Now run the main.py and see what happens
(.venv)
javad@DESKTOP-2GOSU3N MINGW64 ~/first_django_project
$ python main.py
If you have run the above you will see some errors are raised and the end of the errors you see this:
django.core.exceptions.ImproperlyConfigured: Requested setting LOGGING_CONFIG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
This error simply says that in order to run Django you need to set some configurations. In addition, it suggests that you have two options to configure Django:
- Define an environment variable DJANGO_SETTINGS_MODULE
- Call settings.configure()
First, we go with second option to see how it works. In order to configure settings we define a Settings in the main.py file.
import django
from django.conf import settings
class Settings:
LOGGING = None
LOGGING_CONFIG = None
FORCE_SCRIPT_NAME = None
INSTALLED_APPS = []
TEMPLATES = []
settings.configure(Settings, DEBUG=True)
django.setup()
Now run the python main.py and see that the program runs with no error.
Django Settings from Environment
We knew how to use settings.configure to configure django. Now, we want to know how to configure django by defining an environment variable DJANGO_SETTINGS_MODULE. In practice, we usually go with this approach as you will see in future lessons.
To go with this approach, first create a new folder and name it my_site. Then open my_site directory and add __init__.py file in it. This file is added to make the my_site a python packge. At this moment the structure of directories is as follows:
(.venv)
javad@DESKTOP-2GOSU3N MINGW64 ~/first_django_project
$ mkdir my_site
$ cd my_site
javad@DESKTOP-2GOSU3N MINGW64 ~/first_django_project/my_site
$ touch __init__.py
Then add settings.py inside my_site package with the following lines of codes:
LOGGING = None
LOGGING_CONFIG = None
FORCE_SCRIPT_NAME = None
INSTALLED_APPS = []
TEMPLATES = []
Now, go to top directory (first_django_project) and edit the main.py as follows:
import os
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_site.settings")
django.setup()
Now run the python main.py and see that the program runs with no error.
Clean Directory
In the next lesson, we are going to do something else an recreate the my_site directory. Therefore before starting the next lesson make sure to delete the my_site directory an all of its content. The directories sturcutre before starting the next lesson must look like this:
Summary
In this lesson, you learnt:
- To use Django we need to configure some settings
- There are two ways to configure django settings
The method we used in this lesson to configure Django is not what you will do in practice. In the next lesson, we are going to start a django project which will automatically generate some default settings for us.