• Register
0 votes
340 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

Please log in or register to answer this question.

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 80 views
80 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, 2020 Martin K 6.6k points
0 votes
1 answer 19 views
0 votes
1 answer 1.9K views
1.9K 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 304 views
304 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, 2020 samhaz 5k points
0 votes
1 answer 239 views
239 views
Problem : When I try to run following command: python manage.py updatestats I get the error as below: “TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'” I am unable to figure out ... ) File "/Users/alecxe/project/Dev/project_files/project/main/management/commands/updatestats.py", line 34, in handle unique_visits=display_unique_visits(hostname),)
asked Jan 2, 2020 alecxe 7.5k points
0 votes
1 answer 239 views
239 views
Problem: Hello kodlogs, I am not good in the object oriented programming just know the few concepts about the java object-oriented like inheritance, polymorphism operator overloading, method overloading and super function.But when it comes to python object it is difficult to me ... list(range(0,5)) d_e = defaultdict(lst) O Error: TypeError: first argument must be callable or None Please Help !
asked Jun 17, 2020 Gavin 15.3k points
1 vote
1 answer 28 views
28 views
Problem: I have a program where I used tuple and it is throwing e some eror message. The code I worte is : import random board = [] for x in range(0,5): board.append(["O"] * 5) def print_board(board): for row in board: print " ".join(row) print_board(board) ... len(board)-1 or guess_col < 0 or guess_col > len(board[0])-1: TypeError: list indices must be integers not tuple How to solve this thing?
asked Apr 27, 2020 samhaz 5k points
3 votes
2 answers 232 views
232 views
Problem: I am trying to learn python3 and tuple. It was going pretty smooth unless I tried to access an element from a tuple of my program. It throws back an error: typeerror: tuple indices must be integers or slices, not tuple What's wrong? Any explanation?
asked Mar 25, 2020 Gavin 15.3k points
0 votes
2 answers 464 views
464 views
Problem : I want to set up the multiple setting files (e.g. Dev, Prod, ..) that include some base settings also. But unable to gain successes in my goal. Whenever I am trying to run my ./manage.py runserver I ... == "__main__": os.environ.setdefault("DJANGO_SETTINGS_MODULE", "cb.settings.development") from django.core.management import execute_from_command_line execute_from_command_line(sys.argv)
asked Dec 5, 2019 alecxe 7.5k points