2015-09-18 19:22:55 +03:00
Getting Up and Running Locally
==============================
2024-10-05 22:20:29 +03:00
.. index :: PostgreSQL
2015-09-19 00:26:29 +03:00
2015-09-18 19:22:55 +03:00
2018-03-09 21:17:56 +03:00
Setting Up Development Environment
----------------------------------
2015-09-18 19:22:55 +03:00
2018-03-09 21:17:56 +03:00
Make sure to have the following on your host:
2015-12-01 17:09:50 +03:00
2024-10-05 22:20:29 +03:00
* uv https://docs.astral.sh/uv/getting-started/installation/
2018-09-15 23:23:06 +03:00
* PostgreSQL_.
* Redis_, if using Celery
2020-04-27 05:31:46 +03:00
* Cookiecutter_
2018-03-09 21:17:56 +03:00
2024-10-05 17:29:57 +03:00
#. .. include:: generate-project-block.rst
2020-04-27 05:31:46 +03:00
2018-03-09 21:17:56 +03:00
#. Install development requirements: ::
2015-09-18 19:22:55 +03:00
2021-08-09 23:29:09 +03:00
$ cd <what you have entered as the project_slug at setup stage>
2024-10-05 22:20:29 +03:00
$ uv sync
2020-04-27 05:31:46 +03:00
$ git init # A git repo is required for pre-commit to install
2024-10-26 14:41:29 +03:00
$ uv run pre-commit install
2019-10-29 17:47:27 +03:00
2020-11-15 00:10:36 +03:00
.. note ::
2019-10-29 17:47:27 +03:00
2021-06-09 06:38:12 +03:00
the `pre-commit` hook exists in the generated project as default.
For the details of `pre-commit`, follow the `pre-commit`_ site.
2015-09-18 19:22:55 +03:00
2018-09-15 23:23:06 +03:00
#. Create a new PostgreSQL database using createdb_: ::
2015-09-18 19:22:55 +03:00
2022-02-20 16:21:06 +03:00
$ createdb --username=postgres <project_slug>
2022-12-14 00:32:57 +03:00
2022-02-20 16:21:06 +03:00
`` project_slug `` is what you have entered as the project_slug at the setup stage.
2015-09-18 19:22:55 +03:00
2018-09-15 23:23:06 +03:00
.. note ::
2018-09-29 14:24:28 +03:00
if this is the first time a database is created on your machine you might need an
`initial PostgreSQL set up`_ to allow local connections & set a password for
the `` postgres `` user. The `postgres documentation`_ explains the syntax of the config file
that you need to change.
2018-09-15 23:23:06 +03:00
#. Set the environment variables for your database(s): ::
2018-09-29 14:24:28 +03:00
$ export DATABASE_URL=postgres://postgres:<password> @127.0.0.1:5432/<DB name given to createdb>
2018-09-15 23:23:06 +03:00
.. note ::
Check out the :ref: `settings` page for a comprehensive list of the environments variables.
.. seealso ::
To help setting up your environment variables, you have a few options:
* create an `` .env `` file in the root of your project and define all the variables you need in it.
Then you just need to have `` DJANGO_READ_DOT_ENV_FILE=True `` in your machine and all the variables
will be read.
* Use a local environment manager like `direnv`_
2018-03-09 21:17:56 +03:00
#. Apply migrations: ::
2016-06-04 01:31:50 +03:00
2024-10-26 14:41:29 +03:00
$ uv run python manage.py migrate
2016-06-04 01:31:50 +03:00
2020-04-13 18:26:09 +03:00
#. If you're running synchronously, see the application being served through Django development server: ::
2018-03-09 21:17:56 +03:00
2024-10-26 14:41:29 +03:00
$ uv run python manage.py runserver 0.0.0.0:8000
2018-03-09 21:17:56 +03:00
2023-07-10 20:35:57 +03:00
or if you're running asynchronously: ::
2020-04-13 18:26:09 +03:00
2024-10-26 14:41:29 +03:00
$ uv run uvicorn config.asgi:application --host 0.0.0.0 --reload --reload-include '*.html'
2020-04-13 18:26:09 +03:00
2023-07-10 20:35:57 +03:00
If you've opted for Webpack or Gulp as frontend pipeline, please see the :ref: `dedicated section <bare-metal-webpack-gulp>` below.
2018-09-15 23:23:06 +03:00
.. _PostgreSQL: https://www.postgresql.org/download/
.. _Redis: https://redis.io/download
2020-04-27 05:31:46 +03:00
.. _CookieCutter: https://github.com/cookiecutter/cookiecutter
2018-09-15 23:23:06 +03:00
.. _createdb: https://www.postgresql.org/docs/current/static/app-createdb.html
2021-11-15 14:38:43 +03:00
.. _initial PostgreSQL set up: https://web.archive.org/web/20190303010033/http://suite.opengeo.org/docs/latest/dataadmin/pgGettingStarted/firstconnect.html
2018-09-15 23:23:06 +03:00
.. _postgres documentation: https://www.postgresql.org/docs/current/static/auth-pg-hba-conf.html
2021-06-09 06:38:12 +03:00
.. _pre-commit: https://pre-commit.com/
2018-09-15 23:23:06 +03:00
.. _direnv: https://direnv.net/
2018-03-09 21:17:56 +03:00
2016-06-04 01:31:50 +03:00
2024-03-29 12:48:14 +03:00
Creating Your First Django App
-------------------------------
After setting up your environment, you're ready to add your first app. This project uses the setup from "Two Scoops of Django" with a two-tier layout:
- **Top Level Repository Root** has config files, documentation, `manage.py` , and more.
- **Second Level Django Project Root** is where your Django apps live.
- **Second Level Configuration Root** holds settings and URL configurations.
The project layout looks something like this: ::
<repository_root>/
├── config/
│ ├── settings/
│ │ ├── __init__.py
│ │ ├── base.py
│ │ ├── local.py
│ │ └── production.py
│ ├── urls.py
│ └── wsgi.py
├── <django_project_root>/
│ ├── <name_of_the_app>/
│ │ ├── migrations/
│ │ ├── admin.py
│ │ ├── apps.py
│ │ ├── models.py
│ │ ├── tests.py
│ │ └── views.py
│ ├── __init__.py
│ └── ...
├── requirements/
│ ├── base.txt
│ ├── local.txt
│ └── production.txt
├── manage.py
├── README.md
└── ...
Following this structured approach, here's how to add a new app:
#. **Create the app** using Django's `` startapp `` command, replacing `` <name-of-the-app> `` with your desired app name: ::
2024-10-26 14:41:29 +03:00
$ uv run python manage.py startapp <name-of-the-app>
2024-03-29 12:48:14 +03:00
#. **Move the app** to the Django Project Root, maintaining the project's two-tier structure: ::
$ mv <name-of-the-app> <django_project_root>/
#. **Edit the app's apps.py** change `` name = '<name-of-the-app>' `` to `` name = '<django_project_root>.<name-of-the-app>' `` .
#. **Register the new app** by adding it to the `` LOCAL_APPS `` list in `` config/settings/base.py `` , integrating it as an official component of your project.
2018-03-09 21:17:56 +03:00
Setup Email Backend
-------------------
2016-06-04 01:31:50 +03:00
2023-09-06 22:41:01 +03:00
Mailpit
2018-03-09 21:17:56 +03:00
~~~~~~~
2016-06-04 01:31:50 +03:00
2023-09-06 22:41:01 +03:00
.. note :: In order for the project to support Mailpit_ it must have been bootstrapped with `` use_mailpit `` set to `` y `` .
2016-06-12 06:50:25 +03:00
2023-09-06 22:41:01 +03:00
Mailpit is used to receive emails during development, it is written in Go and has no external dependencies.
2016-06-12 06:50:25 +03:00
2018-03-09 21:17:56 +03:00
For instance, one of the packages we depend upon, `` django-allauth `` sends verification emails to new users signing up as well as to the existing ones who have not yet verified themselves.
2015-09-18 19:22:55 +03:00
2023-09-06 22:41:01 +03:00
#. `Download the latest Mailpit release`_ for your OS.
2016-01-09 02:40:19 +03:00
2023-09-06 22:41:01 +03:00
#. Copy the binary file to the project root.
2015-09-18 19:22:55 +03:00
2018-03-09 21:17:56 +03:00
#. Make it executable: ::
2015-09-18 19:22:55 +03:00
2023-09-06 22:41:01 +03:00
$ chmod +x mailpit
2015-09-18 19:22:55 +03:00
2018-03-09 21:17:56 +03:00
#. Spin up another terminal window and start it there: ::
2016-06-13 15:05:43 +03:00
2023-09-06 22:41:01 +03:00
./mailpit
2015-09-18 19:22:55 +03:00
2018-03-09 21:17:56 +03:00
#. Check out `<http://127.0.0.1:8025/>`_ to see how it goes.
2015-11-18 14:16:25 +03:00
2018-03-09 21:17:56 +03:00
Now you have your own mail server running locally, ready to receive whatever you send it.
2016-06-13 15:05:43 +03:00
2023-09-06 22:41:01 +03:00
.. _`Download the latest Mailpit release`: https://github.com/axllent/mailpit
2018-03-09 21:17:56 +03:00
Console
~~~~~~~
2023-09-06 22:41:01 +03:00
.. note :: If you have generated your project with `` use_mailpit `` set to `` n `` this will be a default setup.
2018-03-09 21:17:56 +03:00
Alternatively, deliver emails over console via `` EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' `` .
In production, we have Mailgun_ configured to have your back!
2015-09-18 19:22:55 +03:00
2015-09-19 00:26:29 +03:00
.. _Mailgun: https://www.mailgun.com/
2015-09-18 19:22:55 +03:00
2019-03-02 16:51:30 +03:00
Celery
------
2023-01-11 20:38:38 +03:00
If the project is configured to use Celery as a task scheduler then, by default, tasks are set to run on the main thread when developing locally instead of getting sent to a broker. However, if you have Redis setup on your local machine, you can set the following in `` config/settings/local.py `` ::
2019-03-02 16:51:30 +03:00
2019-06-18 22:50:17 +03:00
CELERY_TASK_ALWAYS_EAGER = False
2021-10-22 23:40:53 +03:00
2023-01-11 20:38:38 +03:00
Next, make sure `redis-server` is installed (per the `Getting started with Redis`_ guide) and run the server in one terminal::
2021-10-22 23:40:53 +03:00
2023-01-11 20:38:38 +03:00
$ redis-server
Start the Celery worker by running the following command in another terminal::
2024-10-26 14:41:29 +03:00
$ uv run celery -A config.celery_app worker --loglevel=info
2023-01-11 20:38:38 +03:00
That Celery worker should be running whenever your app is running, typically as a background process,
so that it can pick up any tasks that get queued. Learn more from the `Celery Workers Guide`_ .
The project comes with a simple task for manual testing purposes, inside `<project_slug>/users/tasks.py` . To queue that task locally, start the Django shell, import the task, and call `delay()` on it::
2024-10-26 14:41:29 +03:00
$ uv run python manage.py shell
2023-01-11 20:38:38 +03:00
>> from <project_slug>.users.tasks import get_users_count
>> get_users_count.delay()
You can also use Django admin to queue up tasks, thanks to the `django-celerybeat`_ package.
2024-10-05 17:29:57 +03:00
.. _Getting started with Redis: https://redis.io/docs/getting-started/
2023-01-11 20:38:38 +03:00
.. _Celery Workers Guide: https://docs.celeryq.dev/en/stable/userguide/workers.html
.. _django-celerybeat: https://django-celery-beat.readthedocs.io/en/latest/
2019-03-02 16:51:30 +03:00
2023-07-10 20:35:57 +03:00
.. _bare-metal-webpack-gulp:
Using Webpack or Gulp
---------------------
2018-03-09 21:17:56 +03:00
2023-07-10 20:35:57 +03:00
If you've opted for Gulp or Webpack as front-end pipeline, the project comes configured with `Sass`_ compilation and `live reloading`_ . As you change your Sass/JS source files, the task runner will automatically rebuild the corresponding CSS and JS assets and reload them in your browser without refreshing the page.
2021-11-16 23:29:43 +03:00
2023-04-26 10:06:56 +03:00
#. Make sure that `Node.js`_ v18 is installed on your machine.
2021-11-16 23:29:43 +03:00
#. In the project root, install the JS dependencies with::
$ npm install
#. Now - with your virtualenv activated - start the application by running::
$ npm run dev
2023-07-10 20:35:57 +03:00
This will start 2 processes in parallel: the static assets build loop on one side, and the Django server on the other.
#. Access your application at the address of the `` node `` service in order to see your correct styles. This is http://localhost:3000 by default.
.. note :: Do NOT access the application using the Django port (8000 by default), as it will result in broken styles and 404s when accessing static assets.
2021-11-16 23:29:43 +03:00
.. _Node.js: http://nodejs.org/download/
.. _Sass: https://sass-lang.com/
.. _live reloading: https://browsersync.io
2018-03-09 21:17:56 +03:00
Summary
-------
2015-09-18 19:22:55 +03:00
2018-03-09 21:17:56 +03:00
Congratulations, you have made it! Keep on reading to unleash full potential of Cookiecutter Django.