![Show Menu](styles/mobile-menu.png)
![Page Background](./../common/page-substrates/page0189.png)
I’m using a
relative import
(
from .secret key
instead of
from secret_key
) to
be absolutely sure we’re importing the local module, rather than one from
somewhere else on
sys.path
. I’ll talk a bit more about relative imports in the
next chapter.
Other people, such as the eminent authors of the excellent
Two Scoops of Django, suggest using environment variables to set things like se‐
cret keys; you should use whatever you feel is most secure in your
environment.
Next we create or update the virtualenv:
deploy_tools/fabfile.py.
def
_update_virtualenv
(
source_folder
):
virtualenv_folder
=
source_folder
+
'/../virtualenv'
if
not
exists
(
virtualenv_folder
+
'/bin/pip'
):
#
run
(
'virtualenv --python=python3
%s
'
%
(
virtualenv_folder
,))
run
(
'
%s
/bin/pip install -r
%s
/requirements.txt'
%
(
#
virtualenv_folder
,
source_folder
))
We look inside the virtualenv folder for the
pip
executable as a way of checking
whether it already exists.
Then we use
pip install -r
like we did earlier.
Updating static files is a single command:
deploy_tools/fabfile.py.
def
_update_static_files
(
source_folder
):
run
(
'cd
%s
&& ../virtualenv/bin/python3 manage.py collectstatic --noinput'
%
(
#
source_folder
,
))
We use the virtualenv binaries folder whenever we need to run a Django
manage.py
command, to make sure we get the virtualenv version of Django, not
the system one.
Finally, we update the database with
manage.py migrate
:
deploy_tools/fabfile.py.
def
_update_database
(
source_folder
):
run
(
'cd
%s
&& ../virtualenv/bin/python3 manage.py migrate --noinput'
%
(
source_folder
,
))
Breakdown of a Fabric Script for Our Deployment
|
161