This code is used to power the snippet sharing site, djangosnippets.org
- Python version 3.11
- PostgreSQL
Clone the repo:
https://github.com/django/djangosnippets.org.git
Create your virtual environment:
python -m venv venv
Activate in Linux:
source venv/bin/activate
Activate in Windows:
venv\Scripts\activate
Connect to PostgreSQL
Connect in Linux:
psql -U $(whoami) -d postgres
Connect in Windows:
psql -U postgres
Create a PostgreSQL database and role:
postgres=# CREATE DATABASE djangosnippets; postgres=# CREATE USER djangosnippets WITH SUPERUSER PASSWORD 'djangosnippets'; postgres=# GRANT ALL PRIVILEGES ON DATABASE djangosnippets TO djangosnippets;
Exit psql shell:
postgres=# exit
Install requirements:
pip install -r requirements/development.txt
Copy .env.template.local file, rename to .env and configure variables for your local postgres database.
Copy in Linux:
cp .env.template.local .env
Copy in Windows:
copy .env.template.local .env
Run migrations and create superuser:
Migrate:
python manage.py migrate
Optionally load data first:
python manage.py loaddata fixtures/cab.json
Create superuser:
python manage.py createsuperuser
Install tailwind (npm is required):
python manage.py tailwind install
Run server locally:
python manage.py runserver_plus
Run tailwind in another terminal locally:
python manage.py tailwind start
Using Docker allows you to set up the development environment more quickly if Docker is installed 🐳
Build the Docker images:
docker compose -f docker-compose.local.yml build
Start the containers:
docker compose -f docker-compose.local.yml up -d
Go to: http://127.0.0.1:8000/ and enjoy 🙌
You need to copy .env.example to .env and configure to your needs. The example is fine to start with development.
You may wish to use docker locally for production dependency testing and development; here are the setup instructions:
$ docker-compose -f docker-compose.production.yml build $ docker-compose -f docker-compose.production.yml up -d
-d denotes running docker in a detached state:
$ docker-compose -f docker-compose.production.yml run web python manage.py migrate $ docker-compose -f docker-compose.production.yml run web python manage.py createsuperuser $ docker-compose -f docker-compose.production.yml run web python manage.py loaddata fixtures/cab.json $ npm run build $ docker-compose -f docker-compose.production.yml run web python manage.py collectstatic
The docker setup is running as close as possible to the production setup in Heroku:
Postgres 12.3 Gunicorn Redis
To run our tests with docker:
$ docker-compose -f docker-compose.yml run web python manage.py test --settings=djangosnippets.settings.testing
To run tests:
$ python manage.py test --settings=djangosnippets.settings.testing
DjangoSnippets uses the Foundation framework as the core of its visual style. To get this working on your local machine you need compass and bower to compile your stylesheets. Please never modify the generated .css files directly. Use the .scss ones instead.
To keep the setup path as short as possible, run the following commands in your terminal:
$ cd djangosnippets/static $ bower install && compass watch
If you don't have either of these two installed, you can find detailed instructions on their respective websites.
Please make sure that you commit only a compressed version of the CSS file as this is what will be deployed. (In order to do that the default configuration inside djangosnippets/static/config.rb is output_style = :compressed)
The production setup is currently tailored to Heroku and, therefore, mostly automatic. The difference between these two setups is configured in the djangosnippets.settings.production module and the requirements.txt file.