Python django - 导入错误:没有名为视图的模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21485148/
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 22:57:23  来源:igfitidea点击:

django - import error: no module named views

pythondjangoimportdjango-views

提问by David J.

I've been racking my brains and can't figure out why there should be an import error when 'views' is imported. I get the following message when I visit my index page:

我一直在绞尽脑汁,无法弄清楚为什么在导入“视图”时会出现导入错误。当我访问我的索引页面时,我收到以下消息:

"
Request Method: GET
Request URL:    http://127.0.0.1:8000/moments/
Django Version: 1.6.1
Exception Type: ImportError
Exception Value:    
No module named views
Exception Location: C:\Python27\lib\site-packages\django\utils\importlib.py in import_module, line 40
"

Here is my urls.py

这是我的 urls.py

from django.conf.urls import patterns, url

from moments_app import views

urlpatterns = patterns('',
    url(r'^$', "views.index", name='index'),
    url(r'^$', "views.choose_dataset", name='choose'),
    url(r'^get_moments/', "views.get_moments", name='get_moments'),
    url(r'^learn/$', "views.learn", name='learn'),
    url(r'^(?P<moment_id>\d+)/$', "views.detail", name='detail'),

)

I clearly have a module named views in my moments_app folder. Also, moments_app is in my path. Does anyone have any ideas as to what might be causing this?

我的 moment_app 文件夹中显然有一个名为 views 的模块。此外, moment_app 在我的道路上。有没有人对可能导致这种情况的原因有任何想法?

采纳答案by Martijn Pieters

You prefixed your route names with a relativemodule name. Use an absolute name:

您使用相对模块名称作为路由名称的前缀。使用绝对名称:

urlpatterns = patterns('',
    url(r'^$', "moments_app.views.index", name='index'),
    url(r'^$', "moments_app.views.choose_dataset", name='choose'),
    url(r'^get_moments/', "moments_app.views.get_moments", name='get_moments'),
    url(r'^learn/$', "moments_app.views.learn", name='learn'),
    url(r'^(?P<moment_id>\d+)/$', "moments_app.views.detail", name='detail'),
)

or better still, use the first argument to specify the full module path:

或者更好的是,使用第一个参数来指定完整的模块路径:

urlpatterns = patterns('moments_app.views',
    url(r'^$', "index", name='index'),
    url(r'^$', "choose_dataset", name='choose'),
    url(r'^get_moments/', "get_moments", name='get_moments'),
    url(r'^learn/$', "views.learn", name='learn'),
    url(r'^(?P<moment_id>\d+)/$', "detail", name='detail'),
)

although a combination of the two is also allowed:

虽然也允许两者结合:

urlpatterns = patterns('moments_app',
    url(r'^$', "views.index", name='index'),
    url(r'^$', "views.choose_dataset", name='choose'),
    url(r'^get_moments/', "views.get_moments", name='get_moments'),
    url(r'^learn/$', "views.learn", name='learn'),
    url(r'^(?P<moment_id>\d+)/$', "views.detail", name='detail'),
)

回答by Aravind Aran

You have imported your view as

您已将视图导入为

from moments_app import views

Some times it won't work.

有时它不起作用。

Use this

用这个

from moments_app.views import *


urlpatterns = patterns('',

    url(r'^$', index, name='index'),
    url(r'^$', choose_dataset, name='choose'),
    url(r'^get_moments/', get_moments, name='get_moments'),
    url(r'^learn/$', learn, name='learn'),
    url(r'^(?P<moment_id>\d+)/$', detail, name='detail'),

)

It will work..

它会工作..

回答by Dave

Two year update:

两年更新:

In Django 1.8 and later both string views and the patternsfunction are deprecated, resulting in a simpler and more reliable syntax:

在 Django 1.8 及更高版本patterns中,不推荐使用字符串视图和函数,从而产生更简单、更可靠的语法:

from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^$', views.choose_dataset, name='choose'),
    url(r'^get_moments/', views.get_moments, name='get_moments'),
    url(r'^learn/$', views.learn, name='learn'),
    url(r'^(?P<moment_id>\d+)/$', views.detail, name='detail'),
]

Note that there are no "relative" or "absolute" view names with the callable syntax -- if you import the viewsmodule you get its definitions. I'd avoid importing the individual view functions since there's a tiny chance that another import could define a colliding name. If you're not worried about collisions and don't mind putting your app name in the file, the urls can be slightly shortened:

请注意,可调用语法没有“相对”或“绝对”视图名称——如果您导入views模块,您将获得其定义。我会避免导入单个视图函数,因为另一个导入定义冲突名称的可能性很小。如果您不担心冲突并且不介意将您的应用程序名称放在文件中,则可以稍微缩短网址:

from moments_app.views import index, choose_dataset, get_moments, learn, detail

urlpatterns = [
    url(r'^$', index, name='index'),
    url(r'^$', choose_dataset, name='choose'),
    url(r'^get_moments/', get_moments, name='get_moments'),
    url(r'^learn/$', learn, name='learn'),
    url(r'^(?P<moment_id>\d+)/$', detail, name='detail'),
]

回答by ChandyShot

Just Change import statement to

只需将导入语句更改为

import appname.views

This works fine for my code.

这适用于我的代码。

回答by Sheheryar Khalid

I faced this problem and tried above answers but the issue was that i was missing a string quote in my "render" function in views.py which i missed and was facing issue in urls.py which states that no module "views" is present in "portfolio"(app name). Hope this helps!

我遇到了这个问题并尝试了上面的答案,但问题是我在views.py中的“render”函数中缺少一个字符串引用,我错过了它并且在urls.py中遇到了问题,它指出没有模块“views”存在在“投资组合”(应用程序名称)中。希望这可以帮助!