This document describes how to quickly deploy a local Django project to the cloud through a web function.
This document mainly describes how to deploy in the console. You can also complete the deployment on the command line. For more information, please see Deploying Framework on Command Line.
Djangoin the search box, select Django Framework Template, and click Next as shown below:
Run the following command to confirm that Django has been installed in your local environment.
python -m pip install Django
Hello World sample project locally.
django-admin startproject helloworld && cd helloworld
The directory structure is as follows:
$ tree . manage.py Manager |--*** | |-- __init__.py Package | |-- settings.py Settings file | |-- urls.py Route | `-- wsgi.py Deployment
python manage.py runserver command locally to start the bootstrap file. Below is the sample code:
http://127.0.0.1:8000 in a browser, and you can access the sample Django project locally as shown below:
Next, perform the following steps to make simple modifications to the locally created project, so that it can be quickly deployed through a web function. The steps of project transformation for Django are as follows:
As the Django dependency library is not provided in the standard cloud environment of SCF, you must install the dependencies and upload them together with the project code. Please create the
requirements.txt file first with the following content:
Run the following command to install:
pip install -r requirements.txt -t .
As the initialized default project imports the
db.sqlite3library, please install this dependency synchronously or configure comments for the
DATABASESfield in the
setting.pyfile of the project.
scf_bootstrap bootstrap file
The listening port in the web function must be 9000, so you need to change the listening address and port in the following way: create the
scf_bootstrap bootstrap file in the project root directory and add the following content to it (which is used to configure environment variables, specify service bootstrap commands, and make sure that your service can be started normally through this file):
/var/lang/python3/bin/python3 manage.py runserver 9000
After the creation is completed, you need to run the following command to modify the executable permission of the file. By default, the permission
755 is required for it to start normally. Below is the sample code:
chmod 777 scf_bootstrap
- In the SCF environment, only files in the
/tmpdirectory are readable/writable. We recommend you select
/tmpwhen outputting files. If you select other directories, write will fail due to the lack of permissions.
- If you want to output environment variables in the log, you need to add the
-uparameter before the bootstrap command, such as
python -u app.py.
scf_bootstrapdirectory as an example) and make sure that your service can be normally started locally.
Be sure to change the
pythonpath to the local path during local testing.
After the deployment is completed, you can quickly access and test your web service in the SCF console and try out various features of SCF, such as layer binding and log management. In this way, you can enjoy the advantages of low cost and flexible scaling brought by the serverless architecture as shown below: