Find the required permissions of Django URLs without calling them?

前端 未结 3 1218
忘了有多久
忘了有多久 2021-02-09 01:17

My Django app currently has URLs which are protected by \'permission_required()\' functions.

This function is called in three different ways.

  1. As a decorato
3条回答
  •  旧时难觅i
    2021-02-09 02:02

    I had a similar issue, but it went a little deeper. Instead of just permissions, I also wanted other tests based on the lidded in user (ie, is_staff, or user.units.count() > 1). Duplicating these in the view and the template seems prone to errors.

    You can introspect a view object, and see all of the decorators wrapping it, and work out if they are checks (in my case: the first argument I'd u or user). If they all pass, then allow rendering the link.

    Get all decorators wrapping a function describes the technique in a little more detail. You can find the app that wraps this up into a handy replacement for {% url %} at Django-menus.

提交回复
热议问题