Django用shopify Webhooks发出信号 [英] Django signals with shopify webhooks
问题描述
我是Django信号和Shopify Webhooks的新手,但我想在项目中实现此功能.
I am new to Django signals and Shopify webhooks, but I want to implement this feature in to a project.
我正在使用此程序包(其中还包括一组WebhookSignals)来接收并验证Shopify Webhook,但是然后我想对收到的信息进行处理(具体来说,我想处理订单的客户信息并将其存储在数据库中).
I am using this package, which also includes a set of WebhookSignals, to receive and verify the Shopify webhook, but then I want to do stuff with the information I receive (to be specific, I want to handle the customer information of an order and store it in a databse).
我认为我需要使用提供的信号来执行此操作,但是我并不真正了解该怎么做.到目前为止,我已经尝试在我的项目目录中(与settings.py一起)放置一个signals.py文件,该文件看起来如下:
I believe I need to use the provided signals to do this, but I don't really understand how to. So far, I've tried to put a signals.py file in my project directory (together with settings.py) that looks the following:
from shopify_webhook.signals import orders_create
def my_callback(sender, **kwargs):
print("Request finished!")
orders_create.connect(my_callback)
这显然行不通,但是当我从Shopify收到一个Webhook时,我将如何定义一个被调用的函数?
This obviously doesn't work, but how would I define a function that gets called whenever I receive a webhook from Shopify?
推荐答案
For what it's worth, I'd recommend just using the @webhook
decorator directly on a view instead of unnecessarily complicating things with signals.
这是您的视图外观:
from shopify_webhook.decorators import webhook
from myapp.models import AuthAppShopUser
@webhook
def orders_create(request):
user = AuthAppShopUser.objects.get(myshopify_domain=request.webhook_domain)
order_data = request.webhook_data
# The rest of your view here
上面的示例假设您正在使用 django-shopify-auth 用户身份验证,并已根据其文档设置了用户模型 AuthAppShopUser
.您还需要确保已将视图注册到 urls.py
中的url模式,并且还通过Shopify API将Webhook注册到了商店.
The above example assumes that you're using django-shopify-auth for your user authentication, and have set up the user model AuthAppShopUser
in accordance to its documentation. You'll also want to be sure that you've registered the view to a url pattern within your urls.py
, and also registered the webhook to the store via the Shopify API.
这篇关于Django用shopify Webhooks发出信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!