Skip to content

Instantly share code, notes, and snippets.

@tag1216
Created March 31, 2016 04:55
Show Gist options
  • Save tag1216/857d23dbf62ff0655117a18e88e90a50 to your computer and use it in GitHub Desktop.
Save tag1216/857d23dbf62ff0655117a18e88e90a50 to your computer and use it in GitHub Desktop.
djangoでurls.pyにデコレータを書く
def decorate(view_func, decorators):
"""
Viewをデコレータでラップする。
@param view_func:
@param decorators:
@return:
"""
if decorators:
for decorator in reversed(decorators):
view_func = decorator(view_func)
return view_func
# 未ログインでもアクセス可能
PUBLIC = []
# ログインユーザーのみアクセス可能
AUTHENTICATED = [
login_required,
]
urlpatterns = [
url(r'^$', decorete(views.IndexView.as_view(), PUBLIC), name='index''),
url(r'home/^$', decorete(views.HomeView.as_view(), AUTHENTICATED), name='home'')
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment