BCS Create Django Admin

To create and administration site for you models you must first establish a super user by issuing the following command in the project directory.

python3 manage.py createsuperuser

The results of issuing the command is as follows.

Superuser created successfully is the desired result.

Modify the admin.py in your application directory to reflect the following.

from django.contrib import admin
from .models import Post
 
# Register your models here.
 
admin.site.register(Post)

Start the server and bring up the administration web site.

python3 manage.py runserver

In the browser issue the following command.

http://localhost:8000/admin

When prompted to login use the credentials created when you established the superuser.

Mr. Arch Brooks, Software Engineer, Brooks Computing Systems, LLC authored this article.

BCS Create Django Application

Return to the project location in terminal and issue the following command.

django-admin startapp blog

In the terminal application change directories to current application sub directory.

Modify models.py to define the tables in question.

All the associated field descriptions can be reviewed by clicking here.

After the tables have been defined use the following command to make migrations.

b

python3 manage.py makemigrations blog

The following depicts the output from the command.

We should apply all migrations to the database by issuing the following command.  Return to the project root and issue the following command.

python3 manage.py migrate

The result of the following command is as follows.

Mr. Arch Brooks, Software Engineer, Brooks Computing Systems, LLC authored this article.

BCS Create Django Site with MySQL Backend

For those times when Django site need to support MySQL this tutorial may be helpful.

The first task is to create the Django project. Go to the command prompt and issue the following command.

django-admin startproject mysite

The result may be observed in the following screen shot.

Next we should allow the MySQL connection.  Firs we need to import the MySQL interface by including the two statements below into settings.py for your project.

import pymysql 
pymysql.install_as_MySQLdb()

Next declare the default database description to point to the correct MySQL database by incorporating the following lines.

'ENGINE': 'django.db.backends.mysql', 
  'NAME': 'pblog', 
  'USER' : 'bcs', 
  'PASSWORD' : 'YourPassword', 
  'HOSTS' : 'localhost', 
  'PORT' : '3306',

After the chages are incorporated the resulting settigs.py file reflects the following.

"""
Django settings for mysite project.
 
Generated by 'django-admin startproject' using Django 1.11.8.
 
For more information on this file, see
https://docs.djangoproject.com/en/1.11/topics/settings/
 
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.11/ref/settings/
"""
 
import os
import pymysql
pymysql.install_as_MySQLdb()
 
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
 
 
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/
 
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'r8=&&1s%@60c%&cffq(bb54%_=px-@z=z#(htd!)jo^ny8xy9j'
 
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
 
ALLOWED_HOSTS = []
 
 
# Application definition
 
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]
 
MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
 
ROOT_URLCONF = 'mysite2.urls'
 
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]
 
WSGI_APPLICATION = 'mysite2.wsgi.application'
 
 
# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases
 
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'pblog',
        'USER' : 'bcs',
        'PASSWORD' : 'YourPassword',
        'HOSTS' : 'localhost',
        'PORT' : '3306',
    }
}
 
 
# Password validation
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators
 
AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]
 
 
# Internationalization
# https://docs.djangoproject.com/en/1.11/topics/i18n/
 
LANGUAGE_CODE = 'en-us'
 
TIME_ZONE = 'UTC'
 
USE_I18N = True
 
USE_L10N = True
 
USE_TZ = True
 
 
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/
 
STATIC_URL = '/static/'

We should start the server to determine the correctness of our configuration.

python manage.py runserver

Notice the server is running at the prescribed location.

The final step to this portion of the process is to migrate the database by issuing the following command.

python3 manage.py migrate

After the migration is successful you should have similar output messages in your terminal session.

Mr. Arch Brooks, Software Engineer, Brooks Computing Systems, LLC authored this article.

BCS Django Twitter Bootstrap


Django and the Twitter Bootstrap offers the ultimate environment for web development for multiple devices.

Create the project.

django-admin startproject yourAppName

Allow the appropriate access by updating the following in settings.py file.

ALLOWED_HOSTS = ['109.203.114.78', 'localhost', 'archbrooks.us']

Now configure the desired database.  Special imports are required for MySQL.

import pymysql
pymysql.install_as_MySQLdb()

Now include the database configuration parameters.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'YourDB',
        'USER' : 'YourUser',
        'PASSWORD' : 'Password',
        'HOSTS' : '',
        'PORT' : '',
    }
}

At this point I would start the server to check for errors.
python manage.py runserver 0.0.0.0:8000

The next step is to install the Twitter Bootstrap.
pip install django-twitter-bootstrap

The following is generated after sucessful install of bootstrap.
Enter root@server:~/django/nws# pip install django-twitter-bootstrap
Collecting django-twitter-bootstrap
  Downloading django-twitter-bootstrap-3.3.0.tar.gz (156kB)
    100% |████████████████████████████████| 163kB -276578bytes/s
Building wheels for collected packages: django-twitter-bootstrap
  Running setup.py bdist_wheel for django-twitter-bootstrap ... done
  Stored in directory: /root/.cache/pip/wheels/7b/b2/e7/93eebb9195c2f80c83ce543768b99629c3da5ec89fb81bfb1b
Successfully built django-twitter-bootstrap
Installing collected packages: django-twitter-bootstrap
Successfully installed django-twitter-bootstrap-3.3.0
code here

Now create the first application of the site.
django-admin startapp myapp

At this point I would start the server to check for errors.

python manage.py runserver 0.0.0.0:8000

If no errors were reported you are now ready to activate the first page of your site.

Modify the urls.py file for the application to reflect the following:

from django.conf.urls import include, url
from django.contrib import admin
from mws import views
 
urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^admin/', include(admin.site.urls)),
]

 

Mr. Arch Brooks, Software Engineer, Brooks Computing Systems, LLC authored this article.

BCS Django Using MySQL Backend

To use a MySQL database with Django change the settings file to depict the following.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'DB_NAME',
        'USER': 'DB_USER',
        'PASSWORD': 'DB_PASSWORD',
        'HOST': 'localhost',   # Or an IP Address that your DB is hosted on
        'PORT': '3306',
    }
}

Mr. Arch Brooks, Software Engineer, Brooks Computing Systems, LLC authored this article.

BCS Django Workbench

Top
Create New App
Start The Server
Launch Web Browser
Launch Django IDE

When you need to write applications using Django here is a workbench you may find to be helpful.  This full featured application performs all the necessary Django function without entering a single command on the command line.

When you create a new project the following dialog appears to collect a family name.

dwb02

If the family name already exists the following message appears and execution of the creation step ends.

dwb03

Top

Create New App

To create a new application select the Start App menu item and the following dialog will appear.

dwb08

When the user select a project by double clicking on the desired item the following dialog appears.

dwb09

Click the OK control to accept the application name.

If the application already exists the following dialog will appear.

dwb10

This application will not allow you to write over and existing project.  If the application does not exists then it is created and the user can proceed to utilize the newly generated code.

Top

Start The Server

To start the server for a particular Django project select the Start Server Menu Item.

dwb04

Select the desired sub folder to indicate the server to start by double clicking on the desirable item.

dwb05

The server is started and is available at port 8000 (by default).

Top

Launch Web Browser

By selecting the Launch Browser Option the web browser is launched to review the started application.

Top

Launch Django IDE

To invoke the Django IDE select the Launch Django IDE option and the following dialog will appear.

dwb06

The IDE is started with the file name selected appearing in the editor.

dwb07

A 32 bit executable program may be downloaded by clicking here.
A 64 bit executable program may be downloaded by clicking here.
The component documentation can be found by clicking here.
The source code may be found by clicking here.

Mr. Arch Brooks, Software Engineer, Brooks Computing Systems, LLC authored this article.

BCS Django Tutorial 01

Python version 3.5.x does not employ the normal MySQL databases.  Eclipse Neon 4.6 supports PyDev plugin for Python and Django.  I recommend using Eclipse for project creations.  When I authored this article initially Python 3.5.x and Eclipse Neon 4.6 were not available.  The screenshot below show the types of project supported by Eclipse.

dje01

To create a new project invoke the command prompt shell and key in the command as depicted below.

jt01

As you can see the jt01 sub directory has been created and contains the new Django project.

If your DBMS is not configured correctly you can use the following example to do so.

jt02

To configure MySQL for Django modify he “settings.py” of your project to reflect the following.

Near the top of the file place these two statements beneath the existing import statement.

import pymysql
pymysql.install_as_MySQLdb()

Move down to the the databases section and include the code below.

DATABASES = {
    'default': {
        'NAME': 'bcswebtools',
        'ENGINE': 'django.db.backends.mysql',
        'USER': 'user',
        'PASSWORD': 'password',
        'OPTIONS': {
          'autocommit': True,
        },
    }
}

Mr. Arch Brooks, Software Engineer, Brooks Computing Systems, LLC authored this article.