简体   繁体   English

如何在应用程序启动时向 EndPoint 发送请求

[英]How do I Send Request to EndPoint on application Startup

I am working on a Django project which has an API endpoint that receives a post request and sends a welcome email to the registered user, currently, I have to use a form to send this request to the endpoint, is there a way to manually read the email and name from my environmental variable and send a request once I run the app the first time?我正在开发一个 Django 项目,该项目有一个 API 端点,该端点接收发布请求并向注册用户发送欢迎电子邮件,目前,我必须使用表单将此请求发送到端点,有没有办法手动读取我的环境变量中的电子邮件和名称,并在我第一次运行应用程序时发送请求? something like就像是

EMAIL = 'try@test.com'
NAME = 'Bob'

I have this stored as an env variable already我已经将它存储为环境变量

and here is my current code这是我当前的代码

@require_http_methods(["POST"])
@login_required
def add_user(request):
    if request.is_ajax():
        name = request.POST.get('name')
        email = request.POST.get('email')
        if not BlueUsers.objects.filter(user_email=email).exists():
            newuser_obj = BlueUsers.objects.create(user_name=name, user_email=email)
            conf_obj = Config.objects.first()
            if conf_obj:
                post_url = "{}/priv/create-user/".format(conf_obj.hostname)
                data = {
                    'name': newuser_obj.user_name,
                    'email': newuser_obj.user_email,
                    'redtree_user_id': newuser_obj.id
                }
                headers = {'data-auth-key': conf_obj.authentication_token}
                try:
                    response = requests.post(post_url, data=data, headers=headers)
                except:
                    response = None

I have been struggling with this我一直在努力解决这个问题

There is a file name apps.py that loads your app configuration and runs code on app startup.有一个名为apps.py的文件,用于加载您的应用配置并在应用启动时运行代码。

Your purpose should be served by following piece of code您的目的应该通过以下代码实现

class MyAppConfig(AppConfig):
    name = "myapp"

    def ready(self):
        # your model and other imports here

        email = os.environ.get('EMAIL')
        name = os.environ.get('NAME')

        if not BlueUsers.objects.filter(user_email=email).exists():
            newuser_obj = BlueUsers.objects.create(user_name=name, user_email=email)
            conf_obj = Config.objects.first()
            if conf_obj:
                post_url = "{}/priv/create-user/".format(conf_obj.hostname)
                data = {
                    'name': newuser_obj.user_name,
                    'email': newuser_obj.user_email,
                    'redtree_user_id': newuser_obj.id
                }
                headers = {'data-auth-key': conf_obj.authentication_token}
                try:
                    response = requests.post(post_url, data=data, headers=headers)
                except:
                    response = None

Any logic that you write inside the ready method of your AppConfig class is going to be executed once on each startup.您在 AppConfig 类的ready方法中编写的任何逻辑都将在每次启动时执行一次。

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

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