Background
Setting up a Django project from scratch can be a daunting task, especially if you are not familiar with the process. I am going to share my experience on how I set up my Django projects to help you overcome some of the common issues that may arise. This guide is intended for beginners and assumes you already know how to start a Django application and create an app.
Create Project
I assume that all those reading this project already knows how to start a Django application and how to create an app. So I will be skipping over these topics in details. The commands to do those are:
python manage.py startproject <project_name>
python manage.py startapp <app_name>
Changing Variables
You will need to change variables in:
manage.py
config/wsgi.py
config/asgi.py
config/settings.py
Within settings you will need to change
ROOT_URLCONF = 'config.urls'
WSGI_APPLICATION = 'config.wsgi.application'
Project Structure
To keep my root folder clutter-free, I prefer to use the following structure for my project: config/ for settings and other configurations and a folder named after the project name for the apps. So, for instance, if my project is called “my_project” and I have apps named “accounts,” “dashboard,” and “payments,” my project structure would look like this:
config/
...asgi.py
...settings.py
...urls.py
...wsgi.py
abc_shop/
...accounts/
...dashboard/
...payments/
Configuration
Now that we have our project structure set up, let’s move on to the most important aspect of any project: user authentication. Always create a custom user model in your project to have complete control over user authentication. You can do this by creating an “accounts” app and adding a model named “CustomUser” to its models.py file. You can then customize the user model to fit your project’s needs.
To make Django use your custom user model, add the following line to your settings.py file:
Always add custom user to your project. You can do this by creating an accounts app and inside the models.py file write
from django.contrib.auth.models import AbstractUser
class CustomUser(AbstractUser):
pass
You can customize the user however you want.
Inside the admin.py of the same module, write
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib import admin
from armandokan.accounts.models import CustomUser
class UserAdmin(BaseUserAdmin):
pass
admin.site.register(CustomUser, UserAdmin)
Once again you are free to customize it however you like.
With this done, now go to settings.py file and modify this variable
AUTH_USER_MODEL = "accounts.CustomUser"
With this we now have a custom user.
Now run this
python manage.py makemigrations accounts
Django Jazzmin
Next up is the Django admin interface. While the default admin interface is functional, I prefer to use the Jazzmin theme for a more modern and aesthetically pleasing interface.
Here is the link to it’s documentation: https://django-jazzmin.readthedocs.io/installation/
To install Jazzmin, run the following command:
pip install -U django-jazzmin
Add jazzmin
to your INSTALLED_APPS
before django.contrib.admin
, and Voila!
INSTALLED_APPS = [
'jazzmin',
'django.contrib.admin',
[...]
]
See configuration for optional customization of the theme
See development for notes on setting up for development
Customizing Jazzmin
Here is the customization that I use for jazzmin.
JAZZMIN_SETTINGS = {
"site_title": "OrgBea",
"site_header": "OrgBea",
"site_brand": "OrgBea",
"site_logo": "img/logo_sm.png",
"login_logo": "",
"login_logo_dark": "",
"site_logo_classes": "",
"site_icon": "",
"welcome_sign": "OrgBea: The caffeine for your organization.",
"copyright": "Org Bea",
"search_model": "accounts.CustomUser",
"user_avatar": None,
"topmenu_links": [
{"name": "Home", "url": "admin:index", "permissions": ["auth.view_user"]},
{"name": "Support", "url": "mailto://dev@asaurav.com.np", "new_window": True},
# {"model": "auth.User"},
],
"usermenu_links": [
# {"model": "auth.user"}
],
"show_sidebar": True,
"navigation_expanded": True,
"hide_apps": [],
"hide_models": [],
# "order_with_respect_to": [],
"icons": {
"auth": "fas fa-users-cog",
"auth.user": "fas fa-user",
"auth.Group": "fas fa-users",
},
"default_icon_parents": "fas fa-chevron-circle-right",
"default_icon_children": "fas fa-circle",
"related_modal_active": True,
"custom_css": "css/admin_custom.css",
"custom_js": "smart-selects/admin/js/chainedfk.js",
"show_ui_builder": False,
"changeform_format": "horizontal_tabs",
"changeform_format_overrides": {"auth.user": "collapsible", "auth.group": "vertical_tabs"},
}
JAZZMIN_UI_TWEAKS = {
"navbar_small_text": False,
"footer_small_text": False,
"body_small_text": False,
"brand_small_text": False,
"brand_colour": False,
"accent": "accent-primary",
"navbar": "navbar-white navbar-light",
"no_navbar_border": False,
"navbar_fixed": True,
"layout_boxed": False,
"footer_fixed": False,
"sidebar_fixed": True,
"sidebar": "sidebar-dark-primary",
"sidebar_nav_small_text": False,
"sidebar_disable_expand": False,
"sidebar_nav_child_indent": False,
"sidebar_nav_compact_style": False,
"sidebar_nav_legacy_style": False,
"sidebar_nav_flat_style": False,
"theme": "default",
"dark_mode_theme": None,
"button_classes": {
"primary": "btn-outline-primary",
"secondary": "btn-outline-secondary",
"info": "btn-outline-info",
"warning": "btn-outline-warning",
"danger": "btn-outline-danger",
"success": "btn-outline-success"
}
}
Oviously rename the variable values and change the logo urls and so on.