Django如何使用Webhook和发布数据?

时间:2018-09-07 15:34:44

标签: django django-models django-views webhooks zapier

我对django webhooks感到困惑,因为我是python和django的初学者。我在文档中看到的大部分内容。我如何连接到我的应用程序并发布数据。谁能告诉我如何为我的数据设置网络挂钩。 我想将数据发布到zapier。

Models.py

class Driver(models.Model):
    class Meta():
        db_table = "driver"
        verbose_name = "Driver"
        verbose_name_plural = "Drivers"
        ordering = ['driver_firstname', 'driver_lastname']

    driver_firstname = models.CharField(
        max_length=64,
        blank=True,
        null=True
    )
    driver_lastname = models.CharField(
        max_length=64,
        blank=True,
        null=True
    )

    driver_email= models.CharField(
        max_length=64,
        blank=True,
        null=True

    )

Views.py

def drivers(request):
    if request.method == 'POST':
        if request.POST['driver_firstname'] and request.POST['driver_lastname'] and request.POST['driver_email']
            driver = Driver()
            driver.driver_firstname = request.POST['driver_firstname']
            driver.driver_lastname = request.POST['driver_lastname']
            driver.driver_email = request.POST['driver_email']
            driver.save()

1 个答案:

答案 0 :(得分:2)

如果要在应用程序外部发布数据,可以查看HTTP库Requests

例如:

import requests

driver_firstname = request.POST['driver_firstname']
driver_lastname = request.POST['driver_lastname']
driver_email = request.POST['driver_email']

payload = {'driver.driver_firstname': driver_firstname,
           'driver.driver_lastname': driver_lastname,
           'driver.driver_email': driver_email
           }

r = requests.post("http://your-url.org/post", data=payload)