Author set up to run with PostgreSQL, as a matter of taste/habit. You can just go with sqlite
, just modify the DB connection string as described in the Environment Variables
section below.
Set up a virtualenv
(author used pyenv
) and install the required packages:
pip install -r requirements.txt
To set environment variables, set them a newly-created .envrc
file (which can be set) both on your local env or production. The git
repo is configured to ignore this file. Caveats:
- it has to be located in the project's root directory
- it has to be loaded every time you enter the directory as all Django commands below expect the env variables in it to be loaded
- you can use an automated tool you like for this, author is biased towards direnv
The mandatory variable that needs to be set is the database URL for dj-database-url. PostgreSQL example:
DATABASE_URL="postgres://user:password@localhost:port/imgclickerdb"
Run the below 4 commands (one per line):
- to create tables
- to load items data from JSON file
- to create superuser with which to view data via admin
- to run the server and access
/admin/
with your newly-created superuser credentials
./manage.py migrate
./manage.py runscript load_items
./manage.py createsuperuser
./manage.py runserver
Command:
$ ./manage.py test
Example output:
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
...
----------------------------------------------------------------------
Ran 3 tests in 0.007s
OK
Item.title
was set to have amax_length
of128
characters as the longesttitle
in the current data is74
characters long.- Django
URLField
is being used for images, instead ofImageField
because the spec does not state that the admin is required to manage image uploads. Hence going with the "simplest solution that can possibly work". - Email recipient when clicking image is unset, hence set
[email protected]
as default recipient (which should then be replaced by user, until this part is spec'ed out) - No paging on home page list (only 6 items right now).
- Added logic to add
/preview
where it's missing on the object'simage_url
field.
To initialise the website for Docker:
- ensure any required environment variables are loaded in the host machine's environment variables (i.e. any variables in
.direnv
)
And then:
docker-compose up # start the containers
docker-compose exec db sh # log onto "db" docker container
Once logged in log onto psql
using psql -U postgres
and run:
CREATE DATABASE imgclickerdb OWNER postgres;
Exit both psql
and the db
docker container.
Enter the web container at:
docker-compose exec web sh
And once logged in run:
./manage.py migrate
./manage.py runscript load_items
./manage.py createsuperuser
Refresh the site at http://localhost:8000/
and it should work.
Including dependencies, after code change, assuming container name web
:
docker-compose up -d --no-deps --build web
I.e. command:
docker-compose up -d --no-deps --build [container-name]
Source link.
docker-compose run --service-ports web
Where again web
is the container's name.
Source repo example here