我有一个过滤器,当用户在该特定城市并在html中呈现时,该过滤器仅从特定城市获取数据。现在在我的本地机器上运行完美,我只能看到来自该特定城市的场地,活动和用户。但是,当我尝试从AWS在EC2服务器上运行相同的过滤器设置时,我得到如下错误。由于某种原因,它无法识别请求中的密钥。如果从视图的场地和教师部分删除request.session['city-name']:
,则页面加载没有错误但是它显示我来自所有城市的数据,我必须拥有来自这个城市的数据。设置和获取使用GET的citysession(视图的最后一部分)工作正常,所以奇怪的是,当将GET连接到过滤器时,它不起作用。当它在当地完美运作时更是如此。
这是一个问题,例如数据库是如何设置的,还是AWS集成的问题?我还从下面的本地添加了工作视图。我怎么纠正这个?
KeyError at /
'city-name'
Request Method: GET
Request URL: http://xxxxxxxxxxxxxxxxxxxxxxxx
Django Version: 1.10.5
Exception Type: KeyError
Exception Value:
'city-name'
Exception Location: /opt/python/run/venv/local/lib/python2.7/site-packages/django/contrib/sessions/backends/base.py in __getitem__, line 57
Python Executable: /opt/python/run/venv/bin/python
Python Version: 2.7.12
Python Path:
['/opt/python/current/app/milingual',
'/opt/python/current/app',
'',
'/opt/python/run/venv/local/lib64/python2.7/site-packages',
'/opt/python/run/venv/local/lib/python2.7/site-packages',
'/opt/python/run/venv/lib64/python2.7',
'/opt/python/run/venv/lib/python2.7',
'/opt/python/run/venv/lib64/python2.7/site-packages',
'/opt/python/run/venv/lib/python2.7/site-packages',
'/opt/python/run/venv/lib64/python2.7/lib-dynload',
'/usr/lib64/python2.7',
'/usr/lib/python2.7',
'/opt/python/bundle/19/app']
Server time: Thu, 15 Feb 2018 00:02:09 +0000
Traceback:
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
39. response = get_response(request)
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in _legacy_get_response
249. response = self._get_response(request)
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/opt/python/current/app/app_core/views.py" in IndexPage
27. if Occurrence.objects.filter(date__gte=timezone.now()).filter(event__location=venue).exists() and venue.city.name==request.session['city-name']:
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/contrib/sessions/backends/base.py" in __getitem__
57. return self._session[key]
Exception Type: KeyError at /
Exception Value: 'city-name'
本地工作的view.py
#Landing Page
def IndexPage(request):
venues = ProfileVenue.objects.all().filter()
oc_list = []
for venue in venues:
if Occurrence.objects.filter(date__gte=timezone.now()).filter(event__location=venue).exists() and venue.city.name==request.session['city-name']:
oc = Occurrence.objects.all().filter(date__gte=timezone.now()).filter(event__location=venue)[:1].get()
oc_list.append(oc)
if len(oc_list) == 3: break
teachers = ProfileTeacher.objects.all().filter(published=True)[:3]
teachers_list = []
for teacher in teachers:
if teacher.city.name==request.session['city-name']:
teachers_list.append(teacher)
languages = Language.objects.all()
levels = LanguageLevel.objects.all()
events = EventType.objects.all()
context = {
'venues_today': oc_list,
'teachers': teachers_list,
'languages': languages,
'levels': levels,
'events': events
}
return render(request, "index.html", context)
def SetCitySession(request):
if request.method == "POST":
request.session['city-name'] = request.POST['cityName']
request.session['city-id'] = request.POST['cityId']
return JsonResponse({})
def GetCitySession(request):
if request.method == "GET":
cityName = request.session['city-name']
cityId = request.session['city-id']
context = {
"cityName": cityName,
"cityId": cityId
}
return JsonResponse(context)
答案 0 :(得分:0)
我通过在视图的开头添加此代码段来修复此问题:
$(document).ready(function () {
var data1 = [
["FINANCE"],
["SALE"],
["SALE3"]
];
var data2 = [
["FINANCE", "FINANCE1"],
["FINANCE", "FINANCE2"],
["SALE", "SALE1"],
["SALE", "SALE2"],
["SALE", "SALE3"],
["SALE", "SALE4"],
["SALE3", "NOSALE"]
]
var stringData = "";
var dyn_ul = "";
var dyn_li = '';
var dyn_div = '';
for (var i = 0; i < data1.length; i++) {
stringData = $("<li><a data-toggle='tab' href=#" + data1[i][0] + ">" + data1[i][0] + "</a></li>");
$(".list").append(stringData);
$(".list li:first-child").addClass("active");
dyn_div = "<div class='tab-pane fade' id=" + data1[i][0] + "><ul class='p nav nav-tabs'>";
for (var j = 0; j < data2.length; j++) {
if (data2[j][0] === data1[i][0]) {
dyn_div += "<li><a data-toggle='tab' href=#" + data2[j][1] + ">" + data2[j][1] + "</a></li>";
}
}
dyn_div += '</ul></div>';
$(".tab-content").append(dyn_div);
}
$(".p li:first-child").addClass("active");
$(".tab-content div.tab-pane:first-child").addClass("active in");
})
然后错误就消失了。我最初的努力并不适合查看django执行并且不得不迭代代码几次。通过
展示思考问题的价值