Django Dashboard Black - Open-Source Boilerplate Code

sm0ke

Sm0ke

Posted on January 13, 2020

Django Dashboard Black - Open-Source Boilerplate Code

Hello Coders,

This article presents a simple, open-source Django Dashboard crafted with authentication, database, basic modules, and deployment scripts on top of Black Dashboard UI (free version).

For newcomers, Black Dashboard is a beautiful Bootstrap 4 Admin Dashboard with a huge number of components built to fit together and look amazing. It combines colors that are easy on the eye, spacious cards, beautiful typography, and graphics.

Django Dashboard Black - Template project provided by AppSeed.


What is Django

Short note for newcomers: Django is an open-source web application framework written in Python programming language. A framework means a collection of modules that are logically grouped together and allow you to create web applications by reusing stuff, instead of writing all from scratch.

Useful Django Resources:

  • Django - official website and docs
  • Django - related content provided by the (popular) Full-Stack-Python platform

Code structure

The web app has three main modules grouped by functionalities:

  • core module is the application workhorse, used to handle the static assets and global configuration
  • authentication module - manage the login and users registration
  • app module - load the app pages if the user is authenticated or redirect the request to the login page, otherwise.

The relevant files, for all modules, are listed in this simple ASCII chart:

< PROJECT ROOT >
   |
   |-- core/                               # Implements app logic and serve the static assets
   |    |-- settings.py                    # Django app bootstrapper
   |    |-- wsgi.py                        # Start the app in production
   |    |-- urls.py                        # Define URLs served by all apps/nodes
   |    |
   |    |-- static/
   |    |    |-- <css, JS, images>         # CSS files, Javascripts files
   |    |
   |    |-- templates/                     # Templates used to render pages
   |         |
   |         |-- includes/                 # HTML chunks and components
   |         |    |-- navigation.html      # Top menu component
   |         |    |-- sidebar.html         # Sidebar component
   |         |    |-- footer.html          # App Footer
   |         |    |-- scripts.html         # Scripts common to all pages
   |         |
   |         |-- layouts/                  # Master pages
   |         |    |-- base-fullscreen.html # Used by Authentication pages
   |         |    |-- base.html            # Used by common pages
   |         |
   |         |-- accounts/                 # Authentication pages
   |         |    |-- login.html           # Login page
   |         |    |-- register.html        # Register page
   |         |
   |      index.html                       # The default page
   |     page-404.html                     # Error 404 page
   |     page-500.html                     # Error 404 page
   |       *.html                          # All other HTML pages
   |
   |-- authentication/                     # Handles auth routes (login and register)
   |    |
   |    |-- urls.py                        # Define authentication routes  
   |    |-- views.py                       # Handles login and registration  
   |    |-- forms.py                       # Define auth forms  
   |
   |-- app/                                # A simple app that serve HTML files
   |    |
   |    |-- views.py                       # Serve HTML pages for authenticated users
   |    |-- urls.py                        # Define some super simple routes  
   |
   |-- requirements.txt                    # Development modules - SQLite storage
   |
   |-- .env                                # Inject Configuration via Environment
   |-- manage.py                           # Start the app - Django default start script
   |
   |-- ************************************************************************
Enter fullscreen mode Exit fullscreen mode

The environment

Before we can use the app we should have Python3 installed in the workstation. To test the installation, open a terminal and type:

PS C:\wamp64\www> python --version
Python 3.7.2 <--- All good
Enter fullscreen mode Exit fullscreen mode

Build the app

As mentioned before, the app is published on Github platform and the source code comes with a comprehensive README file with all the necessary instructions to build the app:

Clone the app sources (via GIT)

$ # Get the code
$ git clone https://github.com/app-generator/django-dashboard-black.git
$ cd django-dashboard-black
Enter fullscreen mode Exit fullscreen mode

Install the dependencies

The modules can be installed as global dependencies (not recommended) or using a Virtualenv that executes the code in an isolated environment.

$ virtualenv env
$ source env/bin/activate
$ pip3 install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

Once the modules are installed, the next step is to set up the database. The app comes with an SQLite database, the default option for Django simple apps.

$ # Create tables
$ python manage.py makemigrations
$ python manage.py migrate
Enter fullscreen mode Exit fullscreen mode

The makemigrations subcommand will generate the necessary SQL code and migrate will create the database and tables. The app uses a single table for users registration and login. If all goes well, now we should be able to start the application.

Start the web app

$ # Start the application (development mode)
$ python manage.py runserver # default port 8000
$
$ # Start the app - custom port 
$ # python manage.py runserver 0.0.0.0:<your_port>
$
$ # Access the web app in browser: http://127.0.0.1:8000/
Enter fullscreen mode Exit fullscreen mode

By visiting the app in the browser, we should see the login screen:

Django Dashboard Black - Open-Source Admin Panel the Login Screen.

Currently, we don't have any user in our database but we can easily create one using the registration page:

Django Dashboard Black - Open-Source Admin Panel the Register Screen.

During the registration process, some basic checks are performed in the backend server (user already exists, weak password) and if all goes well, the user is created and the request is redirected to login page.

After authentication, the web app will display all the menus and the user information (id, email, and username) is injected in the user profile page:

Django Dashboard Black - Open-Source Admin Panel the Register Screen.

The user information is available in any view as a global object in the request:

        <form>
         ...

          <div class="row">
            <div class="col-md-5 pr-md-1">
              <div class="form-group">
                <label>UserID (disabled)</label>
                <input type="text" class="form-control" disabled="" value="{{ request.user.id }}">
              </div>
            </div>
          </div>

          ...

Enter fullscreen mode Exit fullscreen mode

Thanks for reading! For more resources, feel free to access:

💖 💪 🙅 🚩
sm0ke
Sm0ke

Posted on January 13, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related