Django - import error: no module named views

I rack my brains and cannot understand why an import error should occur when "views" is imported. When visiting my index page, the following message appears:

" 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

 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 explicitly have a module named views in my events_app folder. In addition, moment_app is on my way. Does anyone have any ideas as to what might be causing this?

+8
python import django django-views
source share
3 answers

You have specified 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 yet, use the first argument to indicate the full path to the module:

 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'), ) 
+11
source share

Two year update:

In Django 1.8 and later, both string representations and the patterns function are deprecated, resulting in a simpler and more reliable syntax:

 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" names with the syntax being called - if you import the views module, you will get its definitions. I would not import individual view functions, since there is a small chance that another import can determine the counter name. If you're not worried about collisions and don't mind putting your application name in a file, the URLs can be shortened a bit:

 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'), ] 
+5
source share

You have imported your view as

 from moments_app import views 

This will not work several times.

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'), ) 

That will work.

-one
source share

All Articles