However, the problem is that I am still getting the following error message in the terminal:
AttributeError: ‘WSGIRequest’ object has no attribute ‘is_ajax’
The following image shows the views.py file where the is_ajax() function is being called on line 48:
When I attempt this, for some reason I get the following error in the terminal:
ValueError: Cannot assign “<SimpleLazyObject: <django.contrib.auth.models.AnonymousUser object at 0x7f75e744f610>>”: “ExamResult.user” must be a “User” instance.
The following is the trackback when I click the “Save” button:
Django version 4.0.5, using settings 'django_project.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[]
Internal Server Error: /exams/2/save/
Traceback (most recent call last):
File "/home/deniz/SeniorProject/django_project/vEnv/venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
File "/home/deniz/SeniorProject/django_project/vEnv/venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/deniz/SeniorProject/django_project/Exams/views.py", line 96, in save_exam_view
ExamResult.objects.create(exam=exam, user=user, score=score_)
File "/home/deniz/SeniorProject/django_project/vEnv/venv/lib/python3.8/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/deniz/SeniorProject/django_project/vEnv/venv/lib/python3.8/site-packages/django/db/models/query.py", line 512, in create
obj = self.model(**kwargs)
File "/home/deniz/SeniorProject/django_project/vEnv/venv/lib/python3.8/site-packages/django/db/models/base.py", line 541, in __init__
_setattr(self, field.name, rel_obj)
File "/home/deniz/SeniorProject/django_project/vEnv/venv/lib/python3.8/site-packages/django/db/models/fields/related_descriptors.py", line 235, in __set__
raise ValueError(
ValueError: Cannot assign "<SimpleLazyObject: <django.contrib.auth.models.AnonymousUser object at 0x7f8a12d34880>>": "ExamResult.user" must be a "User" instance.
[09/Jul/2022 23:39:58] "POST /exams/2/save/ HTTP/1.1" 500 91098
The following is how the “Save” button on the web page looks:
Looking at this a bit closer, I see the error message is saying:
ValueError: Cannot assign “<SimpleLazyObject: <django.contrib.auth.models.AnonymousUser object at 0x7f8a12d34880>>”: “ExamResult.user” must be a “User” instance.
It appears that this request is coming from an “unauthenticated” request. Is this user logged in? If not, then you don’t have a user object in the request. (Or more accurately, what you have is an instance of the AnonymousUser.)
Yes. Good news! I was able to fix the problem. I had to change the name of a variable that linked to one of the attributes for a particular model. I appreciate your help with all of this Mr. Whitesell.
This isn’t quite the same issue. The HttpRequest.is_ajax method was deprecated in Django 3.1 and removed in Django 4.0.
Are you actually trying to call the is_ajax method that you defined above your view? If so, then you don’t reference it as request.is_ajax, it’s just is_ajax.