# -*- coding: utf-8 -*-"""Contains view's decorators which automates common tasks."""import tracebackfrom django.shortcuts import render_to_responsefrom django.template import RequestContextfrom common.http import HttpResponseJsondef render_to(template):"""Render view's output with ``template`` using ``RequestContext``.If decorated view returns dict object then wrap it in RequestContext andrender the template.If decorated view returns non dict object then just return this object.Args::template: path to templateExample::@render_to('blog/index.html')def post_list(request):posts = Post.objects.all()return {'posts': posts,}"""def decorator(func):def wrapper(request, *args, **kwargs):output = func(request, *args, **kwargs)if not isinstance(output, dict):return outputelse:ctx = RequestContext(request)return render_to_response(template, output, context_instance=ctx)return wrapperreturn decoratordef ajax(func):"""Convert views's output into JSON.Decorated view should return dict object.If ``request.method`` is not ``POST`` then deny the request.If view raises Exception then return JSON message with error description."""def wrapper(request, *args, **kwargs):if request.method == 'POST':try:response = func(request, *args, **kwargs)except Exception, ex:response = {'error': traceback.format_exc()}else:response = {'error': {'type': 403, 'message': 'Accepts only POST request'}}if isinstance(response, dict):return HttpResponseJson(response)else:return responsereturn wrapperdef ajax_get(func):"""Convert views's output into JSON.Decorated view should return dict object.If view raises Exception then return JSON message with error description."""def wrapper(request, *args, **kwargs):try:response = func(request, *args, **kwargs)except Exception, ex:response = {'error': traceback.format_exc()}if isinstance(response, dict):return HttpResponseJson(response)else:return responsereturn wrapperdef disable_cache(func):def decorated(*args, **kwargs):resp = func(*args, **kwargs)resp['Pragma'] = 'no-cache'resp['Expires'] = '0'resp['Cache-Control'] = 'no-cache, no-store, must-revalidate'return respreturn decorated
Error is
line 61
except Exception, ex:
^
SyntaxError: invalid syntax
-- You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/7a6f6c81-4cc5-4528-9748-b3fe7437f29c%40googlegroups.com.
No comments:
Post a Comment