[英]django - import error: no module named views
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.我的 moment_app 文件夹中显然有一个名为 views 的模块。 Also, moments_app is in my path.
此外, moment_app 在我的道路上。 Does anyone have any ideas as to what might be causing this?
有没有人对可能导致这种情况的原因有任何想法?
You prefixed your route names with a relative module 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'),
)
Two year update:两年更新:
In Django 1.8 and later both string views and the patterns
function 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 views
module you get its definitions.请注意,可调用语法没有“相对”或“绝对”视图名称——如果您导入
views
模块,您将获得其定义。 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:
如果您不担心冲突并且不介意将您的应用程序名称放在文件中,则可以稍微缩短网址:
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'),
]
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..它会工作..
Just Change import statement to只需将导入语句更改为
import appname.views
This works fine for my code.这适用于我的代码。
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).我遇到了这个问题并尝试了上面的答案,但问题是我在views.py中的“render”函数中缺少一个字符串引用,我错过了它并且在urls.py中遇到了问题,它指出没有模块“views”存在在“投资组合”(应用程序名称)中。 Hope this helps!
希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.