Django is a Python-based open-source Web framework. This topic describes how to use Django to create an application, link a MySQL database to the application, and deploy the application in Web+.

Step 1: Install Django

Use the following command to install Django. You need to install the pymysql package. It includes MySQL, which will be used later to link the application.
pip install Django pymysql 
Notice Before installing Django 2.2 or later, you need to install a version that is later than Python 3.5. This topic takes Python 3.7.4 as an example.

Step 2: Create an application.

  1. Use the following django-admin command to create a project.
    django-admin startproject webplusdemo
  2. The structure of the new project folder is listed as follows:
    webplusdemo/
    ├── manage.py
    └── webplusdemo
        ├── __init__.py
        ├── settings.py
        ├── urls.py
        └── wsgi.py
  3. Use the following django-admin command to create a project.
    django-admin startproject webplusdemo

Step 3: Create a deployment package

  1. Use the following command to modify the ALLOWED_HOSTS setting in the settings.py file to allow access from all domains.
    ALLOWED_HOSTS = ['*']
  2. Use the following commands to modify database settings in the settings.py file. Django uses SQLite databases by default. This topic takes an ApsaraDB for RDS instance with a MySQL database engine as an example.
    # Database
    # https://docs.djangoproject.com/en/2.2/ref/settings/#databases
    
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': os.environ['WP_RDS_DATABASE'],
            'USER': os.environ['WP_RDS_ACCOUNT_NAME'],
            'PASSWORD': os.environ['WP_RDS_ACCOUNT_PASSWORD'],
            'HOST': os.environ['WP_RDS_CONNECTION_ADDRESS'],
            'PORT': os.environ['WP_RDS_PORT'],
        }
    }
  3. Open the webplusdemo folder, use the following command to archive the directory and create a deployment package named webplusdemo.zip.
    zip -r webplusdemo.zip . /

Step 4: Deploy the application in Web+

  1. Log on to the Web+ console.
  2. On the Overview page, click Create in the Last Updated Environments section.
  3. In the Basic Information step, select Python in the Technology Stack Type field and enter an application name and description. After the configuration is complete, click Next.
  4. In the Deployment Information step, enter a environment name, select Upload Local Application in the Deployment Package Source field, and upload the webplusdemo.zip deployment package. After configuring the deployment package version, click Next.
  5. In the Configurations step, select Custom in the Predefined Configuration setting.
  6. View the ApsaraDB for RDS field and select MySQL in the Database Type setting. Then, configure the other required settings, such as the database version, database edition, and instance type.
  7. View the Lifecycle Hooks setting, and enter the following command in the PostPrepareApp field. After the configuration is complete, click Creation Complete.
    source /etc/bashrc && cd $APP_HOME && python manage.py migrate
  8. In the Creation Complete step, click View Application or Creation Complete to go to the Overview tab of the Application Details page. Click the name of an environment to go to the Overview tab of the Environment Details page. Then, click the link next to the Public IP Address label to view the homepage of the application.

    After the database is configured in the preceding steps, you can view the logon page of the application. You can add information about a user to the user table of the database or use python manage.py createsupersuer command to create a new user.

More information