Each site (staging, live, or any other website) has its own folder. Within that we have a
separate folder for the source code, the database, and the static files. The logic is that,
while the source code might change fromone version of the site to the next, the database
will stay the same. The static folder is in the same relative location,
../static
, that we set
up at the end of the last chapter. Finally, the virtualenv gets its own subfolder too. What’s
a virtualenv, I hear you ask? We’ll find out shortly.
Adjusting the Database Location
First let’s change the location of our database in
settings.py
, and make sure we can get
that working on our local PC. Using
os.path.abspath
prevents any later confusion
about the current working directory:
superlists/settings.py (ch08l003).
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import
os
BASE_DIR
=
os
.
path
.
abspath
(
os
.
path
.
dirname
(
os
.
path
.
dirname
(
__file__
)))
[
...
]
DATABASES
=
{
'default'
: {
'ENGINE'
:
'django.db.backends.sqlite3'
,
'NAME'
:
os
.
path
.
join
(
BASE_DIR
,
'../database/db.sqlite3'
),
}
}
[
...
]
STATIC_ROOT
=
os
.
path
.
join
(
BASE_DIR
,
'../static'
)
Now let’s try it locally:
$
mkdir ../database
$
python3 manage.py migrate --noinput
Creating tables ...
[...]
$
ls ../database/
db.sqlite3
That seems to work. Let’s commit it:
$
git diff
# should show changes in settings.py
$
git commit -am "move sqlite database outside of main source tree"
To get our code onto the server, we’ll use Git and go via one of the code sharing sites. If
you haven’t already, push your code up to GitHub, BitBucket, or similar. They all have
excellent instructions for beginners on how to do that.
Here’s some bash commands that will set this all up. If you’re not familiar with it, note
the
export
command which lets me set up a “local variable” in bash:
elspeth@server:$
export SITENAME=superlists-staging.ottg.eu
elspeth@server:$
mkdir -p ~/sites/$SITENAME/database
Deploying Our Code Manually
|
141