In this article, we'll explore how to customize a tenant site in Django by modifying the navigation bar color, uploading a logo, and creating a custom file storage solution to keep tenant files organized. We'll build upon the coffee shop tenant we created in the previous video, enhancing its appearance and functionality.
To begin customizing the tenant site, we need to change the color of the navigation bar. We will use a package called Django Color Field, which simplifies color selection in the backend. Follow these steps:
Install the package with:
pip install django-colorfield
Next, add this app to the SHARED_APPS
in your Django project's settings. Navigate to the core
folder, then open settings.py
, and include colorfield
in the SHARED_APPS
configuration.
Now that we have the package installed, we will create a new model class to store our site settings, including the color for the navigation bar:
Open the models.py
in the main app (let's say it’s named core
).
Add a new model class called SiteSetting
:
from colorfield.fields import ColorField
from django.db import models
class SiteSetting(models.Model):
name = models.CharField(max_length=100) # Use to describe what the settings are for
color = ColorField() # Color field for navigation bar
Save this file.
Next, we need to register the SiteSetting
table in the Django admin:
Open admin.py
in the same app folder and add the following code:
from django.contrib import admin
from .models import SiteSetting
admin.site.register(SiteSetting)
Now, run migrations with:
python manage.py makemigrations
python manage.py migrate
Start the development server:
python manage.py runserver
Once the site setting table has been added to the database, go to the admin interface:
/admin
to access the admin panel.Site Settings
section, and create a new setting object. For example, name it branding
, and select a color like #00B140
(Starbucks green), then click save.To reflect this color in the navigation bar, we must fetch it from the database efficiently. Instead of using a view for every page, we’ll create a custom template tag that will load the settings:
Create a new folder named templatetags
in your app directory and include an __init__.py
file.
Create a file called header.py
and add the code to create a custom template tag.
from django import template
from .models import SiteSetting
register = template.Library()
@register.inclusion_tag('header.html')
def header(request):
settings = SiteSetting.objects.first()
return ('color': settings.color if settings else None)
Update your base template (e.g., base.html
) to include this header.
Restart the server to load the new template tag.
Now, in the header.html
, modify the background color dynamically using the fetched color:
<div style="background-color: ({ color|default:'#000' )};">
(* Navigation bar content goes here *)
</div>
Next, let's add functionality to upload a logo. Modify the SiteSetting
model to include a logo attribute:
In models.py
, add the following:
logo = models.ImageField(upload_to='logo/', null=True, blank=True)
Migrate the changes with:
python manage.py makemigrations
python manage.py migrate
Update the header.py
to fetch and pass the logo URL to the template.
Update the header.html
to conditionally display the logo.
To keep media files organized by tenant, we implement a custom storage class. Here's how:
Create a new file named storage.py
in your application folder and define a custom storage class that determines the storage backend based on the schema.
Update your settings.py
to use the new storage class:
DEFAULT_FILE_STORAGE = 'yourapp.storage.CustomSchemaStorage'
By following the steps outlined, we've successfully customized a tenant site in Django. We've changed the navigation bar's color, uploaded a logo, and implemented a custom file storage solution to separate tenant files. This structured approach allows for efficient management of media files across multiple tenants.
Django, tenant customization, Django Color Field, media file storage, template tags, site settings, custom storage class
Q: How do I change the navigation bar color for a tenant site?
A: You can use the Django Color Field package to manage colors easily in the backend and update the header template to reflect the desired color.
Q: How can I upload a logo for my tenant site?
A: By adding an ImageField
to your site settings model, you can upload and manage logos directly from the Django admin interface.
Q: How do I ensure media files are stored separately for each tenant?
A: You can create a custom storage class that uses the Django Tenants package for managing file storage based on the tenant schema.
Q: Is it efficient to load tenant settings for every page request?
A: Instead of querying the database for every page, create a custom template tag that loads the settings once and can be reused across templates.
In addition to the incredible tools mentioned above, for those looking to elevate their video creation process even further, Topview.ai stands out as a revolutionary online AI video editor.
TopView.ai provides two powerful tools to help you make ads video in one click.
Materials to Video: you can upload your raw footage or pictures, TopView.ai will edit video based on media you uploaded for you.
Link to Video: you can paste an E-Commerce product link, TopView.ai will generate a video for you.