Boilerplate Django Apps
Reference codebase used in all Django Apps provided by the AppSeed platform
All Django Apps generated by AppSeed share the same codebase structure and features:
  • Reference Codebase - Django Apps Boilerplate​
  • SQLite Database, Django Native ORM
  • Session-Based Authentication, Forms validation
  • Deployment scripts: Docker, Gunicorn/Nginx
Links
​Support (Email and LIVE on Discord) for registered users.

✨ Environment

To use the starter, Python3 should be installed properly in the workstation. If you are not sure if Python is installed, please open a terminal and type python --version. Here is the full list with dependencies and tools required to build the app:
  • ​Python3 - the programming language used to code the app
  • ​GIT - used to clone the source code from the Github repository
  • Basic development tools (g++ compiler, python development libraries ..etc) used by Python to compile the app dependencies in your environment.
  • (Optional) Docker - a popular virtualization software

✨ Start the app in Docker

πŸ‘‰ Step 1 - Download the code from the GH repository (using GIT)
1
$ # Get the code
2
$ git clone https://github.com/app-generator/boilerplate-code-django.git
3
$ cd boilerplate-code-django
Copied!
πŸ‘‰ Step 2 - Start the APP in Docker
1
$ docker-compose up --build
Copied!
Visit http://localhost:5085 in your browser. The app should be up & running.

✨ Manual Build

Download the code
1
$ # Get the code
2
$ git clone https://github.com/app-generator/boilerplate-code-django.git
3
$ cd boilerplate-code-django
Copied!

πŸ‘‰ Set Up for Unix, MacOS

Install modules via VENV
1
$ virtualenv env
2
$ source env/bin/activate
3
$ pip3 install -r requirements.txt
Copied!
Set Up Database
1
$ python manage.py makemigrations
2
$ python manage.py migrate
Copied!
Start the app
1
$ python manage.py runserver
Copied!
At this point, the app runs at http://127.0.0.1:8000/.

πŸ‘‰ Set Up for Windows

Install modules via VENV (windows)
1
$ virtualenv env
2
$ .\env\Scripts\activate
3
$ pip3 install -r requirements.txt
Copied!
Set Up Database
1
$ python manage.py makemigrations
2
$ python manage.py migrate
Copied!
Start the app
1
$ python manage.py runserver
Copied!
At this point, the app runs at http://127.0.0.1:8000/.

✨ Manage App Users

By default, the starter is not provided with users. To access the private pages and the admin section (reserved for superusers) follow up the next sections.

πŸ‘‰ Create Superusers

To access the admin section, Django requires superuser privilegies. Let's create a new superuser and access the admin section of the project:
1
$ python.exe manage.py createsuperuser
2
>>> Username (leave blank to use 'test'): test
3
>>> Email address: [email protected]
4
>>> Password: *******
5
>>> Password (again): *******
6
>>> Superuser created successfully.
Copied!
Once the superuser is successfully created, we can access the admin section:
http://localhost:8000/admin/ (make sure you have a / at the end).

πŸ‘‰ Create (Ordinary) Users

By default, the app redirects guest users to authenticate. In order to access the private pages, follow this set up:
  • Start the app via python manage.py runserver
  • Access the registration page and create a new user:
    • http://127.0.0.1:8000/register/
  • Access the sign in page and authenticate
    • http://127.0.0.1:8000/login/

✨ Codebase structure

The project is coded using a simple and intuitive structure presented below:
1
< PROJECT ROOT >
2
|
3
|-- core/ # Implements app configuration
4
| |-- settings.py # Defines Global Settings
5
| |-- wsgi.py # Start the app in production
6
| |-- urls.py # Define URLs served by all apps/nodes
7
|
8
|-- apps/
9
| |
10
| |-- home/ # A simple app that serve HTML files
11
| | |-- views.py # Serve HTML pages for authenticated users
12
| | |-- urls.py # Define some super simple routes
13
| |
14
| |-- authentication/ # Handles auth routes (login and register)
15
| | |-- urls.py # Define authentication routes
16
| | |-- views.py # Handles login and registration
17
| | |-- forms.py # Define auth forms (login and register)
18
| |
19
| |-- static/
20
| | |-- <css, JS, images> # CSS files, Javascripts files
21
| |
22
| |-- templates/ # Templates used to render pages
23
| |-- includes/ # HTML chunks and components
24
| | |-- navigation.html # Top menu component
25
| | |-- footer.html # App Footer
26
| | |-- scripts.html # Scripts common to all pages
27
| |
28
| |-- layouts/ # Master pages
29
| | |-- base.html # Used by common pages
30
| |
31
| |-- accounts/ # Authentication pages
32
| | |-- login.html # Login page
33
| | |-- register.html # Register page
34
| |
35
| |-- home/ # UI Kit Pages
36
| |-- index.html # Index page
37
| |-- page-404.html # 404 page
38
| |-- *.html # All other pages
39
|
40
|-- requirements.txt # Development modules - SQLite storage
41
|
42
|-- .env # Inject Configuration via Environment
43
|-- manage.py # Start the app - Django default start script
44
|
45
|-- ************************************************************************
Copied!

✨ Application Bootstrap Flow

The entry point of the project is the core.settings.py file where the project configuration is bundled. The most important files that make the project functional are listed below:
  • manage.py (saved in the root of the project) loads core/settings.py
  • core/settings.py:
    • Loads the .env file (dynamic configuration)
    • Loads the project routing:
      • core.urls.py
    • Defines the templates directory
      • apps/templates
    • Defines the INSTALLED_APPS section
      • apps.home - custom app that serve all pages
    • If the DB_ENGINE variable is not present in the environment
      • SQLite persistence is used
    • If the DB_ENGINE is present
      • The DB URI is built dynamically for MySql or PostgreSQL.

✨ Project Routing

The core file that bundles together all routing rules is core/urls.py.
The home application being a generic router that serves all pages defined in the templates/home directory, should be the last rule defined in the urlpatterns.
NOTE: all new apps, should be included above apps.home.urls generic rule.
1
urlpatterns = [
2
path('admin/', admin.site.urls), # Django admin route
3
path("", include("apps.authentication.urls")), # Auth routes - login / register
4
​
5
# ADD NEW Routes HERE
6
​
7
# Leave `Home.Urls` as last the last line
8
path("", include("apps.home.urls"))
9
]
Copied!

✨ UI Assets and Templates

The project comes with a modern UI fully migrated and usable with Django Template Engine.

πŸ‘‰ Page Templates

All pages and components are saved inside the apps/templates directory. Here are the standard directories:
  • templates/layouts: UI masterpages
  • templates/includes: UI components (used across multiple pages)
  • templates/accounts: login & registration page
  • templates/home: all other pages served via a generic routing by apps/home app
1
< PROJECT ROOT >
2
|
3
|-- core/ # Implements app configuration
4
|-- apps/
5
| |
6
| |-- home/ # A simple app that serve HTML files
7
| |-- authentication/ # Handles auth routes (login and register)
8
| |
9
| |-- static/
10
| | |-- <css, JS, images> # CSS files, Javascripts files
11
| |
12
| |-- templates/ # Templates used to render pages
13
| |-- includes/ # HTML chunks and components
14
| | |-- navigation.html # Top menu component
15
| | |-- footer.html # App Footer
16
| | |-- scripts.html # Scripts common to all pages
17
| |
18
| |-- layouts/ # Master pages
19
| | |-- base.html # Used by common pages
20
| |
21
| |-- accounts/ # Authentication pages
22
| | |-- login.html # Login page
23
| | |-- register.html # Register page
24
| |
25
| |-- home/ # UI Kit Pages
26
| |-- index.html # Index page
27
| |-- page-404.html # 404 page
28
| |-- *.html # All other pages
29
|
30
|-- ************************************************************************
Copied!

πŸ‘‰ Static Assets

The static assets used by the project (JS, CSS, images) are saved inside the apps/static/assets folder. This path can be customized with ease via ASSETS_ROOT variable saved in the .env file.
How it works
  • .env defines the ASSETS_ROOT variable
  • core/settings.py read the value of ASSETS_ROOT and defaults to /static/assets if not found:
1
# content of core/settings.py (truncated content)
2
​
3
ASSETS_ROOT = os.getenv('ASSETS_ROOT', '/static/assets')
Copied!
  • All pages and components use the ASSETS_ROOT variable. Here is a sample extracted from templates/layouts/base.html:
1
<head>
2
​
3
<!-- Source Code -->
4
<link rel="stylesheet" href="{{ ASSETS_ROOT }}/css/style.css">
5
​
6
<!-- RUNTIME -->
7
<link rel="stylesheet" href="/static/assets/css/style.css">
Copied!
At runtime, the href property is resolved to /static/assets/css/style.css based on the value saved in the .env file:
1
# No Slash at the end
2
ASSETS_ROOT=/static/assets
Copied!

✨ Default Apps

The codebase comes with two simple apps that handle the authentication and serve all pages saved in the apps/templates/home directory.

πŸ‘‰ Authentication App

This default app defined in apps/authentication handles the authentication routes login, register. The most important files that make the authentication usable, are listed below:
  • forms.py - defines the Login, Registration forms
  • views.py - implements the login, registration flow
  • routes.py - map routing rules over the views
  • models.py - EMPTY file
    • The extended user model is NOT provided

πŸ‘‰ Home App

This app returns all pages saved in the templates/home directory to authenticated users. In case a page is not found, a generic page is returned using a 404 HTTP error status.

✨ Customisation

πŸ‘‰ Set up the MySql Database

Note: Make sure your Mysql server is properly installed and accessible.
Step 1 - Create the MySql Database to be used by the app
  • Create a new MySql database
  • Create a new user and assign full privilegies (read/write)
Step 2 - Install mysqlclient package
1
$ pip install mysqlclient
Copied!
Step 3 - Edit the .env to match your MySql DB credentials. Make sure DB_ENGINE is set to mysql.
  • DB_ENGINE : mysql
  • DB_NAME : default value = appseed_db
  • DB_HOST : default value = localhost
  • DB_PORT : default value = 3306
  • DB_USERNAME: default value = appseed_db_usr
  • DB_PASS : default value = pass
Here is a sample:
1
# .env sample
2
​
3
DB_ENGINE=mysql # Database Driver
4
DB_NAME=appseed_db # Database Name
5
DB_USERNAME=appseed_db_usr # Database User
6
DB_PASS=STRONG_PASS_HERE # Password
7
DB_HOST=localhost # Database HOST, default is localhost
8
DB_PORT=3306 # MySql port, default = 3306
Copied!
At this point, the app should use MySql for the persistence layer.

πŸ‘‰ Adding a new app

The existing codebase can be extended with ease with new apps and features. Here are the steps that create a new application named polls.
Create a new app using startapp command (make sure you are in the root of the project)
1
$ python manage.py startapp polls
Copied!
Write a simple view for the new app - Edit polls/views.py
1
from django.http import HttpResponse
2
​
3
def index(request):
4
return HttpResponse("Hello! This is the polls APP index.")
Copied!
Create urls.py inside the polls directory
1
from django.urls import path
2
​
3
from . import views
4
​
5
urlpatterns = [
6
path('', views.index, name='index'),
7
]
Copied!
Update project routing - core/urls.py file:
1
urlpatterns = [
2
path('admin/', admin.site.urls),
3
path("", include("apps.authentication.urls")),
4
​
5
# ADD NEW Routes HERE
6
path('polls/', include('polls.urls')), # <-- NEW
7
​
8
# Leave `Home.Urls` as last the last line
9
path("", include("apps.home.urls"))
10
]
Copied!
Enable the new app - Update core/settings.py file:
1
... (truncated content)
2
​
3
INSTALLED_APPS = [
4
'django.contrib.admin',
5
'django.contrib.auth',
6
'django.contrib.contenttypes',
7
'django.contrib.sessions',
8
'django.contrib.messages',
9
'django.contrib.staticfiles',
10
'polls', # <-- NEW
11
'apps.home'
12
]
13
​
14
... (truncated content)
Copied!
Start the project and access the project in the browser:
http://localhost:8000/polls/
Django new app - browser output.

πŸ‘‰ Static Assets for production

As explained in the Static Assets section, the assets are managed via:
  • apps/static/assets - the folder where JS, CSS, and images files are saved
  • ASSETS_ROOT - environment variable, that defaults to /static/assets if not defined
In production, the contents of the apps/static/assets files should be copied to an external (public) directory and the ASSETS_ROOT environment variable updated accordingly.
For instance, if the static files are copied to https://cdn.your-server.com/datta-able-assets, the .env file should be updated as below:
1
# No Slash at the end
2
ASSETS_ROOT=https://cdn.your-server.com/datta-able-assets
Copied!

πŸš€ Where to go from here

  • πŸ‘‰ Access the support page in case something is missing
  • πŸ‘‰ Use the App Generator and build a new project