Python 无法导入名称视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25960754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:52:36  来源:igfitidea点击:

Cannot import name views

pythondjangopython-2.7django-viewsdjango-1.7

提问by Macuser

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

Here is urls.py located in qblog/qblog/:

这是位于 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')),
)

Also, if I add "library" to the first import statement (which I don't need) it will give me the same error but with library, "Cannot import name library".

此外,如果我将“库”添加到第一个导入语句(我不需要)中,它会给我同样的错误,但是对于库,“无法导入名称库”。

Here is urls.py located in qblog/blog/:

这是位于 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"),
)

Going to the url http://127.0.0.1:8000/indexprovides the same error.

转到 urlhttp://127.0.0.1:8000/index提供了相同的错误。

I do not get any errors in the terminal when running ./manage.py runserver

运行时我在终端中没有收到任何错误 ./manage.py runserver

Project structure:

项目结构:

.
├── 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

采纳答案by Daniel Roseman

There is no need to import the views in your project-level file. You are not using them there, so no reason to import them.

无需在项目级文件中导入视图。你没有在那里使用它们,所以没有理由导入它们。

If you didneed to, you would just to from blog import views, because the views are in the blog directory and manage.py puts the top-level directory into the Python path.

如果确实需要,您只需from blog import views,因为视图位于 blog 目录中,并且 manage.py 将顶级目录放入 Python 路径中。

回答by Meshack Mbuvi

You can just use import views.This works for me

你可以使用import views。这对我有用