Complete Django for Production - Creating and launching the tech stack (Part 2/6)

Create a fully-fledged web app with django, redis, postgres, celery and more within only a few minutes. Learn why we use this scaffolding and how to use it to great success.

In this part, we will create and understand the following:

  • Fully functional and dockerized django application

  • Separate development and production environments

  • Additional assisting tools, also dockerized:

    • Celery for async tasks, with beat and flower

    • Redis for caching and as message broker

    • PostgreSQL database

Using code scaffolding to jumpstart the project

I was not the first one to develop a SaaS product. You are neither. Many have been before us and many are to follow. That’s a good thing. We leverage the work and knowledge of others to be better ourselves. Once we have learned enough, we will also give back.

What I’m talking about is code scaffolding or code generation. The base for most SaaS products is the same. What sets you apart is what you do with it.

Getting up and running

I’m sure you have installed all required dependencies while reading part 1, so let’s use them to create our base with cookiecutter-django. Fire up your terminal and paste the following:

cookiecutter https://github.com/cookiecutter/cookiecutter-django

Then, answer the questions like I did here, except for your own and your project’s name and domain (we will buy one later, no worries):

cookiecutter https://github.com/cookiecutter/cookiecutter-djangoYou've downloaded /Users/dacx/.cookiecutters/cookiecutter-django before. Is it okay to delete and re-download it? [yes]: yesproject_name [My Awesome Project]: Full Cycle Devproject_slug [full_cycle_dev]:description [Behold My Awesome Project!]: Full Cycle Dev landing appauthor_name [Daniel Roy Greenfeld]: Michael Brayerdomain_name [example.com]: fullcycledev.ioemail [[email protected]]: [email protected] [0.1.0]:Select open_source_license:1 - MIT2 - BSD3 - GPLv34 - Apache Software License 2.05 - Not open sourceChoose from 1, 2, 3, 4, 5 [1]: 1Select username_type:1 - username2 - emailChoose from 1, 2 [1]: 2timezone [UTC]: Europe/Viennawindows [n]: nSelect editor:1 - None2 - PyCharm3 - VS CodeChoose from 1, 2, 3 [1]: 1use_docker [n]: ySelect postgresql_version:1 - 152 - 143 - 134 - 125 - 116 - 10Choose from 1, 2, 3, 4, 5, 6 [1]: 1Select cloud_provider:1 - AWS2 - GCP3 - Azure4 - NoneChoose from 1, 2, 3, 4 [1]: 1Select mail_service:1 - Mailgun2 - Amazon SES3 - Mailjet4 - Mandrill5 - Postmark6 - Sendgrid7 - SendinBlue8 - SparkPost9 - Other SMTPChoose from 1, 2, 3, 4, 5, 6, 7, 8, 9 [1]: 1use_async [n]: nuse_drf [n]: nSelect frontend_pipeline:1 - None2 - Django Compressor3 - Gulp4 - WebpackChoose from 1, 2, 3, 4 [1]: 1use_celery [n]: yuse_mailpit [n]: yuse_sentry [n]: nuse_whitenoise [n]: yuse_heroku [n]: nSelect ci_tool:1 - None2 - Travis3 - Gitlab4 - Github5 - DroneChoose from 1, 2, 3, 4, 5 [1]: 1keep_local_envs_in_vcs [y]: ydebug [n]: n [SUCCESS]: Project initialized, keep up the good work!

Great work, we have now successfully initialized our project. You can find the source code right after code generation on github.

Note: I did not elaborate much on why we answered each question the way we did. That is it’s own topic. Subscribe here for free and join the discord server to not miss it!

We can now start up the application and instantly have the entire tech stack at our hand!

Head back to your terminal, cd into the project folder and execute:

docker compose -f local.yml up

The initial start-up will take a while. Then, your output should look like this:

docker compose -f local.yml up[+] Running 6/6 django 3.1s postgres 2.3s celerybeat 2.1s flower 2.1s docs 2.3s celeryworker 2.3s[+] Building 15.2s (12/25) docker:desktop-linux => [docs internal] load build context 0.0s => => transferring context: 3.85kB 0.0s => [docs python 1/1] FROM docker.io/library/python:3.11.6-slim-bullseye@sha256:a489c01efef7fe4fb 11.4s => => resolve docker.io/library/python:3.11.6-slim-bullseye@sha256:a489c01efef7fe4fb9d7fbd5f8c5e9 0.0s => => sha256:a489c01efef7fe4fb9d7fbd5f8c5e9ba3feea100702caa492ed2cc3b7884e4c8 1.65kB / 1.65kB 0.0s[...][+] Running 11/7 ✔ Network complete-django-for-production_default Created 0.0s ✔ Volume "complete-django-for-production_full_cycle_dev_local_postgres_data" Created 0.0s ✔ Volume "complete-django-for-production_full_cycle_dev_local_postgres_data_backups" Created 0.0s ✔ Container full_cycle_dev_local_postgres Created 0.1s ✔ Container full_cycle_dev_local_mailpit Created 0.1s ✔ Container full_cycle_dev_local_redis Created 0.1s ✔ Container full_cycle_dev_local_docs Created 0.1s ✔ Container full_cycle_dev_local_django Created 0.1s ✔ Container full_cycle_dev_local_celeryworker Created 0.1s ✔ Container full_cycle_dev_local_celerybeat Created 0.1s ✔ Container full_cycle_dev_local_flower Created 0.1sAttaching to full_cycle_dev_local_celerybeat, full_cycle_dev_local_celeryworker, full_cycle_dev_local_django, [...]full_cycle_dev_local_django | * Running on all addresses (0.0.0.0)full_cycle_dev_local_django | * Running on http://127.0.0.1:8000full_cycle_dev_local_django | * Running on http://172.25.0.6:8000full_cycle_dev_local_django | Press CTRL+C to quitfull_cycle_dev_local_django | * Restarting with watchdog (inotify)full_cycle_dev_local_celeryworker | [2023-11-06 11:59:51,779: INFO/SpawnProcess-1] mingle: all alonefull_cycle_dev_local_celeryworker | [2023-11-06 11:59:51,787: INFO/SpawnProcess-1] celery@91421ba6a081 ready.full_cycle_dev_local_django | Performing system checks...full_cycle_dev_local_django |full_cycle_dev_local_django | System check identified no issues (0 silenced).full_cycle_dev_local_django |full_cycle_dev_local_django | Django version 4.2.7, using settings 'config.settings.local'full_cycle_dev_local_django | Development server is running at http://0.0.0.0:8000/full_cycle_dev_local_django | Using the Werkzeug debugger (https://werkzeug.palletsprojects.com/)full_cycle_dev_local_django | Quit the server with CONTROL-C.full_cycle_dev_local_django | * Debugger is active!full_cycle_dev_local_django | * Debugger PIN: 823-814-084

I’ve removed lines to keep it readable. Open http://127.0.0.1:8000/ in your browser to be greeted by the development environment. I will look like this:

If this did not work for you, please contact us on discord and we will help you get up and running.

We have just created the backbone of our product. What would have taken weeks to create and test manually, has been generated instantly. This is overwhelming, but we will manage to understand and use it. In the next post, we will dive deeper into the code and explore what we can do already. Subscribe for free to not miss out!

Like the content? Support!- Follow on Twitter- Join the Discord Community

Reply

or to participate.