<i id='HzToD'><tr id='HzToD'><dt id='HzToD'><q id='HzToD'><span id='HzToD'><b id='HzToD'><form id='HzToD'><ins id='HzToD'></ins><ul id='HzToD'></ul><sub id='HzToD'></sub></form><legend id='HzToD'></legend><bdo id='HzToD'><pre id='HzToD'><center id='HzToD'></center></pre></bdo></b><th id='HzToD'></th></span></q></dt></tr></i><div id='HzToD'><tfoot id='HzToD'></tfoot><dl id='HzToD'><fieldset id='HzToD'></fieldset></dl></div>

    <bdo id='HzToD'></bdo><ul id='HzToD'></ul>
  • <tfoot id='HzToD'></tfoot>
    1. <small id='HzToD'></small><noframes id='HzToD'>

        <legend id='HzToD'><style id='HzToD'><dir id='HzToD'><q id='HzToD'></q></dir></style></legend>

        Django URLs TypeError: view must be a callable or a list/tup

        时间:2024-04-20

            <tbody id='wToIb'></tbody>
          <legend id='wToIb'><style id='wToIb'><dir id='wToIb'><q id='wToIb'></q></dir></style></legend>
        1. <tfoot id='wToIb'></tfoot>
              <i id='wToIb'><tr id='wToIb'><dt id='wToIb'><q id='wToIb'><span id='wToIb'><b id='wToIb'><form id='wToIb'><ins id='wToIb'></ins><ul id='wToIb'></ul><sub id='wToIb'></sub></form><legend id='wToIb'></legend><bdo id='wToIb'><pre id='wToIb'><center id='wToIb'></center></pre></bdo></b><th id='wToIb'></th></span></q></dt></tr></i><div id='wToIb'><tfoot id='wToIb'></tfoot><dl id='wToIb'><fieldset id='wToIb'></fieldset></dl></div>

              <small id='wToIb'></small><noframes id='wToIb'>

                <bdo id='wToIb'></bdo><ul id='wToIb'></ul>

                  本文介绍了Django URLs TypeError: view must be a callable or a list/tuple in the case of include()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  升级到 Django 1.10 后,出现错误:

                  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().
                  

                  我的urls.py如下:

                  My urls.py is as follows:

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

                  完整的回溯是:

                  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().
                  

                  推荐答案

                  Django 1.10 不再允许您在 URL 模式中将视图指定为字符串(例如 'myapp.views.home').

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

                  解决方案是更新您的 urls.py 以包含可调用的视图.这意味着您必须在 urls.py 中导入视图.如果您的 URL 模式没有名称,那么现在是添加名称的好时机,因为使用点分 Python 路径进行反转不再有效.

                  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.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'),
                  ]
                  

                  如果有很多视图,那么单独导入它们可能会很不方便.另一种方法是从您的应用程序中导入视图模块.

                  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.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'),
                  ]
                  

                  请注意,我们使用了 as myapp_viewsas auth_views,这允许我们从多个应用程序导入 views.py 而不会发生冲突.

                  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.

                  请参阅 Django URL 调度程序文档关于 urlpatterns 的更多信息.

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

                  这篇关于Django URLs TypeError: view must be a callable or a list/tuple in the case of include()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:仅在 Django 启动 ONCE 时执行代码? 下一篇:Pylint“未解决的导入";Visual Studio 代码中的错误

                  相关文章

                    <bdo id='8RDaO'></bdo><ul id='8RDaO'></ul>
                  <legend id='8RDaO'><style id='8RDaO'><dir id='8RDaO'><q id='8RDaO'></q></dir></style></legend>

                  <tfoot id='8RDaO'></tfoot>

                  1. <i id='8RDaO'><tr id='8RDaO'><dt id='8RDaO'><q id='8RDaO'><span id='8RDaO'><b id='8RDaO'><form id='8RDaO'><ins id='8RDaO'></ins><ul id='8RDaO'></ul><sub id='8RDaO'></sub></form><legend id='8RDaO'></legend><bdo id='8RDaO'><pre id='8RDaO'><center id='8RDaO'></center></pre></bdo></b><th id='8RDaO'></th></span></q></dt></tr></i><div id='8RDaO'><tfoot id='8RDaO'></tfoot><dl id='8RDaO'><fieldset id='8RDaO'></fieldset></dl></div>

                    <small id='8RDaO'></small><noframes id='8RDaO'>