Ajax Calling View URL

Hi,

I’m receiving a 500 error for an Ajax call that is calling a view to create a row in a model. I think it might be an easy fix but the 500 only comes when I try to call the create row line. Wondering if anyone could help

The VIew


def addUpdateExerciseComplex(request):
    if request.method == "POST":
        exerciseName = request.POST.get('exceriseComplex')
        complexName = request.POST.get('complexName')
        reps = request.POST.get('reps')
        order = request.POST.get('Order')
        sets = request.POST.get('sets')
        ExerciseComplex.objects.create(exerciseName=exerciseName, complexName=complexName, reps=reps, order=order, sets=sets)
    return HttpResponse('')

The Javascript

<script>
  function addUpdateExerciseComplex() {
    let csrftoken = '{{ csrf_token }}'
    $.ajax({
      type: "POST",
      url: "/exercises/builder/update/addUpdateExerciseComplex",
      headers: {'X-CSRFToken':csrftoken},
      data: {
          exceriseName: "Single Kettlebell Lunge",
          complexName: "4 Week W1D1, EMOM, 12, 4 Week W1D1",
          reps: "1",
          order: "3",
          sets: "2",
      },
      success: function () {
        console.log("complete")
      },
      error: function(data){
        alert("fail");
      }
    });
    return false;
  };
</script>

The Error is : jquery-3.7.1.min.js:2

   POST https://laingo93.pythonanywhere.com/exercises/builder/update/addUpdateExerciseComplex 500 (Internal Server Error)

If this is an accurate copy/paste of your code, then one of the issues is that the variable names you’re trying to GET from request.POST don’t match the names that you’re sending in the data object in your ajax POST.

If that’s not the only issue here, then we’ll need to see the complete error with the traceback from your server console.

Hi Ken,

That may have been an issue so thanks for that

I have had a bit more digging and guess its to do with setting foreign keys in models so have changed the code a bit and am getting the following error:

# Change Profile Stats
def addUpdateExerciseComplex(request):
    if request.method == "POST":
        exerciseName = request.POST.get('exerciseName')
        complexName = request.POST.get('complexName')
        reps = request.POST.get('reps')
        order = request.POST.get('Order')
        sets = request.POST.get('sets')
        ExerciseComplex.objects.get_or_create(exerciseName=Exercise(title=exerciseName),complexName=Complex(name=complexName),reps=reps, order=order, sets=sets)

Im receiving a no match found in the error, so I’m not sure if im sending my data correct in the Ajax?

Traceback (most recent call last):
File “/home/laingo93/.virtualenvs/venv/lib/python3.10/site-packages/django/db/models/query.py”, line 948, in get_or_create
return self.get(**kwargs), False
File “/home/laingo93/.virtualenvs/venv/lib/python3.10/site-packages/django/db/models/query.py”, line 649, in get
raise self.model.DoesNotExist(
exercises.models.ExerciseComplex.DoesNotExist: ExerciseComplex matching query does not exist.
NO MATCH
During handling of the above exception, another exception occurred:
NO MATCH
Traceback (most recent call last):
File “/home/laingo93/.virtualenvs/venv/lib/python3.10/site-packages/django/db/backends/base/base.py”, line 299, in _commit
sqlite3.IntegrityError: FOREIGN KEY constraint failed
NO MATCH
The above exception was the direct cause of the following exception:
NO MATCH

I would suggest you add some prints in your POST function to verify that the data you’re getting is what you’re expecting to get.

What i thought was correct data wasn’t. So yeah, data issue !