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 AppSeed users.
​

Environment

To use the stater, Python3 should be installed properly in the workstation. If you are not sure if Python is properly installed, please open a terminal and type python --version. 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.
​

Build the app

To built and start the app locally, follow the steps:
Get the source code
    Download the ZIP from Github Repository
    Using GIT tool in the terminal to clone the source code
Change the current directory to source code directory
1
$ # Make sure you are running the commands INSIDE source code directory
2
$
3
$ # Virtualenv modules installation (Unix based systems)
4
$ virtualenv env
5
$ source env/bin/activate
6
$
7
$ # Virtualenv modules installation (Windows based systems)
8
$ # virtualenv env
9
$ # .\env\Scripts\activate
10
$
11
$ # Install modules - SQLite Storage
12
$ pip3 install -r requirements.txt
13
$
14
$ # Create tables
15
$ python manage.py makemigrations
16
$ python manage.py migrate
17
$
18
$ # Start the application (development mode)
19
$ python manage.py runserver # default port 8000
20
$
21
$ # Start the app - custom port
22
$ # python manage.py runserver 0.0.0.0:<your_port>
23
$
24
$ # Access the web app in browser: http://127.0.0.1:8000/
Copied!
At this point, we can visit the app in the browser http://127.0.0.1:8000/. By default, the app will redirect guest users to the login page. To access the private pages:
    Create a new user using the registration page
    Authenticate using the login page
​

App Codebase

1
< PROJECT ROOT >
2
|
3
|-- core/ # Implements app logic
4
| |-- settings.py # Django app bootstrapper
5
| |-- static/
6
| | |-- <css, JS, images> # CSS files, Javascripts files
7
| |
8
| |-- templates/ # Templates used to render pages
9
| |
10
| |-- includes/ # HTML chunks and components
11
| |-- layouts/ # Master pages
12
| |-- accounts/ # Authentication pages
13
| | |-- login.html # Login page
14
| | |-- register.html # Register page
15
| |
16
| index.html # The default page
17
| page-404.html # Error 404 page
18
| page-500.html # Error 404 page
19
| *.html # All other HTML pages
20
|
21
|-- authentication/ # Handles auth routes (login and register)
22
| |
23
| |-- urls.py # Define authentication routes
24
| |-- views.py # Handles login and registration
25
| |-- forms.py # Define auth forms
26
|
27
|-- app/ # A simple app that serve HTML files
28
| |
29
| |-- views.py # Serve HTML pages for authenticated users
30
| |-- urls.py # Define some super simple routes
31
|
32
|-- requirements.txt # Development modules - SQLite storage
33
|
34
|-- .env # Inject Configuration via Environment
35
|-- manage.py # Start the app
36
|
37
|-- ************************************************************************
Copied!
​

The bootstrap flow

    Django bootstrapper manage.py uses core/settings.py as the main configuration file
    core/settings.py loads the app magic from .env file
    Redirect the guest users to Login page
    Unlock the pages served by app node for authenticated users

App Configuration

The environment configuration file .env specify a short-list with variables:
1
# File: core/settings.py
2
​
3
...
4
​
5
# SECRET_KEY value is read from `.env` file
6
SECRET_KEY = config('SECRET_KEY', default='S#perS3crEt_1122')
7
​
8
...
9
​
10
# Load the production server address from `.env` file
11
ALLOWED_HOSTS = ['localhost', '127.0.0.1', config('SERVER', default='127.0.0.1')]
12
​
13
...
14
​
15
# The SQLite database, located in the root of the project
16
DATABASES = {
17
'default': {
18
'ENGINE': 'django.db.backends.sqlite3',
19
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
20
}
21
}
Copied!
The default database is SQLite and the name and physical location can be changed by updating core/settings.py The database and associated tables are created during the migration commands, listed in the README file:
1
$ python manage.py makemigrations
2
$ python manage.py migrate
Copied!
​

App Tables

The tables created by the Django migration are generated by the default modules that handle the authentication, session management, and permissions:
    ​django.contrib.auth - Django middleware app that implements authentication
    django.contrib.sessions - Django middleware app that implements session management
​

App Forms

The file authentication/forms.py defines the table(s) used by the application. Being a simple starter, by default the following forms are defined:
    Form #1 - LoginForm with fields:
      username
      password
​
    Form #2 - SignUpForm with fields:
      name - The friendly name
      email - eMail address
      username - used to authenticate
      password1 - used to authenticate
      password2 - password check field
​

App Routing

The settings file core/settings.py specify the routing file core/urls.py via ROOT_URLCONF variable:
1
# File: core/settings.py
2
...
3
​
4
ROOT_URLCONF = 'core.urls'
5
​
6
...
Copied!
core/urls.py file
The core routing file aggregates the routing from all apps defined in the project:
1
# File: core/urls.py
2
​
3
urlpatterns = [
4
# Django admin routes - inherited from Django default modules
5
path('admin/', admin.site.urls),
6
​
7
# Authentication routes - login / register
8
# exposed by `authentication` app
9
path("", include("authentication.urls")),
10
​
11
# App routes - the modules that serve the UI Kit pages
12
path("", include("app.urls"))
13
]
14
​
Copied!

​

Pages & Assets

Pages served by the starter are organized using a simple folder structure:
1
< PROJECT ROOT >
2
|
3
|-- core/ # Implements app logic and serve the static assets
4
| |
5
| |-- static/assets/
6
| | |-- css
7
| | |-- JS
8
| | |-- images
9
| | |-- SCSS
10
| |
11
| |-- templates/ # Templates used to render pages
12
| |
13
| |-- includes/ # HTML chunks and components
14
| | |-- navigation.html # Top menu component
15
| | |-- sidebar.html # Sidebar component
16
| | |-- footer.html # App Footer
17
| | |-- scripts.html # Scripts common to all pages
18
| |
19
| |-- layouts/ # Master pages
20
| | |-- base-fullscreen.html # Used by Authentication pages
21
| | |-- base.html # Used by common pages
22
| |
23
| |-- accounts/ # Authentication pages
24
| | |-- login.html # Login page
25
| | |-- register.html # Register page
26
| |
27
| index.html # The default page
28
| page-404.html # Error 404 page
29
| page-500.html # Error 404 page
30
| *.html # All other HTML pages
31
|
32
|-- app/ # A simple app that serve HTML files
33
| |
34
| |-- views.py # Serve HTML pages for authenticated users
35
| |-- urls.py # Define some super simple routes
36
|
37
|-- ************************************************************************
Copied!

Static Assets

The folder that contains all assets provided by the UI Kit is located in the core directory
    static/assets - the root directory for all files (JS, images)
    static/assets/css - CSS files that style the app
    static/assets/img - Images and Icons
    static/assets/js - javascript files provided by the UI Kit
    static/assets/scss - SCSS files, if provided by the UI Kit vendor

Templates Folder

All pages served by the application are located inside this folder.
    templates/layouts - the directory with app master pages (layouts)
    templates/includes - the directory with HTML chunks and components
    templates/accounts - store the authentication pages (login, registration)
    templates/ - all pages defined/served by the app are saved at the root of the templates folder

Common pages

This section lists the common pages defined in all Flask applications prototyped on top of this generic starter.
    login.html, rendered with layouts/base-fullscreen.html
    register.html, rendered with layouts/base-fullscreen.html
    index.html, rendered with layouts/base.html
    page-404.html, rendered with layouts/base.html
    page-403.html, rendered with layouts/base.html

Data Structures

The starter exposes a short-list with data structures used globally across the app:

request.user object

Constructed by AuthenticationMiddleware can be used to detect if the current request is executed by an authenticated user or not. The object has global visibility and can be used in all app controllers and handlers but also in views.
Usage in controller
1
# Sample File
2
​
3
from django.http import HttpResponse
4
​
5
def testme(path):
6
​
7
# Redirect guests users to login page
8
if request.user.is_authenticated:
9
return HttpResponse("User authenticated")
10
else:
11
return HttpResponse("Access forbidden - please authenticate")
Copied!
Usage in view
1
<div class="collapse navbar-collapse" id="navigation">
2
<ul class="navbar-nav ml-auto">
3
​
4
<!-- The Usage of <current_user> object -->
5
{% if request.user.is_authenticated %}
6
​
7
<!-- Html chunk rendered for authenticated users-->
8
​
9
<li class="nav-item">
10
<a href="/" class="nav-link text-primary">
11
<i class="tim-icons icon-minimal-left"></i> Your Dashboard
12
</a>
13
</li>
14
​
15
{% else %}
16
​
17
<!-- Html chunk rendered for guests users-->
18
​
19
<li class="nav-item ">
20
<a href="{% url 'register' %}" class="nav-link">
21
<i class="tim-icons icon-laptop"></i> Register
22
</a>
23
</li>
24
<li class="nav-item ">
25
<a href="{% url 'login' %}" class="nav-link">
26
<i class="tim-icons icon-single-02"></i> Login
27
</a>
28
</li>
29
​
30
{% endif %}
31
​
32
</ul>
33
</div>
Copied!
​
Last modified 4mo ago