I'm trying to create this Ajax request: The views file is as follows:
reports/views.py
from django.shortcuts import render
from profiles.models import Profile
from django.http import JsonResponse
from .utils import get_report_image
from .models import Report
from .forms import ReportForm
# Create your views here.
def create_report_view(request):
form = ReportForm(request.POST or None)
if request.is_ajax():
image = request.POST.get('image')
name = request.POST.get('name')
remarks = request.POST.get('remarks')
img = get_report_image(image)
author = Profile.objects.get(user=request.user)
if form.is_valid():
instance = form.save(commit=False)
instance.image = img
instance.author = author
instance.save()
# Report.objects.create(name=name, remarks=remarks, image=img, author=author)
return JsonResponse({'msg': 'send'})
return JsonResponse({})
utils.py
import base64, uuid
from django.core.files.base import ContentFile
def get_report_image(data):
_ , str_image = data.split(';base64')
decoded_img = base64.b64decode(str_image)
img_name = str(uuid.uuid4())[:10] + '.png'
data = ContentFile(decoded_img, name=img_name)
return data
urls.py
from django.urls import path
from .views import create_report_view
app_name = 'reports'
urlpatterns = [
path('save/', create_report_view, name='create-report'),
]
forms.py
from django import forms
from .models import Report
class ReportForm(forms.ModelForm):
class Meta:
model = Report
fields = ('name', 'remarks')
I'm not sure why but this is the error I'm getting. Does this mean that is_ajax() is no longer accepted with Django 4.1.1? If so how would I need to adjust the code?
if request.is_ajax():
AttributeError: 'WSGIRequest' object has no attribute 'is_ajax'
[21/Sep/2022 07:23:39] "POST /reports/save/ HTTP/1.1" 500 98007