Ok, I just want to understand. After more than 1 hour debugging an entry point, after testing the api a dozen different times with Postman and making sure it works locally and then again, getting a weird Unicode
error on production. I found that if I remove a print statement, it works.
This is the relevant code of my entry point:
@csrf_exempt
def create_books(request):
sent_json = request.body
if not sent_json:
return HttpResponse("No json in request.body", status=404)
sent_json = json.loads(sent_json)
books = sent_json['books']
print "books: %s" % books
for num, book in books.iteritems():
title = book['title']
writer = book['writer']
if Book.objects.filter(titulo=title, writer=writer).exists():
book = Book.objects.get(titulo=title, writer=writer)
else:
book = Book.objects.create(titulo=title, writer=writer)
print "book.title: %s" % book.title # !!! ERROR
So ... when I print the dict like this:
print "books: %s" % books
Everything is fine, but when I print the book.title
print "book.title: %s" % book.title # !!! ERROR
I get a Unicode Error. The title that causes the error is of course contained in the books dictionary. But why does it give an error after it has being saved to the database and called as an attribute of the object?.
After I removed the 2nd print, everything was solved. But I don't understand.