解決Django模板無法使用perms變量問題的方法
前言
本文主要給大家介紹了關(guān)于Django模板無法使用perms變量的解決方法,分享出來供大家參考學(xué)習(xí),下面話不多說了,來一起看看詳細(xì)的介紹吧。
解決方法:
首先,在使用Django內(nèi)置權(quán)限管理系統(tǒng)時(shí),settings.py文件要添加
INSTALLED_APPS添加: 'django.contrib.auth', MIDDLEWARE添加: 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.context_processors.auth', TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, 'templates')], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.i18n', 'django.template.context_processors.media', 'django.template.context_processors.static', 'django.template.context_processors.tz', 'django.contrib.messages.context_processors.messages', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', ], }, }, ]
如何在模板進(jìn)行權(quán)限檢查呢?
根據(jù)官網(wǎng)說明 https://docs.djangoproject.com/en/1.11/topics/auth/default/#permissions ,已登錄用戶權(quán)限保存在模板{{ perms }}
變量中,是權(quán)限模板代理django.contrib.auth.context_processors.PermWrapper
的一個(gè)實(shí)例,具體可以查看django/contrib/auth/context_processors.py源碼
測試用例:
測試過程中,發(fā)現(xiàn){{ perms }}
變量壓根不存在,沒有任何輸出;好吧,只能取Debug Django的源碼了
def auth(request): """ Returns context variables required by apps that use Django's authentication system. If there is no 'user' attribute in the request, uses AnonymousUser (from django.contrib.auth). """ if hasattr(request, 'user'): user = request.user else: from django.contrib.auth.models import AnonymousUser user = AnonymousUser() print(user, PermWrapper(user), '-----------------------') return { 'user': user, 'perms': PermWrapper(user), }
測試訪問接口,發(fā)現(xiàn)有的接口有打印權(quán)限信息,有的沒有,似乎恍然醒悟
可以打印權(quán)限信息的接口返回:
return render(request, 'fms/fms_add.html', {'request': request, 'form': form, 'error': error})
不能打印權(quán)限新的接口返回:
return render_to_response( 'fms/fms.html', data)
render和render_to_response區(qū)別
render是比render_to_reponse更便捷渲染模板的方法,會(huì)自動(dòng)使用RequestContext,而后者需要手動(dòng)添加:
return render_to_response(request, 'fms/fms_add.html', {'request': request, 'form': form, 'error': error},context_instance=RequestContext(request))
其中RequestContext是django.template.Context
的子類.接受request
和context_processors
,從而將上下文填充渲染到模板問題已經(jīng)很明確,由于使用了render_to_response
方法,沒有手動(dòng)添加context_instance=RequestContext(request)
導(dǎo)致模板不能使用{{ perms }}
變量
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
Tensorflow全局設(shè)置可見GPU編號(hào)操作
這篇文章主要介紹了Tensorflow全局設(shè)置可見GPU編號(hào)操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06用map函數(shù)來完成Python并行任務(wù)的簡單示例
這篇文章主要介紹了用map函數(shù)來完成Python并行任務(wù)的簡單示例,多線程和多進(jìn)程編程的問題一直都是Python中的熱點(diǎn)和難點(diǎn),需要的朋友可以參考下2015-04-04淺談keras 的抽象后端(from keras import backend as K)
這篇文章主要介紹了淺談keras 的抽象后端(from keras import backend as K),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06怎么處理Python分割字符串時(shí)有多個(gè)分隔符
在使用Python處理字符串的時(shí)候,有時(shí)候會(huì)需要分割字符。本文就介紹了Python分割字符串時(shí)有多個(gè)分隔符,感興趣的可以了解一下2021-07-07聊聊Pytorch torch.cat與torch.stack的區(qū)別
這篇文章主要介紹了Pytorch torch.cat與torch.stack的區(qū)別說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05Python處理文件的方法(mimetypes和chardet)
這篇文章主要介紹了Python處理文件的方法(mimetypes和chardet),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09