django - import error: no module named views - python

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

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

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).
Hope this helps!

Related

How can I fix errors on this page?

These errors occur when you enter the admin page. I want to fix this. Help me.
problementer image description here
Source code
urls.py-fistsite
from django.contrib import admin
from django.urls import path, include
from polls import views
urlpatterns = [
path('', views.index, name='index'),
path('/polls', include('polls.urls')),
path('/admin', include('polls.urls'))
]
urls.py-polls
from django.urls import path
from . import views
app_name = 'polls'
urlpatterns = [
path('', views.index, name='index'),
path('<int:question_id>/', views.detail, name='detail'),
path('<int:question_id>/result/', views.results, name='results'),
path('<int:question_id>/vote/', views.vote, name='vote')
]
Change your urls.py to this and it should work.
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls), # add this
path('', views.index, name='index'),
]
For some reason you changed the site admin's url to include('polls.urls') which is incorrect. Change it back to path('admin/', admin.site.urls) and django will pick it up. And on the side note, You don't have to add / django has a middleware that does it automatically.

Page not found with Django (404)

In my django project, when I access localhost:8000 it says:
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/
The urls.py is:
from django.conf.urls import include, url
from django.contrib import admin
from polls import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^polls/', include('polls.urls', namespace="polls")),
]
The polls urls.py is:
from django.conf.urls import url, include
from django.contrib import admin
from polls import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^(?P<question_id>\d+)/$', views.detail, name='detail'),
url(r'^(?P<question_id>\d+)/vote/$', views.vote, name='vote'),
url(r'^(?P<question_id>\d+)/results/$', views.results, name='results'),
url(r'^admin/', admin.site.urls),
]
The Django version is 1.10. Can anyone help me identify the problem? Thanks in advance.
You do not have a route for / it seems, so http://localhost:8000/ does not get you anywhere.
You wrote url(r'^polls/', include('polls.urls', namespace="polls")), in urls.py so all the routes defined in polls.urls are to be prefixed with polls/.
You may actually want to go to http://localhost:8000/polls/ (notice the polls/, because the route you defined as index is listed in the polls app.
Had you wanted to route your polls index to your url root, you should change urls.py to something like
urlpatterns = [
url(r'^', include('polls.urls', namespace="polls")),
url(r'^admin/', admin.site.urls),
]
and the forget about the polls/ part in the urls.
In main urls.py change
from
url(r'^polls/', include('polls.urls', namespace="polls")),
to
url(r'^$', include('poll.urls')),

Avoid repetition on Django URLs

I'm making the Django tutorial at the official website and I'm currently setting up URLs for the sample polling application we're creating.
As of now, my polls/urls.py looks like this:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^(?P<question_id>[0-9]+)/$', views.detail, name='detail'),
url(r'^(?P<question_id>[0-9]+)/results/$', views.results, name='results'),
url(r'^(?P<question_id>[0-9]+)/vote/$', views.votes, name='vote')
]
And I can't help but notice the repetition of (?P<question_id>[0-9]+) so I wonder if there's a simpler way to avoid this besides extracting it to a constant like QUESTION_PATTERN = (?P<question_id>[0-9]+)
You can include a list of subpatterns, like this:
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^(?P<question_id>[0-9]+)/', include([
url(r'^$', views.detail, name='detail'),
url(r'^results/$', views.results, name='results'),
url(r'^vote/$', views.votes, name='vote'),
]),
]

Django URL not working

Going to localhost/app works, but localhost/app/upload does not. Am I doing something really obviously wrong? I've tried a few different methods following the documentation without luck. I get a 404 saying the URL pattern does not match.
/project/app/urls.py
from app import views
urlpatterns = patterns('',
url(r'^$', views.view, name='result'),
url(r'^upload/$', views.upload, name='upload'),
)
/project/urls.py
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^app/$', include('app.urls')),
)
Don't terminate the inclusion URL.
url(r'^app/', include('app.urls')),

Django urls.py bug - some patterns not working

I've encountered a one nasty bug in my code while developing a personal blog in Django. Basically, I've changed my urls.py file by adding a couple of rules to make certain views accessible.
urls.py
from django.conf.urls import include, url
from django.contrib import admin
from blog import views
urlpatterns = [
# Examples:
# url(r'^$', 'blogas.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^$', views.index, name='index'),
url(r'^(?P<slug>\w+)', views.view_post, name='view_blog_post'),
url(r'^about/$', views.about, name='about'),
url(r'^posts/$', views.posts, name='posts'),
]
Everything seems to be working except when I try access http://127.0.0.1:8000/about or /posts, Django throws out a 404 error. What is the reason of this? I've defined both rules but the system seems not to recognize the pattern - maybe I've mispelled something? Maybe I know nothing about url formatting (could be, it's my first time doing this stuff)?
A big thanks from a newbie programmer to everyone who finds the bug :)
The url-patterns are processed from top to bottom. Your third pattern ^(?P<slug>\w+) consumes everything, so about and posts is never reached.
An example: Django wants to find the view for the url about/. The patterns ^admin/ and ^$ do not match. But ^(?P<slug>\w+) does, because about starts with letters or numbers (the character sets contained in \w)
>>> import re
>>> re.search('^(?P<slug>\w+)', 'about/')
<_sre.SRE_Match object at 0x10b5b7be8>
So Django found a match, callBs views.view_post and finishes the request. That means, the more specific rule must come first. Better: avoid ambiguity.
You have to change position of url(r'^(?P<slug>\w+)', views.view_post, name='view_blog_post'),.
urlpatterns = [
# Examples:
# url(r'^$', 'blogas.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^$', views.index, name='index'),
url(r'^about/$', views.about, name='about'),
url(r'^posts/$', views.posts, name='posts'),
url(r'^(?P<slug>\w+)', views.view_post, name='view_blog_post'),
]
urlpatterns is list and order of urls is important.

Categories