AttributeError: 'WSGIRequest' object has no attribute 'is_ajax'

I have included the following is_ajax() function, so that I can use it in another Django view function in the same file.

def is_ajax(request):
return request.META.get(‘HTTP_X_REQUESTED_WITH’) == ‘XMLHttpRequest’

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 file where the is_ajax() function is being called on line 48:

I will appreciate any suggestions on how to resolve this issue. Thank you.

Your reference to is_ajax should be a local function call passing the request as a parameter, not trying to call the function on the request object.

if is_ajax(request):

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.


Why is this the case please?

Please copy/paste the traceback (not a screen image) you’re getting for this issue. The image you’ve pasted is for the object has no attribute error.

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
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/", line 55, in inner
    response = get_response(request)
  File "/home/deniz/SeniorProject/django_project/vEnv/venv/lib/python3.8/site-packages/django/core/handlers/", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/deniz/SeniorProject/django_project/Exams/", 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/", 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/", line 512, in create
    obj = self.model(**kwargs)
  File "/home/deniz/SeniorProject/django_project/vEnv/venv/lib/python3.8/site-packages/django/db/models/", line 541, in __init__
    _setattr(self,, rel_obj)
  File "/home/deniz/SeniorProject/django_project/vEnv/venv/lib/python3.8/site-packages/django/db/models/fields/", 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:

The general rule when looking at a traceback is to find the last reference to your code - that’s usually the best first place to start.

In this case, that reference is:

This area of the code is the cause of this error. So the next step would be for you to post your save_exam_view code.

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.