'module'对象没有属性'views' django错误

时间:2022-01-07 07:56:36

If I import django's built in login view as in following code

如果我导入django的内置登录视图,就像下面的代码一样。

from django.conf.urls import patterns, include, url
from django.contrib.auth.views import login

urlpatterns = patterns('',    
    url(r'login/$', login, name='login'),
)

every thing works fine, but if I'll include it in following way

每件事都很好,但是如果我用下面的方法把它包括进来的话

from django.conf.urls import patterns, include, url
from django.contrib import auth

urlpatterns = patterns('',    
    url(r'login/$', auth.views.login, name='login'),
)

I get the following error

我得到以下错误

Exception Value: 'module' object has no attribute 'views'

what is really bothering me is in another project I am importing it the second way and it is working fine. Does anyone know what's going on over here?

真正困扰我的是,在另一个项目中,我用第二种方式导入它,它运行得很好。有人知道这是怎么回事吗?

2 个解决方案

#1


10  

In the second project you've probably already imported the auth.views module before calling auth.views.login. Python stitches your imported modules when it can.

在第二个项目中,您可能已经导入了auth。在调用auth.views.login之前查看模块。Python在可能的情况下会缝合导入的模块。

For example, this will work

例如,这是可行的

>>> from django.contrib.auth.views import login #or from django.contrib.auth import views
>>> from django.contrib import auth

>>> auth.views.login
<function login at 0x02C37C30>

The first import doesn't even have to mention the login view. This will also work.

第一个导入甚至不需要提到登录视图。这也将工作。

>>> from django.contrib.auth.views import logout
...
#then import auth.views.login

The following won't because python does not know of the views module since it isn't registered in auth.__init__.py

下面的代码不会,因为python不知道视图模块,因为它没有在auth.__init__.py中注册

>>> from django.contrib import auth

>>> auth.views.login
...
AttributeError: 'module' object has no attribute 'views'

#2


1  

In the first import (from django.contrib.auth.views import login), the dot syntax is traversing the module hierarchy. In the urlpattern access (auth.views.login), the dot-syntax is doing property (ie. class) lookup. From my shell_plus, you can see that "auth" doesn't have a views property.

在第一句中(来自django.后悔。auth)。视图导入登录),点语法正在遍历模块层次结构。在urlpattern access (auth.view .login)中,点语法是执行属性。类)查找。从我的shell_plus中可以看到,“auth”没有视图属性。

In [1]: from django.contrib import auth

In [2]: auth.<TAB FOR COMPLETION>
auth.BACKEND_SESSION_KEY      auth.load_backend
auth.ImproperlyConfigured     auth.login
auth.PermissionDenied         auth.logout
auth.REDIRECT_FIELD_NAME      auth.models
auth.SESSION_KEY              auth.re
auth.authenticate             auth.rotate_token
auth.forms                    auth.settings
auth.get_backends             auth.signals
auth.get_permission_codename  auth.tokens
auth.get_user                 auth.user_logged_in
auth.get_user_model           auth.user_logged_out
auth.hashers                  auth.user_login_failed
auth.import_by_path           

This is why it's giving you an error. It really shouldn't work if you're trying that in another project/file either -- unless your other project's auth.__init__.py is auto-loading its submodules.

这就是为什么它会给你一个错误。如果你在另一个项目或文件中尝试的话,它真的不应该起作用——除非你的另一个项目的主管。py正在自动加载它的子模块。

#1


10  

In the second project you've probably already imported the auth.views module before calling auth.views.login. Python stitches your imported modules when it can.

在第二个项目中,您可能已经导入了auth。在调用auth.views.login之前查看模块。Python在可能的情况下会缝合导入的模块。

For example, this will work

例如,这是可行的

>>> from django.contrib.auth.views import login #or from django.contrib.auth import views
>>> from django.contrib import auth

>>> auth.views.login
<function login at 0x02C37C30>

The first import doesn't even have to mention the login view. This will also work.

第一个导入甚至不需要提到登录视图。这也将工作。

>>> from django.contrib.auth.views import logout
...
#then import auth.views.login

The following won't because python does not know of the views module since it isn't registered in auth.__init__.py

下面的代码不会,因为python不知道视图模块,因为它没有在auth.__init__.py中注册

>>> from django.contrib import auth

>>> auth.views.login
...
AttributeError: 'module' object has no attribute 'views'

#2


1  

In the first import (from django.contrib.auth.views import login), the dot syntax is traversing the module hierarchy. In the urlpattern access (auth.views.login), the dot-syntax is doing property (ie. class) lookup. From my shell_plus, you can see that "auth" doesn't have a views property.

在第一句中(来自django.后悔。auth)。视图导入登录),点语法正在遍历模块层次结构。在urlpattern access (auth.view .login)中,点语法是执行属性。类)查找。从我的shell_plus中可以看到,“auth”没有视图属性。

In [1]: from django.contrib import auth

In [2]: auth.<TAB FOR COMPLETION>
auth.BACKEND_SESSION_KEY      auth.load_backend
auth.ImproperlyConfigured     auth.login
auth.PermissionDenied         auth.logout
auth.REDIRECT_FIELD_NAME      auth.models
auth.SESSION_KEY              auth.re
auth.authenticate             auth.rotate_token
auth.forms                    auth.settings
auth.get_backends             auth.signals
auth.get_permission_codename  auth.tokens
auth.get_user                 auth.user_logged_in
auth.get_user_model           auth.user_logged_out
auth.hashers                  auth.user_login_failed
auth.import_by_path           

This is why it's giving you an error. It really shouldn't work if you're trying that in another project/file either -- unless your other project's auth.__init__.py is auto-loading its submodules.

这就是为什么它会给你一个错误。如果你在另一个项目或文件中尝试的话,它真的不应该起作用——除非你的另一个项目的主管。py正在自动加载它的子模块。