Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does json.dumps() throw a 500 internal server error when using variables?
    text
    copied!<p>I've tested this out thoroughly and I have no idea why it's doing this, so I was wondering if someone could explain...</p> <p>json.dumps() is throwing a 500 internal server error when I try to use a variable instead of a string.</p> <p>For example:</p> <pre><code>error = 'Login Failed: Your username or password is incorrect.' test = json.dumps({'form': error}) </code></pre> <p>will return a 500 internal server error when attempting to return through jQuery.ajax</p> <p>However,</p> <pre><code>test = json.dumps({'form': 'Login Failed: Your username or password is incorrect.'}) </code></pre> <p>will not throw a 500 internal server error. I've tested this out thoroughly with single quotes, double quotes, different formatting, etc and it all points to json.dumps()</p> <p>For some reason, this is only occurring when calling the view through jQuery.ajax</p> <p><em><strong></em> even if i don't return json.dump() and return a normal json object it still throws a 500 error, which is why I've concluded that it's json.dumps() and not the .ajax call **</strong></p> <p>Here's an extra snippet of code where I'm using this: For unknown reasons.. this will throw an error while calling this particular page through jQuery.ajax()</p> <p>Yes.. even though the variable 'test' is not being returned.</p> <pre><code> elif(password == 'testing_this'): error = 'Login Failed: Your username or password is incorrect.' errors = True test = json.dumps({'form': error}) #print test date = datetime.date.today() if(errors == True): return HttpResponse(json.dumps({'form': 'error message here'}), mimetype='application/json') else: return HttpResponse(json.dumps({'result': True}), mimetype='application/json') </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload