Jacobian
Jacobian

Reputation: 10832

Django error in urlpatterns: No module named views

In Django 1.7.x this construct was working:

# urls.py

import views

urlpatterns = ('',
   url(r'^$', views.index)
)

In Django 1.8.X it stopped working. Now I get this error message when I run the default Django server:

No module named 'views'

I also tried this:

from system.views import *

urlpatterns = ('',
   url(r'^$', views.index)
)

This results in:

name 'views' is not defined

And this:

from system import views

urlpatterns = ('',
   url(r'^$', views.index)
)

I also tried many more combinations that I've seen at stackoverflow, but none of them works. Hope someone can share what magic combination should do the trick.

EDIT

\home
  \jacobian
     \apps
        \apps
          __init__.py
          settings.py
          urls.py
          views.py
          ...
       \system
          __init__.py
          urls.py
          views.py
          ...

Upvotes: 3

Views: 3513

Answers (3)

Pynchia
Pynchia

Reputation: 11586

Your statement is a bit of a mix.

Before version 1.8 it was

from myapp import views

urlpatterns = patterns('',
    url('^$', views.myview),
    url('^other/$', views.otherview),
)

Now, from version 1.8, there is no need for the first void argument to patterns when assigning urlpatterns. Actually there is no need to call patterns at all.

Here is one example form my latest project with Django 1.8:

urlpatterns = [
    url(r'^$', HomePage.as_view(), name='home'),
    url(r'^play/', include('play.urls', namespace='play', app_name='play')),
]

And as described in the Django 1.8 release docs:

Thus patterns() serves little purpose and is a burden when teaching new users (answering the newbie’s question “why do I need this empty string as the first argument to patterns()?”). For these reasons, we are deprecating it. Updating your code is as simple as ensuring that urlpatterns is a list of django.conf.urls.url() instances. For example:

from django.conf.urls import url
from myapp import views

urlpatterns = [
    url('^$', views.myview),
    url('^other/$', views.otherview),
]

Upvotes: 1

Pranjal
Pranjal

Reputation: 689

You should include the views.py file from the application that you have created. So try

from <your app name>.views import *

Upvotes: 1

ham-sandwich
ham-sandwich

Reputation: 4050

I just tried to recreate this issue. It seems that you are correct and just import views no longer works. However, the following import statement works fine for me:

from . import views

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^$', views.index)
]

You can see an example here on the django documentation. I also think this related Stack Overflow question can clarify they reason for dot import syntax:

Q: Python "from [dot]package import ..." syntax

Upvotes: 3

Related Questions