1

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 3

1
def sample(request):
    form = SampleForm(request.POST)
    if form.is_valid():
        return JsonResponse({
            'message': 'success'
        })
    
    return JsonResponse({
        'errors':form.errors,
        'message': 'invalid',    
    },
    status=422
    )
Sign up to request clarification or add additional context in comments.

8 Comments

can you explain more? since you did an answer not a comment!
Your error is like following, { 'message': 'success'} + {'error': '422'}
You can use only signup_form.errors.as_json() in response. Or { 'message': 'validationerror', 'error': dict(signup_form.errors.as_json())}
ok i'll try to applicate what you said but for better understanding please put what you are trying to say properly in you answer above
docs.djangoproject.com/en/3.1/ref/request-response/… . Second argument is not a json type. It is a encoder.
|
0

You are have a problem concatenate problem in response.

1 Comment

can you explain more? since you did an answer not a comment! , i didn't get what you are trying to say!
-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.

4 Comments

what do you mean try to print after authentication?, and i tried to print errors inside the view and it got printed successfully in the terminal, and my question was how loop through those errors in the javascript to display them
but you are getting error status code 500 it means some internal server error. so I think there is a problem inside the view. if you want to iterate inside ajax try for(let error of data){console.log(error)}
ok i'll try,btw when i try to print form.errors.as_json() they get printed correctly, and when i remove signup_form.errors.as_json() from the jsonresponse the error disappears
Be careful while concatenating dict in response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.