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'

更多相关文章

  1. django的视图和URL配置
  2. python,os模块的常用方法
  3. python3中time模块的用法及说明
  4. Python学习笔记(基础篇)_014_GUI模块 easygui的使用
  5. Python Flask WTForms:如何在视图中动态禁用字段?
  6. 如何使用pip安装Python MySQLdb模块?
  7. 在模块和/或包中组织Python类
  8. Python/模块与包之模块
  9. eclipse调用python模块是出错及解决

随机推荐

  1. javascript部分设计模式总结
  2. 这次要讲不清前后端分离,我都怎么地!
  3. 纯CSS3绘制可爱的神奇宝贝
  4. 实不相瞒,在家远程办公15天,我有点自闭
  5. 内置于浏览器中的国际化API[每日前端夜话
  6. 无线标记语言(WML)基础之WMLScript 基础
  7. 使用Spry轻松将XML数据显示到HTML页
  8. 用Asp与XML实现交互的一个实例源码
  9. 不同浏览器对XML的解析是不同的
  10. cdata标签如何使用