skip to Main Content

My signup form works fine when there are no errors and saves users correctly but i couldn’t display errors when there are form field errors after getting them with form.errors.as_json

here is the view of the form:

   if request.is_ajax():
       signup_form = SignupForm(request.POST)
       if signup_form.is_valid():
           signup_form.save()
           full_name = signup_form.cleaned_data.get('full_name')
           email = signup_form.cleaned_data.get('email')
           raw_password = signup_form.cleaned_data.get('password1')
           account = authenticate(email=email, password=raw_password)
           login(request, account)
           return JsonResponse({"success" : True}, status=200)
       else:
           return JsonResponse({'success': False},signup_form.errors.as_json() , status=400)

here are the ajax stuff in my javascript file:


 var $signupForm = $('#form-signup');
 $signupForm.submit(function (event) {
   event.preventDefault();
   var $signupData = $signupForm.serialize();
       $.ajax({
         url: "http://localhost:8000",
         method: 'POST',
         data: $signupData,
         success: function() {
           location.reload()
         },
         error: function () {
            // display form errors in a div or console.log them
            // by looping through them
       },
       });
     });

with this code when there are field inputs errors like "pasword is too short" or somthing like that i get error status code 500
and:

‘str’ object is not callable

in my django server terminal

i checked many question about this but none of them helped and most of them were outdated

3

Answers


  1. I think it’s an authentication problem.

    try to print after authentication.

    if it’s not try to print signup_form.errors.as_json() inside view.

    Login or Signup to reply.
  2. def sample(request):
        form = SampleForm(request.POST)
        if form.is_valid():
            return JsonResponse({
                'message': 'success'
            })
        
        return JsonResponse({
            'errors':form.errors,
            'message': 'invalid',    
        },
        status=422
        )
    
    Login or Signup to reply.
  3. You are have a problem concatenate problem in response.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search