Django.guru

Docker Image for Django Application (Alpine Based)

August 21, 2022
Infrastructure
docker

Apline based images have a huge advantage by it’s final size but in most cases in the Python world if neotiates by Python dependencies which takes too long to build project packages and final size sometimes may be greater than same image build on debian based image. But Apline still great solution for small Django applications to keep final image tiny.
This post written after the similar post related to the debian based image build proccess, so I wont specify here all environmental details and just provide few Dockerfile examples which you may to use for start.

...

How to Use Database Router for Multiple Databases

August 21, 2022
database

Using multiple database in the Django project you may face with situation when you need apply migrations for some app or model just to the some databse instead all of defined databases, or you need to perform read/write actions for models of the some application only for specific database instead specifying .using(db) every time.

...

Configure staticfiles directory for manage.py collectstatic

August 21, 2022
static

Starting new project it’s have no configured staticfiles directory configured by default, so it is not able you to use python manage.py collectstatic script to generate staticfiles bundle. It is small tip how to configure staticfiles fast and easy, just add this line to your settings.py file.

...

Pass Instance With Predefined Data to Form (CBV)

August 21, 2022
forms

In case when you need to initialize form with already predefined details of the form instance, for example form that add comment to a post - you can predefine post_id in the form instance instead applying it in the save() method.

...

get_success_url() to Previous Page

August 21, 2022
forms

Easy way to redirect user on previous (original) page after form processing it is use HTTP_REFERER header, here an example how it’s may be implemented in the get_success_url() of the Django’s class based view.

...

Docker Image for Django Application (Debian Based)

August 20, 2022
Infrastructure
docker

Docker is most reliable and flexible way to distribute your Django applications, it able to include entire environment settings, dependencies and application files to deploy it to the server or any cloud provider. This post show you a few examples how to build Docker image for your Django application, this will be based on Debian base images as it recommended option for Python applications, of course it could be build based on Alpine images but it mostly suitable for small apps because in most cases build time and final image size based on Alpine would be bigger than Debian based, more about issues with Alpine based images you can read here and here. ...

Why Djnago Form Is Invalid How to Print Errors

August 17, 2022
forms

Working with complex views that process multiple forms you can stuck on situation when some of forms didn’t valitated but you have not see a reason for this, to debug this you can print forms errors from the view POST method.

...

Choices Yes/No for Boolean Field in Django

August 17, 2022
forms

Sometimes you need to represent boolean field of the Django model as dropdown list choices, like “Yes/No” instead default boolean field form widget - CheckboxInput. You can implement it like this.

...