我一直绞尽脑汁,无法弄清楚为什么导入'views'时会出现导入错误。当我访问索引页面时收到以下消息:
"
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
"
这是我的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'),
)
我在moments_app文件夹中显然有一个名为views的模块。此外,moments_app在我的道路上。有没有人对可能导致这种情况的原因有任何想法?
答案 0 :(得分:12)
您使用 relative 模块名称为路由名称添加前缀。使用绝对名称:
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'),
)
或者更好的是,使用第一个参数指定完整的模块路径:
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'),
)
虽然也允许两者的组合:
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'),
)
答案 1 :(得分:7)
两年更新:
在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'),
]
请注意,没有带有可调用语法的“相对”或“绝对”视图名称 - 如果导入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'),
]
答案 2 :(得分:0)
您已将视图导入为
from moments_app import views
有时候它不会工作。
使用此
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'),
)
它会起作用..
答案 3 :(得分:0)
只需将导入语句更改为
import appname.views
这对我的代码来说很好用。
答案 4 :(得分:0)
我遇到此问题并尝试了以上答案,但问题是我错过了views.py的“ render”函数中的字符串引号,而我错过了,并且遇到了urls.py的问题,该问题指出没有模块“ views” “”组合(应用程序名称)中包含“”。 希望这会有所帮助!