Python 使用WSGIRequest时,Google应用程序引擎Django 1.5.11上未填充X-AppEngine-CitylalLong
当我在Google app Engine上向我的WSGI Django 1.5.11 Python应用程序提交帖子时,我没有在帖子标题中返回X-AppEngine-citylalong(或City或Country)值 我的wsgi.py是典型的:Python 使用WSGIRequest时,Google应用程序引擎Django 1.5.11上未填充X-AppEngine-CitylalLong,python,django,google-app-engine,Python,Django,Google App Engine,当我在Google app Engine上向我的WSGI Django 1.5.11 Python应用程序提交帖子时,我没有在帖子标题中返回X-AppEngine-citylalong(或City或Country)值 我的wsgi.py是典型的: import os from django.core.wsgi import get_wsgi_application os.environ.setdefault("DJANGO_SETTINGS_MODULE", "web.settings") a
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "web.settings")
application = get_wsgi_application()
通过URL.py以正常方式从中获取请求对象。我的请求属性(如request.method等)都已正确填充。但是,当我查找GAE注入头以查找客户端的地理位置时:
@staticmethod
def get_demographics(request):
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
if x_forwarded_for:
ip = x_forwarded_for.split(',')[0]
else:
ip = request.META.get('REMOTE_ADDR')
if request.method == 'GET':
slatlong = request.GET.get("X-AppEngine-CityLatLong")
lat = slatlong.split(",")[0]
lng = slatlong.split(",")[1]
city = request.GET.get("X-AppEngine-City")
country = request.GET.get("X-AppEngine-Country")
elif request.method == 'POST':
slatlong = request.POST.get("X-AppEngine-CityLatLong")
lat = slatlong.split(",")[0]
lng = slatlong.split(",")[1]
city = request.POST.get("X-AppEngine-City")
country = request.POST.get("X-AppEngine-Country")
else:
lat = 0
lng = 0
city = 'unknown'
country = 'unknown'
return ip, lat, lng, city, country
每个request.POST.get,例如:
request.POST.get("X-AppEngine-CityLatLong")
返回一个非类型,而不是(在citylatelong的情况下)预期的逗号分隔字符串(例如“1.234,-5.678”)。这会导致用于分隔lat/long的后续字符串拆分抛出HTTP 500,其中包含:
“非类型”对象没有“拆分”属性
StackOverflow上的一些代码示例表示使用request.getHeader进行此操作,但当我这样做时,会得到一个HTTP 500,其中包含:
“WSGIRequest”对象没有属性“getHeader”
非常感谢任何解决方案
有关X-AppEngine标题的更多信息:更新:已修复 实际使用的调用有:
request.META.get('HTTP_X_APPENGINE_CITYLATLONG')
request.META.get('HTTP_X_APPENGINE_CITY')
request.META.get('HTTP_X_APPENGINE_COUNTRY')
request.META.get('HTTP_X_APPENGINE_REGION')
META
包含http头。你是说request.META.get()
?对不起,我忘了提到我尝试的前一个版本是request.META.get而不是request.POST.get。它也有同样的问题:分割失败,因为它也返回一个非类型。