Why urls.py generate an error when I runserver

Hello
I am wtahcing a very nice video to start with Django. But I observed a problem of version.
The video was published in 2015 (Aïe)

The video shows how to modify the router.

For exemple, the teacher add the following:

url(r'^$','contacts.views.index.page'),

The problem, in the last django version it starts with path and I did the following, after path(‘admin/’…

path('admin/', admin.site.urls),
path('/', 'contacts.views.index.page'),

It does not work. When I run runserver, I got a such error

res = instance.dict[self.name] = self.func(instance)
File “/home/pierrot/ecosensors/env-console/lib/python3.6/site-packages/django/urls/resolvers.py”, line 595, in urlconf_module
return import_module(self.urlconf_name)
File “/usr/lib/python3.6/importlib/init.py”, line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File “< frozen importlib._bootstrap >”, line 994, in _gcd_import
File “< frozen importlib._bootstrap >”, line 971, in _find_and_load
File “< frozen importlib._bootstrap >”, line 955, in _find_and_load_unlocked
File “< frozen importlib._bootstrap >”, line 665, in _load_unlocked
File “< frozen importlib._bootstrap_external >”, line 678, in exec_module
File “< frozen importlib._bootstrap >”, line 219, in _call_with_frames_removed
File “/home/pierrot/ecosensors/pjtconsole/urls.py”, line 21, in
path(‘/’, ‘appconsole.views.index.page’),
File “/home/pierrot/ecosensors/env-console/lib/python3.6/site-packages/django/urls/conf.py”, line 73, in _path
raise TypeError(‘view must be a callable or a list/tuple in the case of include().’)
TypeError: view must be a callable or a list/tuple in the case of include().

(I am a bit surpise to see something about bootstrap)

I beleive, the video is very nice but not up to date :).

What is now the correct way and path to re-route, the root to a view of my app?

Thanks a lot for your help

from django.urls import path, include
from .views import *

urlpatterns = [
     path('', include('adminsite.urls')),
     path('contacts/', views.view_name.as_view(), name='contact_view'),
     ...
]

Use include() to register all the app urls.

1 Like

Thnaks you, unfortunalety t’s still not working.

I red this https://docs.djangoproject.com/en/4.0/topics/http/urls/

In fact my app folder is appconsole.
in appconsole, created a folder ‘views’ and a file ‘index.py’ and a file init.py
in appconsole/view/, I edited the file index.py and I past the following

from django.shortcuts import render
from django.http import HttpResponse

def page(request):
        return HttpResponse("Bonjour")

then in urls.py I add the following

from django.contrib import admin
from django.urls import path
from . import views

urlpatterns = [
        path('admin/', admin.site.urls),
        path('/', views.page),
#       path('index', ' appconsole.view.index.page'),
]

But sadly, when I run runserver, I got that message

can not import name ‘views’

The traditional structure is that within appconsole, you would create a file named views.py. Then your function page is a function within that file.