17
loading...
This website collects cookies to deliver better user experience
SECRET_KEY
or anything related to database setup should never be placed directly in the Django settings file and in the version control. These are sensitive data and you should safely keep them out of anyone's reach who's not supposed to see it..env
file and keep all your variables there and import them into your settings file using the API of django-environ package. And keep this .env
file out of version by placing it in the .gitignore
file. But you should keep an .env.example
file in the version control that contains a template of the original .env
file.local_settings.py
file and keeping out of version control, you are probably doing it wrong and there are many valid reasons for that.__init__.py
file that will go to a settings directory replacing the settings.py
file. The base.py
settings file will hold all the settings that don't need to change and the development, test, stage and production settings files containing all the environment specific settings with start with importing all the settings from the base.py
file as from .base import *
.manage.py
will be pointing to the older settings file and it'll no longer work. In this case you won't be able to run your development server along with any commands with manage.py
. To solve this, you can create a new environment variable called WORK_ENV
in your .env
file and a new engage.py
file in the settings directory which will contain -from .base import *
working_environment = env.str("WORK_ENV", default="development")
if working_environment == "production":
from .production import *
elif working_environment == "stage":
from .stage import *
elif working_environment == "test":
from .test import *
else:
from .development import *
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'your_project.settings')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "your_project.settings.engage")
settings.py
file. And now, with the split up settings file and the .env
file, it should look like -|--settings
|-- __init__.py
|-- base.py
|-- engage.py
|-- development.py
|-- production.py
|-- stage.py
|-- test.py
|-- .env
|-- .env.example
urls.py
files. You can also choose between two different looks.python manage.py shell
in your virtual environment and you're inside the Django shell. This can come in handy for testing different ORM queries with your defined app models as well as trying out APIs of third party packages.manage.py
for doing tasks. For example, you might need to insert data into your database from a csv file or a daily cron job for doing something that is related to the database. You can't access the database with the Django ORM from outside the application [for example, a cron job]. But if you define your task in a management command, you can call it from anywhere with the Python from that project virtual environment using python manage.py your_command
.virtualenv
and a pip-freeze
command. But the issue that comes up with this is that it exports all the package names that are in your virtual environment. There can be many dependencies of a specific package and if you want to upgrade one, you'll need to update all of it's dependencies manually as well along with it. Also, there are packages that you need only in development like black
, pylint
or django-debug-toolbar
that you definitely don't need in production. To deal with all these, there is an awesome package called pip-tools.requirements.in
file and when you compile this file, it'll generate a nicely formatted requirements.txt
file that will have all the dependencies with notes for which one of them is coming from which specific package. You can also pin your development packages in dev-requirements.in
file and keep your development tools separate.ModelAdmin
from django.contrib.admin
but if you use it with a custom user model, there is an issue. And if you are not already aware, defining the custom user model should be something that you should do as one of the first things when you start a new Django project as suggested by the Django documentation itself.ModelAdmin
with a custom user model is that your user create form and user edit form will present you with password input fields where you'll need to enter the password in the hashed format itself. If you enter yourawesomepassword123 as your password and save the user, that user won't be able to log in with yourawesomepassword123 because that's not the actual stored password. The actual password would be the value that's is yourawesomepassword123 after hashing.UserCreationForm
and UserChangeForm
from django.contrib.auth.forms
and use them in the admin that will be based on UserAdmin
from django.contrib.auth.admin
. You can find all about how to do it on this awesome tutorial. This way you will have the freedom of creating new users from the Django admin with ease.