Skip to main content

Django

Django is a powerful Python Web framework that follows the "Batteries-included" philosophy, delivering an out-of-the-box full-stack solution. Renowned for efficient development, security, and stability, it features built-in modules like ORM, Admin backend, and user authentication, significantly reducing repetitive code. Django employs a clear MVC (MTV) architecture with high scalability, suitable for projects ranging from rapid prototyping to enterprise-level application development. Its automated admin interface and comprehensive documentation further enhance development efficiency.

This guide describes how to deploy the sample Django application on Tencent CloudBase in multiple ways:

Create a Django application

Note: If you already have an existing Django application, you can skip this step.

To create a new Django application, ensure that you have installed Python and Django on your machine.

Set up the project in the directory by following the steps below.

Create a virtual environment

python -m venv env

Activate the virtual environment

source env/bin/activate

Install Django

python -m pip install django

Once everything is set up, run the following command in the terminal to configure a new Django project:

django-admin startproject cloudrun-django

This command will create a new project named cloudrun-django.

Next, cd into the directory and run python manage.py runserver to start the project.

Open your browser and go to http://127.0.0.1:8000, and you will see the Django welcome page.

Configuring Dependencies

Create the requirements.txt file: To track all dependencies for deployment, create a requirements.txt file:

pip freeze > requirements.txt

Note: It is safe to run the above command only in a virtual environment; otherwise, it will generate all installed python packages on the system. This may result in the application failing to start on cloud hosting.

Configuring Dockerfile

Create a Dockerfile file in the root directory of the Django application with the following content:

FROM alpine:3.21.3

# The container's default time zone is UTC. To use Shanghai time, enable the following time zone setting command.
RUN apk add tzdata && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo Asia/Shanghai > /etc/timezone

# Use a domestic mirror source to improve download speed.
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tencent.com/g' /etc/apk/repositories \
&& apk add --update --no-cache python3 py3-pip gcc python3-dev linux-headers musl-dev \
&& rm -rf /var/cache/apk/*

# Using the HTTPS Protocol to Access Container Cloud for Certificate Installation Invocation
RUN apk add ca-certificates

# Copy the current project to the /app directory (excluding files specified in .dockerignore)
COPY . /app

# Set Current Working Directory
WORKDIR /app

# Install dependencies to the specified /install folder
# Use a domestic mirror source to improve download speed.
RUN pip config set global.index-url http://mirrors.cloud.tencent.com/pypi/simple \
&& pip config set global.trusted-host mirrors.cloud.tencent.com \
&& pip install --upgrade pip --break-system-packages \
# If pip install scipy and other math packages fails, you can use apk add py3-scipy instead. Refer to https://pkgs.alpinelinux.org/packages?name=py3-scipy&branch=v3.13 for installation.
&& pip install --user -r requirements.txt --break-system-packages

# Execute the startup command.
# Writing multiple independent CMD commands is incorrect! Only the last CMD command will be executed, and all previous ones will be ignored, causing application errors.
# Refer to [Docker official documentation on CMD command](https://docs.docker.com/engine/reference/builder/#cmd)
CMD ["python3", "manage.py", "runserver","0.0.0.0:8080"]

With the above changes, your Django application will be ready to deploy on Tencent CloudBase!

Deployment to Cloud Hosting

Cloud Hosting provides multiple deployment methods to deploy your application:

Deploy via Console

Open Tencent CloudBase, click Deploy via local code -> enter the service name -> select Upload code package for the deployment method -> select Folder for the code package type -> choose the cloudrun-django directory for upload -> enter 8080 for the port -> click Create and wait for the creation to complete.

Deploying via cli

If you have installed CloudBase CLI, you can use the following command in the project directory to deploy the application to CloudBase Cloud Hosting:

tcb cloudrun deploy -p 8080

After entering the environment and service names, the CLI will automatically package the application image and deploy it to Cloud Hosting.

Besides manual deployment, you can also install the aforementioned applications with one click:

One-Click Deployment from Template

One-Click Deployment from github