无法导入名称视图
Posted
技术标签:
【中文标题】无法导入名称视图【英文标题】:Cannot import name views 【发布时间】:2014-11-15 15:10:53 【问题描述】:ImportError at /
cannot import name views
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 1.7
Exception Type: ImportError
Exception Value:
cannot import name views
Exception Location: /Users/adam/Desktop/qblog/qblog/urls.py in <module>, line 1
Python Executable: /Users/adam/Desktop/venv/bin/python
Python Version: 2.7.8
Python Path:
['/Users/adam/Desktop/qblog',
'/Users/adam/Desktop/venv/lib/python27.zip',
'/Users/adam/Desktop/venv/lib/python2.7',
'/Users/adam/Desktop/venv/lib/python2.7/plat-darwin',
'/Users/adam/Desktop/venv/lib/python2.7/plat-mac',
'/Users/adam/Desktop/venv/lib/python2.7/plat-mac/lib-scriptpackages',
'/Users/adam/Desktop/venv/lib/python2.7/lib-tk',
'/Users/adam/Desktop/venv/lib/python2.7/lib-old',
'/Users/adam/Desktop/venv/lib/python2.7/lib-dynload',
'/usr/local/Cellar/python/2.7.8/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
'/usr/local/Cellar/python/2.7.8/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
'/usr/local/Cellar/python/2.7.8/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
'/usr/local/Cellar/python/2.7.8/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
'/usr/local/Cellar/python/2.7.8/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
'/Users/adam/Desktop/venv/lib/python2.7/site-packages']
Server time: Sun, 21 Sep 2014 15:12:22 +0000
这是位于 qblog/qblog/ 中的 urls.py:
from django.conf.urls import patterns, url
from . import views
urlpatterns = patterns(
'',
url(r'^admin/', include(admin.site.urls)),
url(r'^markdown/', include('django_markdown.urls')),
url(r'^', include('blog.urls')),
)
另外,如果我将“库”添加到第一个导入语句(我不需要),它会给我同样的错误,但库,“无法导入名称库”。
这是位于 qblog/blog/ 中的 urls.py:
from django.conf.urls import patterns, include, url
from . import views
urlpatterns = patterns(
'',
url(r'^$', views.BlogIndex.as_view(), name="index"),
)
转到 url http://127.0.0.1:8000/index
会出现同样的错误。
我在终端运行./manage.py runserver
时没有收到任何错误
项目结构:
.
├── blog
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── admin.py
│ ├── admin.pyc
│ ├── migrations
│ │ ├── 0001_initial.py
│ │ ├── 0001_initial.pyc
│ │ ├── 0002_auto_20140921_1414.py
│ │ ├── 0002_auto_20140921_1414.pyc
│ │ ├── 0003_auto_20140921_1501.py
│ │ ├── 0003_auto_20140921_1501.pyc
│ │ ├── __init__.py
│ │ └── __init__.pyc
│ ├── models.py
│ ├── models.pyc
│ ├── tests.py
│ ├── urls.py
│ ├── urls.pyc
│ ├── views.py
│ └── views.pyc
├── db.sqlite3
├── manage.py
├── qblog
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── settings.py
│ ├── settings.pyc
│ ├── urls.py
│ ├── urls.pyc
│ ├── wsgi.py
│ └── wsgi.pyc
├── static
│ ├── css
│ │ ├── blog.css
│ │ └── bootstrap.min.css
│ ├── icons
│ │ └── favicon.ico
│ └── js
│ ├── bootstrap.min.js
│ └── docs.min.js
└── templates
├── base.html
├── home.html
└── post.html
【问题讨论】:
【参考方案1】:你可以使用import views
。这对我有用
【讨论】:
当我删除from .
有什么不同。以后开发我的项目会遇到什么问题吗?【参考方案2】:
无需在项目级文件中导入视图。你没有在那里使用它们,所以没有理由导入它们。
如果您确实需要,您只需发送至from blog import views
,因为视图位于博客目录中,并且 manage.py 将***目录放入 Python 路径中。
【讨论】:
是的,我就是这么想的。但是,当视图导入语句被删除时,我得到“名称'admin'未定义”。 那是因为你没有应该拥有的from django.contrib import admin
。以上是关于无法导入名称视图的主要内容,如果未能解决你的问题,请参考以下文章
Django - URL 路由问题(无法导入名称'urls')