• Register
0 votes
225 views

Problem :

After upgrading it to Django 1.10, I get below error:

“TypeError: view must be a callable or a list/tuple in the case of include().”

My urls.py is as below :

from django.conf.urls import include, url
urlpatterns = [
    url(r'^$', 'myapp.views.home'),
    url(r'^contact/$', 'myapp.views.contact'),
    url(r'^login/$', 'django.contrib.auth.views.login'),
]

How can I fix above error?

7 5 2
3,870 points

2 Answers

0 votes

Solution :

I had also faced very similar issue in a recent past. I had lot of research on it and found a solution on it. This is a very common problem with a people getting started.

Django 1.10 no longer allows you to specify the views as the string (e.g. 'myapp.views.home') in the URL patterns.

So a solution is to update your urls.py to include a view callable. This means that you must have to import a view in your urls.py. If your URL patterns don't have the names, then now is the good time to add one, as reversing with a dotted python path no longer works.

If there are many views, then importing them individually can be inconvenient for you. So the alternative is to import your views module from the app.

Please Note that we have used the as myapp_views and as auth_views, which allowd us to import a views.py from the multiple apps without them clashing.

Please See a Django URL dispatcher docs for more information about urlpatterns.

9 7 4
38,600 points
0 votes

Solution:

Django 1.10 no longer approves you to seclude views as a string ( 'myapp.views.home') in your URL patterns.

The solution is to update your urls.py to add the view callable. This imply that you have to import the view in your urls.py. In case your URL patterns don't have names, then now is a good time to include one, since reversing with the dotted python path no longer performs.

from django.conf.urls import include, url

from django.contrib.auth.views import login
from myapp.views import home, contact

urlpatterns = [
    url(r'^$', home, name='home'),
    url(r'^contact/$', contact, name='contact'),
    url(r'^login/$', login, name='login'),
]

In case there are many views, then importing them separately can be in expedient. An alternative is to import the views module from your app.

from django.conf.urls import include, url

from django.contrib.auth import views as auth_views
from myapp import views as myapp_views

urlpatterns = [
    url(r'^$', myapp_views.home, name='home'),
    url(r'^contact/$', myapp_views.contact, name='contact'),
    url(r'^login/$', auth_views.login, name='login'),
]

Note that we have employed as myapp_views and as auth_views, which approves us to import the views.py from multiple apps without them clashing.

This error only imply that myapp.views.home is not something that can be called, like a function. It is a string in fact at the time  your solution performs in django 1.9, notwithstanding it throws a warning saying this will reprove from version 1.10 onwards, which is accurately what has occured. The former solution  imports the requires view functions into the script through either from myapp import views as myapp_views or from myapp.views import home, contact

You may also obtain this error in case you have a name clash of a view and a module. I've got the error at the time i allot my view files underneath views folder, /views/view1.py, /views/view2.py and imported some model named table.py in view2.py which occoured to be a name of a view in view1.py. So naming the view functions as  v_table(request,id) helped.

The resolution is to update your urls.py to add the view callable. This imply that you have to import the view in your urls.py. In case your URL patterns don't have names, then now is a good time to include one, since turn over with the dotted python path no longer performs.

from django.contrib.auth.views import login
from myapp.views import home, contact

urlpatterns = [
    url(r'^$', home, name='home'),
    url(r'^contact/$', contact, name='contact'),
    url(r'^login/$', login, name='login'),
]

In case there are many views, then importing them separately can be difficult. An substitute is to import the views module from your app.

from django.contrib.auth import views as auth_views
from myapp import views as myapp_views

urlpatterns = [
    url(r'^$', myapp_views.home, name='home'),
    url(r'^contact/$', myapp_views.contact, name='contact'),
    url(r'^login/$', auth_views.login, name='login'),
]

Note that we have employed as myapp_views and as auth_views, which approves us to import the views.py from multiple apps without them clashing.

10 6 4
31,120 points

Related questions

1 vote
1 answer 39 views
39 views
Problem: I have fundamental knowledge of Django. I have learned Django with the help of online tutorials. Recently I have started working on Django. I have recently tried to upgrade my Django to the Django 1.10. Since then I am often facing below error: TypeError: ... ), ] I have only fundamental knowledge about Django so unable to fix the above error. Kindly help me in fixing above Django error.
asked Jun 8 Martin K 6.6k points
0 votes
1 answer 1.5K views
1.5K views
Problem : I am facing the issue in passing a function as a parameter to another function as follows typeerror: int() argument must be a string, a bytes-like object or a number, not 'nonetype'
asked Nov 18, 2019 peterlaw 6.9k points
1 vote
1 answer 62 views
62 views
Problem: I wrote a program in which gives me the following error : TypeError: startswith first arg must be bytes or a tuple of bytes, not str The program I wrote is: def allFasta(filename): xd = open(filename, 'yb') headers = "" sequence = "" while True: ... ("youn.xd") Why is this type error message is appearing for this program. Is there any problem with my programs arguments? Thanks in advance.
asked Jun 11 samhaz 5k points