【问题标题】:global name 'get_user_model' is not defined未定义全局名称“get_user_model”
【发布时间】:2016-05-26 22:00:29
【问题描述】:
/project/reset_password_confirm/MTQ-4c8-65d880f1c28996091226/上的

NameError

global name 'get_user_model' is not defined
Request Method: POST
Django Version: 1.9.1
Exception Type: NameError
Exception Value:    
global name 'get_user_model' is not defined
Exception Location: /root/django/studie/project/views.py in post, line 770
Python Executable:  /usr/bin/python
Python Version: 2.7.9

views.py

from django.contrib.auth.models import User

第 770 行:

    def post(self, request, uidb64=None, token=None, *arg, **kwargs):
    UserModel =  get_user_model()
    ....

为什么 get_user_model() 不起作用?有什么建议吗?

【问题讨论】:

    标签: python django django-models django-views nameerror


    【解决方案1】:

    您需要将导入添加到您的views.py

    from django.contrib.auth import get_user_model
    

    【讨论】:

    • 我已经试过了。它说: ImportError: cannot import name get_user_model
    • 编辑:我的错! from django.contrib.auth.models import get_user_model 显然不起作用。 from django.contrib.auth import get_user_model 确实有效!谢谢:)
    猜你喜欢
    • 2018-05-04
    • 2015-12-22
    • 2014-04-18
    • 2015-08-08
    • 2011-04-27
    • 2013-09-04
    • 2015-02-26
    • 2016-09-11
    • 1970-01-01
    相关资源
    最近更新 更多