Django is a great framework for web development, with built-in ORM, MVC architecture, and a large ecosystem with many external plugins.
However, it would be a bit complicated if you are new to it. There are some tips that would help you at the beginning.
Managing apps
You can create an app with this command:
python manage.py startapp `app_name`
An application in a Django project contains the logic of how to handle the requests from client users, or it could be a group of background tasks. If you think it would deploy to different servers with different features, it should separate the features into different apps. For example, you might have coreapp
app for public users and myadmin
app for internal users, since coreapp
deploy to more servers since more usage but myadmin
should deploy to fewer servers.
And an app should depend on apps as a stack to avoid circular import. That means myadmin
depend on coreapp
but coreapp
should not depend on myadmin
. You would also create a scheduler
app depend on coreapp
and myadmin
.
Encapsulate business logic
Web applications need to exchange data to databases, usually about the business logic. It could be a good idea to encapsulate that for readability and reusable. Django provides a custom manger for structuring it.
Let’s say there is a BookingRoom
model to store booking room information. You might want to find the rooms not booked, and you can use a custom model manager like:
class NonBookingRoomManager(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(booked_by__isnull=True)
def get_available_regions(self):
return self.get_queryset().values_list("region", flat=True).distinct()
class BookingRoom(models.Model):
# ...
region = model.CharField(max_length=40)
booked_by = models.OneToOneField(User, on_delete=models.RESTRICT)
non_booking_rooms = NonBookingRoomManager()
From the example above, we can define the manager that returns results fulfilling criteria in get_queryset
. We can also define other methods in the manager for querying other information. The usage of the manager like:
# Get all non-booking rooms
BookingRoom.non_booking_rooms.all()
# Get all regions for non-booking rooms
BookingRoom.non_booking_rooms.get_available_regions()
More details of using managers
Local development
Usually, you would need to override some settings for local development, like installing Django debug toolbar. Add the following code to your project’s settings.py
can implement for that:
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
local_settings_path = os.path.join(BASE_DIR, 'local_settings.py')
if os.path.exists(local_settings_path):
from local_settings import *
Then create the local_settings.py
in the project’s root directory, config Django debug toolbar like:
from settings import *
INSTALLED_APPS += [ "debug_toolbar", ]