Code

Opened 8 months ago

Closed 8 months ago

#21522 closed New feature (wontfix)

Add rendering decorators

Reported by: anonymous Owned by: nobody
Component: Core (Other) Version: 1.6
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

A lot of frameworks now allow you to do :

@something
def hello(request):
    return {}

We could apply that to django :

@render()
def hello(request):
    return 'Hello'

Returns a HttpRequest('Hello')

@render('template.html')
def hello(request):
    return {}

Is an alternative syntax for return render('template.html', {}). It's more declarative, and removes the boiler plate code from the function body where only the view logic now appears.

Possiblities :

@render('template.xml', mimetype='application/xml')
def hello(request):
    return {}

@render('template.html', code=404)
def hello(request):
    return {}

# this will return HttpRequest(json.loads({}), mimetype='application/json'))
@render(json=True)
def hello(request):
    return {}

Attachments (0)

Change History (1)

comment:1 Changed 8 months ago by russellm

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to wontfix
  • Status changed from new to closed

Nope.

The Golden rule of decorators -- they shouldn't modify the prototype of the function that they're decorating. If an undecorated function returns a HttpResponse, then so should the decorated function.

Completely aside from this philosophical argument -- I simply don't see any advantage in readability or reusability in:

@render('template.html', code=404)
def hello(request):
    return {}

compared to:

def hello(request):
    return render('template.html', {}, code=404)

On a historical note - this has been proposed many times in the past, and been rejected for the same reasons.

So - marking wontfix.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.