We are only dealing with a single setup file The file is generated by the startproject command. But when you're ready to configure it, you'll find that you need multiple configuration files to keep your development environment separate from your product environment. For example, you may not want to change DEBUG from False to True every time you test a change in your code on your local machine, but Django makes it easy to avoid this by using multiple configuration files.
If you want to organize your configuration files by product settings and development settings, you can do this in one of the following three ways.
- Set up as two comprehensive, independent profiles of each other
- Set up a basic profile (e.g., for development) and a second (for product) profile, the second profile simply imports the configuration from the basic one and rewrites what needs to be defined.
- Use a separate configuration file that contains a Python logical judgment to change settings based on the context.
We'll explain each of these in turn.
The first and most basic way to do this is to define two separate configuration files. If you are following the previous example, you already have one, now you just need to make a copy of it and name it settings_production.py (the filename can be defined to your liking), change the DEBUG and other settings in this new file.
The second method is more similar, but with much less redundancy. As an alternative to using two configuration files with mostly the same content, you can use one file for the base file and another file to import the relevant settings from the base file. For example
# DEBUG = True TEMPLATE_DEBUG = DEBUG DATABASE_ENGINE = 'postgresql_psycopg2' DATABASE_NAME = 'devdb' DATABASE_USER = '' DATABASE_PASSWORD = '' DATABASE_PORT = '' # ... # settings_production.py from settings import * DEBUG = TEMPLATE_DEBUG = False DATABASE_NAME = 'production' DATABASE_USER = 'app' DATABASE_PASSWORD = 'letmein'
Here, settings_production.py imports all the settings from and simply redefines the settings that require special handling in product mode. In this case, DEBUG is set to False, but we've set different database access parameters for the product schema. (The latter will show you that you can redefine any setting, not just basic ones like DEBUG.)
Ultimately, the most streamlined solution to reach two configured environment settings is to use a single configuration file where settings are made according to the different environments. One way to accomplish this is to check the current hostname. Example:
# import socket if () == 'my-laptop': DEBUG = TEMPLATE_DEBUG = True else: DEBUG = TEMPLATE_DEBUG = False # ...
Here, we import the socket module from the python standard library and use it to check the hostname of the current system. We can verify that the code is running on the product server by checking the hostname.
A key is that configuration files are simply files that contain python code. You can import this python code from other files, and you can perform arbitrary logical judgments and other operations with this code. If you are going to follow this scenario, make sure that the code in these configuration files is safe (bulletproof) enough. If this configuration file throws any exceptions, Django has the potential to crash very badly.
rename
Feel free to rename yours to settings_dev.py or settings/ or, Django doesn't care what you name your config file as long as you tell it which one you're using.
But if you do rename the file created by the startproject command, you'll find that it gives you an error message saying that it can't find the configuration file. That's because it's trying to import a module called settings from this file. You can do this either by modifying the file to import your own module instead of importing settings, or by using instead of using, in the latter case, the DJANGO_SETTINGS_MODULE environment variable. python path. (e.g. '').
DJANGO_SETTINGS_MODULE
After going through the code changes in this way, the next part of the chapter will focus on the directives needed for releases to specific environments (such as Apache). These directives are different for each environment, but one thing is the same. In each environment, you need to tell the web server what your DJANGO_SETTINGS_MODULE is, which is the entry point for your Django application. DJANGO_SETTINGS_MODULE points to your configuration file, which points to your ROOT_URLCONF, which points to your views, and so on.
DJANGO_SETTINGS_MODULE is the path to your configuration file's python For example, assuming mysite is in your Python path, DJANGO_SETTINGS_MODULE for the example we're working on is ''.