简体   繁体   中英

GET in Django working in local but not functioning on AWS ElasticBeanStalk

I have a filter that only takes data from a particular city when a user is in that particular city and renders it in the html. Now on my local machine this is running perfectly and I am able to see only venues, events and users from that particular city. However when I try run the same filter settings on the EC2 server from AWS I get the error as below. For some reason its failing to recognise the key in the request.If remove the request.session['city-name']: from both the venue and the teacher section of the view then the page loads without and error BUT it shows me data from all the cities and it is absolutely imperative that I only have data from the one city. To Set and Get citysession (last section of the view) which uses GET is working just fine so its strange that when connecting GET to the filter it then doesn't work. Even more so when it is working perfectly on local.

Is this a problem of how the database was setup for example or a problem with AWS integration or something? I've also added the working view from local below. Like how do I rectify this?

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'

The working view.py on local

#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)

I fixed this by adding this snippet at the beginning of my view:

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']

The error then disappeared. My initial efforts did not cater to look ups django perform and had to iterate the code a couple of times. Goes to show the value of thinking things through

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM