Django Road: Installation and Configuration

Keywords: Python Django Database MySQL

Django Road: Installation and Configuration

MTV

Model Template View
Business Processing of Database Template Files

Understanding the Django framework, full-featured

I. Install the basic configuration of Django & Django

Install Django

    pip3 django

Configure Django

1. Configure Django environment variables

D:\Program files\python37
D:\Program files\python37\Lib\site-packages\django\bin
D:\Program files\python37\Scripts

2. Verify Django installation configuration
Enter the following commands at the python terminal

import django
django.get_version();

3.Django Project Management

  • Create project

    django-admin.exe startproject mysite

  • Django directory structure

     mySites
        mySites     Configuration of the whole program
            __init__.py
            setting.py      #configuration file
            urls.py         #URL correspondence
            wsgi.py         #WSGI follows the WSGI specification and configures uwsgi+nginx Online
        manage.py           #Manage Django programs:
                                    -python manage.py
                                    -python mangage.py startapp xx
                                    -python manage.py makemigrations
                                    -python manage.py migrate
  • Running django project

        python manage.py runserver
        python manage.py runserver 127.0.0.1:8001
  • Create APP

Create app s under the project directory

  cd  projectName
python manage.py startapp CMDB

Different functional modules can be created into different APP s

  • CMDDB APP directory structure

    - d migrations database modification table structure operation record  
    -_ Each folder in init_ py Python 2 has this file, which is considered Packages, and python 3 is optional.
    - admin.py Django provides us with background management
     - apps.py denotes configuring the current apps
     - Model. py ORM, which writes the specified class, creates the database table structure by command
     - View.py All business logic codes related to cmddb
     - tests.py for unit testing

II. Django-related file path configuration

  • Configuration template file path

    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [os.path.join(BASE_DIR,'templates')],
            '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',
                ],
            },
        },
    ]
  • Configure static file paths

    Django 2.14 refers to external css, js, pictures need to be set before they can be used

    1. Create a directory static for storing css, js and pictures under the project directory;

    2. Setting django

    Add this line'django. contrib. static files'to INSTALL_APPS in set. PY

    As shown below

    INSTALLED_APPS = [
        'django.contrib.admin',  
        'django.contrib.auth',  
        'django.contrib.contenttypes',  
        'django.contrib.sessions',  
        'django.contrib.messages',  
        'django.contrib.staticfiles',  
        ] 

    3. At the end of set.py, change to the following

        #Static files (CSS, JavaScript, Images)
        #https://docs.djangoproject.com/en/2.1/howto/static-files/
        STATIC_URL = '/static/'
        STATICFILES_DIRS = [
            os.path.join(BASE_DIR, "static"),
        ]

    4. The HTML file is referenced as follows

    {%load staticfiles%}
    <link rel='stylesheet' href="{% static 'common.css' %}" />
    <span style="background-image: url('/static/input_icons_24.png');background-position: 0 172px;height: 24px;width: 24px;display: inline-block;position: absolute; top:4px;left:0px;">
    </span>
  • Error case

1. Errors

Forbidden (403)
CSRF verification failed. Request aborted.

Modify the setting:
Comment on this line

'django.middleware.csrf.CsrfViewMiddleware',
Temporary solutions

2. Errors
UnboundLocalError at /login/
Exception Value:
local variable 'error_msg' referenced before assignment

The local variable error_msg is referenced without being defined. The reason for this error is that the location of the error_msg definition is incorrect, that is, its scope is incorrect: the scope defined by error_msg cannot be referenced within the return statement.

Configure Django data engine to MySQL

django default data engine sqlite3, to use mysql to modify the configuration

1. Prepare mysql database name: DjangoDB, create statements and authorize

     create database DjangoDB;
     grant all on DjangoDB.* to django@'172.16.200.%' identified by 'centos';
     flush privileges;

2. Configure the Django data engine to mysql in the set.py file

    DATABASES = {
    'default': {
    'ENGINE': 'django.db.backends.mysql',
    'HOST': '172.16.200.3',
    'NAME': 'DjangoDB',
    'USER': 'django',
    'PASSWORD': 'centos',
    'PORT': '3306',
    }
    }

3. Install django database driver pymysql

pip3 pymysql

Add in init.py of the same name folder of the django project

    import pymysql
    pymysql.install_as_MySQLdb()

4. register app
Add the app name to set.py
as follows

    INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'HostDB',
    ]

Otherwise, the command cannot recognize models.py in aap

5. Generating table structure

python manage.py makemigrations appName
python manage.py migrate appName

Posted by ultrasound0000 on Fri, 03 May 2019 14:30:38 -0700