This topic describes how to deploy a Django application to Function Compute. Function Compute provides a method different from traditional deployment methods. You can deploy a traditional Django application to Function Compute and use the application for production without performing operations such as purchasing a server. The deployed Django application can use the auto scaling, pay-as-you-go, and O&M-free features of Function Compute.

Prerequisites

The following operations are complete:

Background information

Django is a Python web framework. Developers can use Django and a little code to complete most of the content required for a website and further develop full-featured web services with ease. For more information, visit Django.

Procedure

  1. Run the following command in the specified directory to initialize a Django project:
    s init devsapp/start-django -d start-django
    The -d parameter specifies the project name. In this example, the project name is start-django.
    Note Skip this step if the item exists.
    Sample command output:
     Serverless Awesome: https://github.com/Serverless-Devs/package-awesome
    
     devsapp!start-django@0.0.14.zip file decompression completed
     please select credential alias default
    
        ______ _
        |  _  (_)
        | | | |_  __ _ _ __   __ _  ___
        | | | | |/ _` | '_ \ / _` |/ _ \
        | |/ /| | (_| | | | | (_| | (_) |
        |___/ | |\__,_|_| |_|\__, |\___/
             _/ |             __/ |
            |__/             |___/
    
    
        Welcome to the start-django application
         This application requires to open these services:
             FC : https://fc.console.aliyun.com/
             ACR: https://cr.console.aliyun.com/
         This application can help you quickly deploy the Django project:
             Full yaml configuration: https://github.com/devsapp/django#%E5%AE%8C%E6%95%B4yaml
             Django development docs: https://docs.djangoproject.com/en/3.2/
         This application homepage: https://github.com/devsapp/start-django
    
    
     Thanks for using Serverless-Devs
     You could [cd /test/start-django] and enjoy your serverless journey!
    ️ If you need help for this example, you can use [s -h] after you enter folder.
     Document Star: https://github.com/Serverless-Devs/Serverless-Devs
  2. Run the following command to go to the project directory:
    cd start-django
  3. Run the following command to deploy the project:
    s deploy
    Sample command output:
    [2021-09-03T11:49:11.322] [INFO ] [S-CLI] - Start ...
    [2021-09-03T11:49:11.332] [INFO ] [S-CLI] - Start the pre-action
    [2021-09-03T11:49:11.332] [INFO ] [S-CLI] - Action: pip3 install -t ./src -r ./src/requirements.txt -i https://mirrors.aliyun.com/pypi/simple/
    WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
    Collecting asgiref==3.3.1 (from -r ./src/requirements.txt (line 1))
      Using cached https://mirrors.aliyun.com/pypi/packages/89/49/5531992efc62f9c6d08a7199dc31176c8c60f7b2548c6ef245f96f29d0d9/asgiref-3.3.1-py3-none-any.whl
    Collecting Django==3.1.7 (from -r ./src/requirements.txt (line 2))
      Using cached https://mirrors.aliyun.com/pypi/packages/b8/6f/9a4415cc4fe9228e26ea53cf2005961799b2abb8da0411e519fdb74754fa/Django-3.1.7-py3-none-any.whl
    Collecting pytz==2021.1 (from -r ./src/requirements.txt (line 3))
      Using cached https://mirrors.aliyun.com/pypi/packages/70/94/784178ca5dd892a98f113cdd923372024dc04b8d40abe77ca76b5fb90ca6/pytz-2021.1-py2.py3-none-any.whl
    Collecting sqlparse==0.4.1 (from -r ./src/requirements.txt (line 4))
      Using cached https://mirrors.aliyun.com/pypi/packages/14/05/6e8eb62ca685b10e34051a80d7ea94b7137369d8c0be5c3b9d9b6e3f5dae/sqlparse-0.4.1-py3-none-any.whl
    Installing collected packages: asgiref, pytz, sqlparse, Django
    Successfully installed Django-3.1.7 asgiref-3.3.1 pytz-2021.1 sqlparse-0.4.1
    Target directory /test/start-django/src/asgiref-3.3.1.dist-info already exists. Specify --upgrade to force replacement.
    Target directory /test/start-django/src/sqlparse-0.4.1.dist-info already exists. Specify --upgrade to force replacement.
    Target directory /test/start-django/src/asgiref already exists. Specify --upgrade to force replacement.
    Target directory /test/start-django/src/django already exists. Specify --upgrade to force replacement.
    Target directory /test/start-django/src/sqlparse already exists. Specify --upgrade to force replacement.
    Target directory /test/start-django/src/pytz already exists. Specify --upgrade to force replacement.
    Target directory /test/start-django/src/pytz-2021.1.dist-info already exists. Specify --upgrade to force replacement.
    Target directory /test/start-django/src/Django-3.1.7.dist-info already exists. Specify --upgrade to force replacement.
    [2021-09-03T11:49:14.648] [INFO ] [S-CLI] - End the pre-action
      Using web framework type: nas, If you want to deploy with container, you can [s cli fc-default set web-framework container] to switch.
    (node:2908) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
    (Use `node --trace-deprecation ...` to show where the warning was created)
      End of request
      Deployed.
      End of request
    [2021-09-03T11:49:19.688] [INFO ] [FC-DEPLOY] - Using region: cn-shenzhen
    [2021-09-03T11:49:19.690] [INFO ] [FC-DEPLOY] - Using access alias: default
    [2021-09-03T11:49:19.690] [INFO ] [FC-DEPLOY] - Using accessKeyID: 188077086902****
    [2021-09-03T11:49:19.690] [INFO ] [FC-DEPLOY] - Using accessKeySecret: KdcUEJLoGto****
      Using fc deploy type: sdk, If you want to deploy with pulumi, you can [s cli fc-default set deploy-type pulumi] to switch.
    [2021-09-03T11:49:19.936] [INFO ] [FC-DEPLOY] - Checking Service serverless-devs-django exists
    [2021-09-03T11:49:20.070] [INFO ] [FC-DEPLOY] - Setting role: AliyunFCDefaultRole
    [2021-09-03T11:49:20.249] [INFO ] [RAM] - Checking Role AliyunFCDefaultRole exists
    [2021-09-03T11:49:20.346] [INFO ] [RAM] - Updating role: AliyunFCDefaultRole
    [2021-09-03T11:49:20.462] [INFO ] [RAM] - Checking Plicy AliyunFCDefaultRolePolicy exists
    [2021-09-03T11:49:20.562] [INFO ] [FC-DEPLOY] - Using vpcConfig: auto: fc will try to generate related vpc resources automatically
    [2021-09-03T11:49:21.094] [INFO ] [VPC] - Getting vpc: fc-deploy-component-generated-vpc-cn-shenzhen
    [2021-09-03T11:49:21.192] [INFO ] [VPC] - Getting vswitch: fc-deploy-component-generated-vswitch-cn-shenzhen
    [2021-09-03T11:49:21.358] [INFO ] [VPC] - Getting securityGroup: fc-deploy-component-generated-securityGroup-cn-shenzhen
    [2021-09-03T11:49:21.484] [INFO ] [FC-DEPLOY] - Generated vpcConfig:
    securityGroupId: sg-wz92ctigpi221sm****
    vSwitchId: vsw-wz9qnuult4q5g4f7n****
    vpcId: vpc-wz9x9bzs0wtvjgt6n****
    
    [2021-09-03T11:49:21.484] [INFO ] [FC-DEPLOY] - Using nasConfig: auto: fc will try to generate related nas file system automatically
    [2021-09-03T11:49:38.106] [INFO ] [FC-DEPLOY] - Generated nasConfig:
    groupId: 10003
    mountPoints:
      - fcDir: /mnt/auto
        nasDir: /serverless-devs-django
        serverAddr: example.com
    userId: 10003
    
    [2021-09-03T11:49:38.108] [INFO ] [FC-DEPLOY] - Checking Function django exists
    [2021-09-03T11:49:38.249] [WARN ] [FC-DEPLOY] - Image registry.cn-shenzhen.aliyuncs.com/web-framework/python3:0.0.1 dose not exist locally.
    Maybe you need to run 's build' first if it dose not exist remotely.
    [2021-09-03T11:49:38.252] [INFO ] [FC-DEPLOY] - Checking Trigger serverless-devs-django exists
    [2021-09-03T11:49:38.345] [INFO ] [FC-DEPLOY] - Checking Trigger serverless-devs-django exists
    [2021-09-03T11:49:38.445] [INFO ] [FC-DEPLOY] - Creating service: serverless-devs-django
    [2021-09-03T11:49:38.446] [INFO ] [FC-DEPLOY] - Creating function: django
    [2021-09-03T11:49:38.446] [INFO ] [FC-DEPLOY] - Creating triggers: ["serverless-devs-django"]
      Make service serverless-devs-django success.
      Make function serverless-devs-django/django success.
      Make trigger serverless-devs-django/django/serverless-devs-django success.
    [2021-09-03T11:49:41.163] [INFO ] [FC-DEPLOY] - Checking Service serverless-devs-django exists
    [2021-09-03T11:49:41.246] [INFO ] [FC-DEPLOY] - Checking Function django exists
    [2021-09-03T11:49:41.376] [INFO ] [FC-DEPLOY] - Checking Trigger serverless-devs-django exists
    [2021-09-03T11:49:41.563] [INFO ] [FC-DEPLOY] - Creating custom domain: django.serverless-devs-django. 188077086902****.cn-shenzhen.fc.devsapp.net
    [2021-09-03T11:49:42.283] [INFO ] [FC-DOMAIN] - Creating custom domain: django.serverless-devs-django. 188077086902****.cn-shenzhen.fc.devsapp.net
    
    There is auto config in the service: serverless-devs-django
    [2021-09-03T11:49:42.392] [INFO ] [WEB-FRAMEWORK] - nas component get src is: ./src
    Packing ...
    Package complete.
      Start uploading file: /mnt/2bfb748534-tpm20-cn-shenzhen/serverless-devs-django/django/src.zip
      File uploaded successfully: /mnt/2bfb748534-tpm20-cn-shenzhen/serverless-devs-django/django/src.zip
      upload done
    [2021-09-03T11:50:57.611] [INFO ] [FC-NAS] - unzipping file
    [2021-09-03T11:51:12.460] [INFO ] [FC-NAS] - unzip done
      Try container acceleration
    framework-test:
      region: cn-shenzhen
      serviceName: serverless-devs-django
      functionName: django
      customDomains:
        - >-
          http://django.serverless-devs-django. 188077086902****.cn-shenzhen.fc.devsapp.net

Result

After the project is deployed, you can view the temporary domain name generated by Function Compute in the execution output. The temporary domain name can be used to access the newly deployed application. For example, run the following command to access the application:

curl http://django.serverless-devs-django.188077086902****.cn-shenzhen.fc.devsapp.net
Note The temporary domain name is used only for demonstration and development purposes. It is valid for a limited period of time. You must use a domain name that has obtained an Internet Content Provider (ICP) filing from Alibaba Cloud for production. For more information, see Configure a custom domain name.