Book Image

Django 3 Web Development Cookbook - Fourth Edition

By : Aidas Bendoraitis, Jake Kronika
Book Image

Django 3 Web Development Cookbook - Fourth Edition

By: Aidas Bendoraitis, Jake Kronika

Overview of this book

Django is a web framework for perfectionists with deadlines, designed to help you build manageable medium and large web projects in a short time span. This fourth edition of the Django Web Development Cookbook is updated with Django 3's latest features to guide you effectively through the development process. This Django book starts by helping you create a virtual environment and project structure for building Python web apps. You'll learn how to build models, views, forms, and templates for your web apps and then integrate JavaScript in your Django apps to add more features. As you advance, you'll create responsive multilingual websites, ready to be shared on social networks. The book will take you through uploading and processing images, rendering data in HTML5, PDF, and Excel, using and creating APIs, and navigating different data types in Django. You'll become well-versed in security best practices and caching techniques to enhance your website's security and speed. This edition not only helps you work with the PostgreSQL database but also the MySQL database. You'll also discover advanced recipes for using Django with Docker and Ansible in development, staging, and production environments. By the end of this book, you will have become proficient in using Django's powerful features and will be equipped to create robust websites.
Table of Contents (15 chapters)

Defining overwritable app settings

This recipe will show you how to define settings for your app that can then be overwritten in your project's settings file. This is especially useful for reusable apps that you can customize by adding a configuration.

Getting ready

Follow the steps in the Getting ready in the Creating app configuration recipe to create your Django app.

How to do it...

  1. Define your app settings using the getattr() pattern in models.py if you just have one or two settings, or in the app_settings.py file if the settings are extensive and you want to organize them better:
# myproject/apps/magazine/app_settings.py
from django.conf import settings
from django.utils.translation import gettext_lazy as _

# Example:
SETTING_1 = getattr(settings, "MAGAZINE_SETTING_1", "default value")

MEANING_OF_LIFE = getattr(settings, "MAGAZINE_MEANING_OF_LIFE", 42)

ARTICLE_THEME_CHOICES = getattr(
settings,
"MAGAZINE_ARTICLE_THEME_CHOICES",
[
('futurism', _("Futurism")),
('nostalgia', _("Nostalgia")),
('sustainability', _("Sustainability")),
('wonder', _("Wonder")),
]
)
  1. models.py will contain the NewsArticle model, like this:
# myproject/apps/magazine/models.py
from
django.db import models
from django.utils.translation import gettext_lazy as _


class NewsArticle(models.Model):
created_at = models.DateTimeField(_("Created at"),
auto_now_add=True)
title = models.CharField(_("Title"), max_length=255)
body = models.TextField(_("Body"))
theme = models.CharField(_("Theme"), max_length=20)

class Meta:
verbose_name = _("News Article")
verbose_name_plural = _("News Articles")

def __str__(self):
return self.title
  1. Next, in admin.py, we will import and use the settings from app_settings.py, as follows:
# myproject/apps/magazine/admin.py
from
django import forms
from django.contrib import admin

from .models import NewsArticle

from .app_settings import ARTICLE_THEME_CHOICES


class NewsArticleModelForm(forms.ModelForm):
theme = forms.ChoiceField(
label=NewsArticle._meta.get_field("theme").verbose_name,
choices=ARTICLE_THEME_CHOICES,
required=not NewsArticle._meta.get_field("theme").blank,
)
class Meta:
fields = "__all__"


@admin.register(NewsArticle)
class NewsArticleAdmin(admin.ModelAdmin):
form = NewsArticleModelForm
  1. If you want to overwrite the ARTICLE_THEME_CHOICES settings for a given project, you should add MAGAZINE_ARTICLE_THEME_CHOICES in the project settings:
# myproject/settings/_base.py
from django.utils.translation import gettext_lazy as _
# ...
MAGAZINE_ARTICLE_THEME_CHOICES = [
('futurism', _("Futurism")),
('nostalgia', _("Nostalgia")),
('sustainability', _("Sustainability")),
('wonder', _("Wonder")),
('positivity', _("Positivity")),
('solutions', _("Solutions")),
('science', _("Science")),
]

How it works...

The getattr(object, attribute_name[, default_value]) Python function tries to get the attribute_name attribute from object and returns default_value if it is not found. We try to read different settings from the Django project settings module or, if they don't exist there, the default values are used.

Note that we could have defined the choices for the theme field in models.py, but instead we created a custom ModelForm in administration and set the choices there. This was done to avoid the creation of new database migrations whenever the ARTICLE_THEME_CHOICES is changed.

See also

  • The Creating app configuration recipe
  • Chapter 6, Model Administration