我正在使用https://github.com/Shopify/shopify_django_app中的示例应用程序并尝试找到创建webhook的最佳位置。下面是示例代码中的finalize方法,我试图在创建会话后创建webhook。
def finalize(request):
shop_url = request.REQUEST.get('shop')
try:
shopify_session = shopify.Session(shop_url)
request.session['shopify'] = {
"shop_url": shop_url,
"access_token": shopify_session.request_token(request.REQUEST)
}
#create the webhooks
shopify.ShopifyResource.activate_session(shopify_session)
product_hook = shopify.Webhook()
product_hook.topic ='products/create'
product_hook.address = settings.SHOPIFY_APP_URL + '/notify/product/'
product_hook.format = 'json'
product_hook.save()
except Exception, err:
log.error(str(err))
messages.error(request, "Could not log in to Shopify store.")
return redirect(reverse('shopify_app.views.login'))
messages.info(request, "Logged in to shopify store.")
response = redirect(_return_address(request))
request.session.pop('return_to', None)
return response
我没有在日志中获得任何内容,因此它似乎已经过了webhook创建。但是,没有创建webhook,然后我收到Django错误:
NoReverseMatch at /finalize/
Reverse for 'root_path' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
对我所犯错误的任何见解将不胜感激。
以下是示例应用中的views.py:
from django.shortcuts import render_to_response, redirect
from django.contrib import messages
from django.core.urlresolvers import reverse
from django.template import RequestContext
from django.conf import settings
import shopify
def _return_address(request):
return request.session.get('return_to') or reverse('root_path')
def login(request):
# Ask user for their ${shop}.myshopify.com address
# If the ${shop}.myshopify.com address is already provided in the URL,
# just skip to authenticate
if request.REQUEST.get('shop'):
return authenticate(request)
return render_to_response('shopify_app/login.html', {},
context_instance=RequestContext(request))
def authenticate(request):
shop = request.REQUEST.get('shop')
if shop:
scope = settings.SHOPIFY_API_SCOPE
redirect_uri = request.build_absolute_uri(reverse('shopify_app.views.finalize'))
permission_url = shopify.Session(shop.strip()).create_permission_url(scope, redirect_uri)
return redirect(permission_url)
return redirect(_return_address(request))
def finalize(request):
shop_url = request.REQUEST.get('shop')
try:
shopify_session = shopify.Session(shop_url)
request.session['shopify'] = {
"shop_url": shop_url,
"access_token": shopify_session.request_token(request.REQUEST)
}
except Exception:
messages.error(request, "Could not log in to Shopify store.")
return redirect(reverse('shopify_app.views.login'))
messages.info(request, "Logged in to shopify store.")
response = redirect(_return_address(request))
request.session.pop('return_to', None)
return response
def logout(request):
request.session.pop('shopify', None)
messages.info(request, "Successfully logged out.")
return redirect(reverse('shopify_app.views.login'))
添加urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^$', 'shopify_app.views.login'),
url(r'^admin/', include(admin.site.urls)),
url(r'^login/', 'shopify_app.views.login'),
url(r'^authenticate/$', 'shopify_app.views.authenticate'),
url(r'^finalize/$', 'shopify_app.views.finalize'),
url(r'^logout/$', 'shopify_app.views.logout'),
)