【问题标题】:GET in Django working in local but not functioning on AWS ElasticBeanStalkDjango 中的 GET 在本地工作但在 AWS ElasticBeanStalk 上不起作用
【发布时间】:2018-07-25 16:32:45
【问题描述】:

我有一个过滤器,它仅在用户位于特定城市时从特定城市获取数据并将其呈现在 html 中。现在在我的本地机器上运行完美,我只能看到该特定城市的场馆、活动和用户。但是,当我尝试从 AWS 在 EC2 服务器上运行相同的过滤器设置时,我收到如下错误。由于某种原因,它无法识别请求中的密钥。如果从视图的场地和教师部分删除request.session['city-name']:,则页面加载时没有出现错误它向我显示来自所有城市,我绝对必须只有一个城市的数据。设置和获取使用 GET 的城市会话(视图的最后一部分)工作得很好,所以奇怪的是,当将 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)

【问题讨论】:

    标签: django get amazon-elastic-beanstalk


    【解决方案1】:

    我通过在视图开头添加此 sn-p 来解决此问题:

    def IndexPage(request):
    
        if 'city-name' not in request.session:
            cityName='Madrid'
            request.session['city-name'] = request.POST.get('cityName')
    
        else:
            cityName = request.session['city-name']
        if 'city-id' not in request.session:
            cityId = 1
            request.session['city-id'] = request.POST.get('cityId')
        else:
            cityId = request.session['city-id']
    

    然后错误消失了。我最初的努力并没有迎合 django 执行的查找,并且不得不迭代代码几次。去展示思考的价值通过

    【讨论】:

      猜你喜欢
      • 2020-12-24
      • 1970-01-01
      • 1970-01-01
      • 2019-04-20
      • 2017-06-05
      • 2023-03-12
      • 2019-10-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多