繁体   English   中英

无服务器 Django 使用 Azure 函数

[英]Serverless Django using Azure Functions

我一直在尝试寻找在 Azure 函数的无服务器环境中运行 Django 的方法。

假设我只能使用 Azure 服务的约束,我想确保我们将编写的 Django 代码应该是可移植的(可以部署在其他任何地方)。

我一直在尝试几种方法,包括Azure 上的 Python:第 4 部分—运行无服务器 Django和无服务器运行框架,仍然无法正常运行。

我想确定,即使有人对运行 Django 无服务器有一个可行的想法,并且有一些关于良好资源的指导?

I don't know what you exactly want to do with Django, but I implemented Azure Functions to run Django commands (ie https://docs.djangoproject.com/en/3.1/howto/custom-management-commands/ ).

First I followed the steps to create a function app in a custom docker Linux container: https://docs.microsoft.com/de-de/azure/azure-functions/functions-create-function-linux-custom-image?tabs =bash%2Cportal&pivots=编程语言-python

This includes setting up the function app via the Azure functions CLI at the root of my Django project and a Docker container which is based on the docker image mcr.microsoft.com/azure-functions/python:2.0-python3.7-slim

然后我通过 CLI 创建了一个 Azure function:

func new --name my-function --template "Timer trigger"

并启动 Django 并调用自定义命令我的 function 代码看起来像

def main(mytimer: func.TimerRequest) -> None:
    utc_timestamp = datetime.datetime.utcnow().replace(
        tzinfo=datetime.timezone.utc).isoformat()

    if mytimer.past_due:
        logging.info('The timer is past due!')

    logging.info('Python timer trigger function started at %s', utc_timestamp)
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mydjango.settings')
    django.setup()
    call_command('my_command')  # possible to add command params here
    logging.info('Successfully run my command')

重要的部分是

    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mydjango.settings')
    django.setup()

在 function 代码中设置 Django。

也许它也有助于您的目的。 如果您有任何问题,我也可以为您提供更多详细信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM