Django URLs error: view must be a callable or a list/tuple in the case of include()

我现在的Django version 是 1.11.2,
the folling solution is come from stackoverflow:==================================================================-----question:
After upgrading to Django 1.10,I get the error:
  TypeError: view must be a callable or a list/tuple in the case of include().

My urls.py is as follows:

urlpatterns = [
    url(r‘^$‘, ‘myapp.views.home‘),
    url(r‘^contact/$‘, ‘myapp.views.contact‘),
    url(r‘^login/$‘, ‘django.contrib.auth.views.login‘),
]
 

After upgrading to Django 1.10, I get the error:

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

My urls.py is as follows:

urlpatterns = [
    url(r‘^$‘, ‘myapp.views.home‘),
    url(r‘^contact/$‘, ‘myapp.views.contact‘),
    url(r‘^login/$‘, ‘django.contrib.auth.views.login‘),
]

The full traceback is:

Traceback (most recent call last):
  File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 121, in inner_run
    self.check(display_num_errors=True)
  File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/core/management/base.py", line 385, in check
    include_deployment_checks=include_deployment_checks,
  File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/core/management/base.py", line 372, in _run_checks
    return checks.run_checks(**kwargs)
  File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/core/checks/registry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/core/checks/urls.py", line 14, in check_url_config
    return check_resolver(resolver)
  File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/core/checks/urls.py", line 24, in check_resolver
    for pattern in resolver.url_patterns:
  File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/urls/resolvers.py", line 310, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/urls/resolvers.py", line 303, in urlconf_module
    return import_module(self.urlconf_name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/alasdair/dev/urlproject/urlproject/urls.py", line 28, in <module>
    url(r‘^$‘, ‘myapp.views.home‘),
  File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/conf/urls/__init__.py", line 85, in url
    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().
-------the solutions:

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

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

  

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‘),
]

If there are many views, then importing them individually can be inconvenient. An alternative 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 used as myapp_views and as auth_views, which allows us to import the views.py from multiple apps without them clashing.

See the Django URL dispatcher docs for more information about urlpatterns.



时间: 2024-08-09 22:02:32

Django URLs error: view must be a callable or a list/tuple in the case of include()的相关文章

Django urls.py报错: raise TypeError(&#39;view must be a callable or a list/tuple in the case of include()

Django urls.py报错: raise TypeError('view must be a callable or a list/tuple in the case of include() 报错信息 "F:\PyCharm 2016.2.2\bin\runnerw.exe" C:\Users\Administrator\AppData\Local\Programs\Python\Python35\python.exe F:/Django/blogs/manage.py run

django错误笔记——TypeError: view must be a callable or a list/tuple in the case of include().解决办法

django增加用户认证模块时,总是提醒模块的url.py中 url(r'^login/$', 'django.contrib.auth.views.login', name='login'),出错: TypeError: view must be a callable or a list/tuple in the case of include(). 解决办法:改为下面的写法 from django.contrib.auth.views import login ... url(r'^logi

django 运行报错view must be a callable or a list/tuple in the case of include()

网上找了相关资料发现 django 1.10之后不在支持URL用字符串表示了 http://stackoverflow.com/questions/38744285/django-urls-error-view-must-be-a-callable-or-a-list-tuple-in-the-case-of-includ 那我们换种写法, from myindex.bb import hi import myindex urlpatterns = [ url(r'^admin/', admin

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

原文连接: http://www.imooc.com/qadetail/98920 我是这么写的就好了 from django.conf.urls import url from django.contrib import admin from blogapp import views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'helloworld',views.hello) ] django 1.10版本改了写法了.首先要在

Django urls配置方式

例子以上一篇随笔中的blog应用进行. urls.py默认生成的格式如下: 1 """whsweb URL Configuration 2 3 The `urlpatterns` list routes URLs to views. For more information please see: 4 https://docs.djangoproject.com/en/1.8/topics/http/urls/ 5 Examples: 6 Function views 7 1

Django框架 之 view视图

浏览目录 概述 简单的视图 HttpRequest对象 CBV和FBV 给视图加装饰器 Request对象 Response对象 JsonResponse对象 Django shortcut functions 一.概述 一个视图函数(类),简称视图,是一个简单的Python 函数(类),它接受Web请求并且返回Web响应. 响应可以是一张网页的HTML内容,一个重定向,一个404错误,一个XML文档,或者一张图片. 无论视图本身包含什么逻辑,都要返回响应.代码写在哪里也无所谓,只要它在你当前项

python3开发进阶-Django视图(View)的常见用法

阅读目录 简述Django的View(视图) CBV和FBV Request对象和Response对象 Django组件(render,redirect)详解 一.简述Django的View(视图) 一个视图函数(类),简称视图,是一个简单的Python 函数(类),它接受Web请求并且返回Web响应. 响应可以是一张网页的HTML内容,一个重定向,一个404错误,一个XML文档,或者一张图片. 无论视图本身包含什么逻辑,都要返回响应.代码写在哪里也无所谓,只要它在你当前项目目录下面. 除此之外

Django视图系统(view)

一个视图函数(类),简称视图,是一个简单的Python 函数(类),它接受Web请求并且返回Web响应. 响应可以是一张网页的HTML内容,一个重定向,一个404错误,一个XML文档,或者一张图片. 无论视图本身包含什么逻辑,都要返回响应.代码写在哪里也无所谓,只要它在你当前项目目录下面. 一. CBV和FBV 1. FBV是基于函数的view def add_class(request): if request.method == "POST": class_name = reque

Django 类方式view进行进行用户验证

问题: Django中,view的书写方式有两种,一种是def函数方式,一种是class方式,函数方式通过@login_required装饰器标记函数,就必须通过用户验证,而类,则没办法通过此方法进行标记 那,如何解决这个问题? 利用类的继承方式,写一个基类,需要验证的class view类,首先继承这个基类,后面所有通过此类的url都需要进行用户验证登录,因为可能有许多的view需要使用该类,需要独立出来,建立在utils目录下,起名mixin_is_login.py,然后在各种需要的view