3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Pyramid でカスタムの View Decorator を実装する

Posted at
# -*- coding: utf-8 -*-
from pyramid.view import view_config


def json_api(**params):
    u""" JSON API 用 view decorator """
    settings = dict(renderer='json', xhr=True, _depth=1)
    settings.update(params)

    def wrapped(func):
        return view_config(**settings)(func)

    return wrapped

こんな感じで使えます。

@json_api(route_name="api_user_detail")
def user_detail(request):
    return {"id": 1, "name": "junya"}

decorator のネストの数だけ _depth パラメータをインクリメントする必要があるので、そこだけ注意。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?