django creates the first project

Keywords: Django Database pip MySQL

django creates the first project, using pycharm, and the virtual machine uses ubantu

Create command (if Django is installed)

Have a project and a virtual environment awareness.

You can install Django in the virtual environment if you don't install it

Virtual environment construction

pip install virtualenvwrapper

Mkvirtualenv < name of virtual environment >

Then pip install django

Create a folder, cd it into the folder, and then:

Django admin startproject < project name >

configuration information

  1. Create static, set to root
  2. Create apps and set it as root directory
  3. Add a * sign to ALLOW_USER [] in settings.
  4. Configure log information
  5. Configuration database information

Configure static path

Be sure to configure its path, otherwise the file will not recognize the file in static.

Under settings, / static /, add if you import the os module and sys module.

sys.path.insert(0, BASE_DIR)
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),# For static files
]

Right click the static file to set it as the root directory

apps file

The configuration method is as follows:

  1. Set it as root first
  2. In settings, import the sys module and add it. Note that it must be added before the installed APUs
sys.path.insert(0, BASE_DIR)
sys.path.insert(1, os.path.join(BASE_DIR, 'apps'))

Configure templets file

Create a Templates folder before setting it to templates file type
Then configure in the settings file:

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',
            ],
            'builtins': ['django.templatetags.static'],
        },
    },
]

Database configuration

Create the database and give the user permission first

1. CREATE USER: CREATE USER 'username' @ 'host' IDENTIFIED BY 'password';

username: user name; host: specify which host to log in to, localhost is available for this machine,% all remote hosts can be configured; password: user login password;

2. Authorization: grant all privileges on. To 'username' @ '%' IDENTIFIED BY 'password';

Refresh permissions: FLUSH PRIVILEGES;

Format: grant permission on database name. Table name to user @ Login Host identified by "user password";. Represents ownership;

@After that is the IP address (or host name) of the client accessing mysql, which represents any client. If localhost is filled in as local access (then this user cannot access the MySQL database remotely).

Reprinted to: https://blog.csdn.net/wzy weiziyang/article/details/78855775

pip install pymysql first

Then configure it in the init file in the project file

import pymysql
pymysql.install_as_MySQLdb()

Configure database information in settings

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',   # database engine
        'NAME': '',                       # Database name
        'USER': '',                      # Database login user name
        'PASSWORD': '',                # Password
        'HOST': '192.168.216.137',                # IP address of database host, 127.0.0.1 if it remains the default
        'PORT': 3306,                           # Database port number, 3306 if it remains the default
    }
}

Configure Redis cache

PIP install - I https://pypi.doublan.com/simple django-redis

Then configure it in settings

CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379/0", # The last one is database information. There are 0-15 databases. ip also needs to be changed. The default is 127.0.0.1, and port forwarding needs to be configured
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
        }
    },
}

Configure logs file

There is a bug.

If the error is reported that the logs directory cannot be found, check whether the directory is correct.

step

  1. First, create the logs folder in the same level directory, which creates a file ending with cnf
  2. Configure in settings
LOGGING = {
    # Edition
    'version': 1,
    # Disable existing loggers
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(lineno)d %(message)s'
        },
        'simple': {
            'format': '%(levelname)s %(module)s %(lineno)d %(message)s'
        },
    },
    'filters': {
        'require_debug_true': {
            '()': 'django.utils.log.RequireDebugTrue',
        },
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'filters': ['require_debug_true'],
            'class': 'logging.StreamHandler',
            'formatter': 'simple'
        },
        'file': {
            'level': 'INFO',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': os.path.join(BASE_DIR, "logs/jiang.log"),  # The location of the log file. This is very important. You can't type it wrong.
            'maxBytes': 300 * 1024 * 1024,
            'backupCount': 10,
            'formatter': 'verbose'
        },
    },
    'loggers': {
        'django': {  # A logger named django is defined
            'handlers': ['console', 'file'],
            'propagate': True,
            'level': 'INFO',  # Minimum log level received by the logger
        },
    }
}

Timezone configuration

# update language
LANGUAGE_CODE = 'zh-hans'
# Modify time zone
TIME_ZONE = 'Asia/Shanghai'

USE_I18N = True

USE_L10N = True
# utc time
USE_TZ = True

Upload to git

  1. Install git at https://blog.csdn.net/suerimn/article/details/94583141

  2. upload

When uploading, please refer to: https://www.cnblogs.com/Steffen-xuan/p/11193248.html. Note: choose HTTPS, then cd to your project directory, and then follow the process given to you by github official website. You can't skip!!

Published 6 original articles, won praise 0, visited 88
Private letter follow

Posted by hypuk on Sat, 08 Feb 2020 01:07:17 -0800