Django : How to exclude form field if the user is staff?

前端 未结 2 576

How to exclude form fields if the user is not staff ? I tried this but didn\'t work , giving an error :

global name \'user\' is not defined



        
2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-20 05:48

    This can be achieved in the template when rendering the form. It will need to allow null values or have a default value in the model definition or alternatively have its validation overridden:

    {% csrf_token %} {% if request.user.is_staff %}

    {{ form.published }}

    {% endif %}

    {{ form.author }}

    Similarly you can check for is_superuser or check permissions, see the docs: https://docs.djangoproject.com/en/dev/topics/auth/default/#permissions

提交回复
热议问题