[Django]-Django error in urlpatterns: No module named views

3πŸ‘

βœ…

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

πŸ‘€ham-sandwich

1πŸ‘

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

from <your app name>.views import *
πŸ‘€Pranjal

1πŸ‘

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),
]
πŸ‘€Pynchia

Leave a comment