So I am using Django and Python 3.9. I get a KeyError at /myapp/myroute
, to which it tells me the value of the key I tried. The problem is it points to line 929, which is else:
.
# this is the path to the main image
if "filename" in request.session:
if "file_url" in request.session:
ip = request.session["file_url"]
# error here - this is line 925 in my code
else:
s3 = boto3.client('s3')
ip = f"""{request.session["iam"]}/image/{request.session["filename"]}"""
request.session["file_url"] = ip
if ip != us.ImagePath:
change = True
else:
return redirect("index")
s3r = boto3.resource("s3")
obj = None
if "qrp" not in request.session:
request.session["qrp"] = ""
try:
request.session["qrp"] = f"""{request.session["iam"]}/sub_image/{str(request.session["usid"])}.png"""
obj = s3r.Object('iet-ticket-bucket', qrfn)
obj.load()
except botocore.exceptions.ClientError as e:
if e.response["Error"]["Code"] == "404":
img = q.make(json.dumps({"id": request.session["usid"], "storename": ""}))
iba = io.BytesIO()
img.save(iba, format="PNG")
s3 = boto3.client('s3')
s3.put_object(Bucket="iet-ticket-bucket", Key=request.session["qrp"], Body=iba.getvalue(), ContentType="image/png")
request.session["qrp"] = qrp
change = True
# where the error should be
qrp = request.session["qrp"]
I've done some googling, and granted I may be looking for the wrong thing, but does anyone have any possible suggestions as to what the problem is? If you could include any other similar problems and solutions to them in your answer, that'd be super helpful. Thank you.
Stack Trace:
Traceback (most recent call last):
File "/home/ec2-user/django/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
File "/home/ec2-user/django/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/ec2-user/DjangoProjects/appws/app/views.py", line 926, in thank_you
else:
File "/home/ec2-user/django/django/contrib/sessions/backends/base.py", line 53, in __getitem__
return self._session[key]
KeyError: 'qrp'
Answer for my specific case:
So I really don't fully understand what was happening, but here are a few more clues. I already have this set up behind apache - ie, I've completed the move to production steps. I was running systemctl start httpd
to restart (instead of systemctl restart httpd
- oops) the server, which explains why some of my code changes were being reflected, while others (for instance changing some of the settings.py configuration settings) were not reflected. I guess the output from django runs a cat
on the erroring file and head
s and tail
s it to output it to the webpage (and due to using start
and not restart
), so that's why the lines where jumping around and displaying the wrong error? Anyways, the error is being thrown on the right line.