I'm not sure if returning a response is any different in middleware than it
would be in a normal view. In that case:

return HttpResponse(json_data, mimetype="application/json")


On Thu, Jun 21, 2012 at 6:54 AM, Taras_96 <taras...@gmail.com> wrote:

> Does anyone have opinions on the best way of having middleware catch
> exceptions, and instead of rendering the error into a HTML template, to
> return a JSON object? Currently I have the middleware below that catches
> exceptions, and if it can find an extra user error message, puts that onto
> the request (that the template then picks up).
>
> class ExceptionUserErrorMessageMiddleware(object):
>     def process_exception(self, request, exception):
>         """ if the exception has information relevant to the user, then
>         tack that onto the request object"""
>
>         theFormat = djrequest.get_getvar(request, settings.FORMAT_PARAM, "")
>         msg = getMessage(exception)
>         if msg:
>             setattr(request, USER_ERROR_MESSAGE_ATTR, msg)
>
>         if theFormat == "json":
>             print "do something"
>
> What's the best way of returning a json object here? Should I set any
> additional headers?
>
> Is there a way of doing the same for exceptional circumstances that don't
> pass through middleware (I'm pretty sure 404 doesn't, are there any others)?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/EtPHiu7SIB8J.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to