JSONResponseMixin throws an exception in Django 1.5 #25

Merged
merged 2 commits into from Jan 21, 2013

Projects

None yet

3 participants

@dmpayton
Contributor

All JSONResponseMixin views in my Django 1.5 app throw an exception when using the (now deprecated) django.utils.simplejson library:

Internal Server Error: /account/billing/update-card/
Traceback (most recent call last):
  File "/home/derek/dev/django-montage/django/core/handlers/base.py", line 116, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/home/derek/dev/django-montage/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/derek/.virtualenvs/montage/local/lib/python2.7/site-packages/braces/views.py", line 381, in dispatch
    return handler(request, *args, **kwargs)
  File "/home/derek/dev/django-montage/montage/apps/bank/views/billing.py", line 27, in post_ajax
    return self.render_json_response({})
  File "/home/derek/.virtualenvs/montage/local/lib/python2.7/site-packages/braces/views.py", line 354, in render_json_response
    json_context = json.dumps(context_dict, cls=DjangoJSONEncoder, ensure_ascii=False)
  File "/home/derek/.virtualenvs/montage/local/lib/python2.7/site-packages/simplejson/__init__.py", line 334, in dumps
    **kw).encode(obj)
TypeError: __init__() got an unexpected keyword argument 'namedtuple_as_object'

This patch updates braces.views to check for the built-in json library first, with a fallback for older Python/Django versions.

@dmpayton
Contributor

Derp, I should learn2traceback.

This isn't strictly issue with Django 1.5, but seems like simplejson/simplejson#37. However, since django.utils.simplejson is deprecated anyways, and defaulting to json did fix my issue, I still stand by the pull request.

@chrisjones-brack3t chrisjones-brack3t commented on the diff Jan 11, 2013
braces/views.py
from django.http import HttpResponse
from django.utils.decorators import method_decorator
from django.utils.http import urlquote
from django.views.generic import CreateView
+## Django 1.5+ compat
+try:
+ import json
+except ImportError:
+ from django.utils import simplejson as json
+
@chrisjones-brack3t
chrisjones-brack3t Jan 11, 2013 Member

Two spaces after the import since it's a class definition.

@kennethlove
kennethlove Jan 21, 2013 Member

It'd be two spaces if it was a function definition, too :)

@chrisjones-brack3t
Member

I made one comment on the pull request. Small spacing issue. Fix that and I'll get it merged in and I should be able to roll out a new version today as well.

@dmpayton
Contributor

@chrisjones-brack3t Just wanted to ping you on this; I'm not sure if Github sends out notifications when additional commits are added to an existing PR. :)

@chrisjones-brack3t
Member

It didn't send me a notification until you pinged me. I'll get this pushed out today.

@chrisjones-brack3t chrisjones-brack3t merged commit bcf69cd into brack3t:master Jan 21, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment