[Solved]-Django – import error: no module named views

14👍

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

8👍

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:

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. 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'),
]
👤Dave

1👍

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

0👍

Just Change import statement to

import appname.views

This works fine for my code.

0👍

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!

Leave a comment