gpt4 book ai didi

django - 从另一个 View 调用服务器上的API

转载 作者:行者123 更新时间:2023-12-04 04:00:17 25 4
gpt4 key购买 nike

所以,这有点奇怪。我有一个使用TastyPie来为其API提供动力的Django项目,以及一些将用于为各种站点的插件提供动力的 View /模板。我被要求使用我们的API来处理对插件的请求,而不是将这些插件构建为标准的Django模板,这意味着我是从另一个 View 调用服务器上的一个 View ,由于某种原因,该 View 无法正常工作我有任何看法。以供引用:

#views.py
from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.template.context import Context, RequestContext
import json, urllib, urllib2

SITE_NAME = "http://localhost:8000/"
API_PATH = "api/v1/"

def aggregate(request):
template = 'lemonwise/plugins/aggregate.html'
sku = request.GET.get('sku')
url = ''.join([SITE_NAME, API_PATH, 'product/?sku=', sku])
product = json.loads(urllib.urlopen(url).read())['objects'][0]
return render_to_response(template, product)

def reviews(request):
template = 'lemonwise/plugins/reviews.html'
sku = request.GET.get('sku')
url = ''.join([SITE_NAME, API_PATH, 'product/?format=json&sku=', sku])
#Comment the next line out and the url is passed correctly
response = urllib2.build_opener().open(url).read()
return HttpResponse(url)
#page = opener.open(url).read()
#return HttpResponse(url)
#product = json.loads(urllib2.build_opener().open(url).read())['objects'][0]
#return HttpResponse(url)
#reviews = [json.loads(urllib.urlopen(SITE_NAME + uri)) for uri in product['reviews']]
#return render_to_response(template, {'reviews': reviews})

def survey(request):
template = 'lemonwise/plugins/survey.html'
sku = request.GET.get('sku')
url = ''.join([SITE_NAME, API_PATH, 'product/?sku=', sku])
product = json.loads(urllib2.build_opener().open(url).read())['objects'][0]
return render_to_response(template, product)

def mosthelpfulpositive(request):
template = 'lemonwise/plugins/mosthelpfulpositive.html'
sku = request.GET.get('sku')
url = ''.join([SITE_NAME, API_PATH, 'product/?sku=', sku])
product = json.loads(urllib2.build_opener().open(url).read())['objects'][0]
uri = product['most_helpful_positive']
most_helpful_positive = json.loads(urllib.urlopen(SITE_NAME + uri))
return render_to_response(template, most_helpful_positive)

def mosthelpfulnegative(request):
template = 'lemonwise/plugins/mosthelpfulnegative.html'
sku = request.GET.get('sku')
url = ''.join([SITE_NAME, API_PATH, 'product/?sku=', sku])
product = json.loads(urllib2.build_opener().open(url).read())['objects'][0]
uri = product['most_helpful_negative']
most_helpful_negative = json.loads(urllib.urlopen(SITE_NAME + uri))
return render_to_response(template, most_helpful_negative)

以及相应的urls.py(在其他应用中):
#urls.py    
from django.conf import settings
from django.conf.urls.defaults import patterns, include, url
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

from tastypie.api import Api
from lemonwise.reviews.api import *

admin.autodiscover()

v1_api = Api(api_name='v1')
v1_api.register(UserResource())
v1_api.register(MerchantResource())
v1_api.register(ProductFamilyResource())
v1_api.register(ProductResource())
v1_api.register(BooleanAttributeResource())
v1_api.register(SlideAttributeResource())
v1_api.register(ProductAttributeResource())
v1_api.register(SubmissionResource())
v1_api.register(ReviewResource())
v1_api.register(ReviewProductAttributeResource())
v1_api.register(CommentResource())
v1_api.register(BestUseResource())
v1_api.register(HelpfulVoteResource())

#Acess the api via http://127.0.0.1:8000/api/v1/user/?format=json

urlpatterns = patterns('',
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),

# Lemonwise apps
url(r'^reviews/', include('lemonwise.reviews.urls')),

#API
url(r'^api/', include(v1_api.urls)),
)

if settings.DEBUG:
urlpatterns += staticfiles_urlpatterns()

关于如何解决这个问题的任何想法?我找不到关于该主题的任何著作。

编辑:更具体地说,发生的是,当我加载这些 View 中的任何一个时,它们挂起以尝试读取api页。另外,我的服务器未显示任何正在处理的请求的指示。 (尽管我可以直接加载api页面。)

最佳答案

现在您的评论已经澄清了这种情况,我可以猜测问题出在哪里。这是因为您正在使用内置的单线程开发服务器。因此,在处理原始请求时,它无法处理对另一个URL的内部请求-因此它会无限期挂起。

正如毛指出的那样,解决方案是考虑一种更好的体系结构。如果您无法做到这一点,那么使用gunicorn之类的东西代替内置服务器可能会有些运气。

关于django - 从另一个 View 调用服务器上的API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6524709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com