gpt4 book ai didi

python - Django新手错误: - TypeError: view must be a callable or a list/tuple in the case of include()

转载 作者:行者123 更新时间:2023-12-01 06:23:14 26 4
gpt4 key购买 nike

我是 python Django 的新手,想知道为什么没有创建 View 。这是我的 urls.py,位于 myproject 下。

from django.contrib import admin

from django.urls import path,include
from django.conf.urls import url
admin.autodiscover()

urlpatterns = ['',
path('admin/', admin.site.urls),
url('myapp/', include('myapp.url'))
]

这是我的 view.py,位于 myapp 文件夹中

from django.shortcuts import render

# Create your views here.
def hello(request):
return render(request,"myapp/templates/hello.html", {})

这是 myapp 文件夹中的 url.py

from django.conf.urls import include, url

urlpatterns = ['', url('hello/', 'views.hello', name='hello'),]

这是完整的回溯

Traceback (most recent call last):
File "/usr/lib/python3.7/threading.py", line 926, in _bootstrap_inner
self.run()
File "/usr/lib/python3.7/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "/home/mayureshkadam/Learning-Django/lib/python3.7/site-packages/django/utils/autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "/home/mayureshkadam/Learning-Django/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run
self.check(display_num_errors=True)
File "/home/mayureshkadam/Learning-Django/lib/python3.7/site-packages/django/core/management/base.py", line 395, in check
include_deployment_checks=include_deployment_checks,
File "/home/mayureshkadam/Learning-Django/lib/python3.7/site-packages/django/core/management/base.py", line 382, in _run_checks
return checks.run_checks(**kwargs)
File "/home/mayureshkadam/Learning-Django/lib/python3.7/site-packages/django/core/checks/registry.py", line 72, in run_checks
new_errors = check(app_configs=app_configs)
File "/home/mayureshkadam/Learning-Django/lib/python3.7/site-packages/django/core/checks/urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "/home/mayureshkadam/Learning-Django/lib/python3.7/site-packages/django/core/checks/urls.py", line 23, in check_resolver
return check_method()
File "/home/mayureshkadam/Learning-Django/lib/python3.7/site-packages/django/urls/resolvers.py", line 407, in check
for pattern in self.url_patterns:
File "/home/mayureshkadam/Learning-Django/lib/python3.7/site-packages/django/utils/functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/mayureshkadam/Learning-Django/lib/python3.7/site-packages/django/urls/resolvers.py", line 588, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/mayureshkadam/Learning-Django/lib/python3.7/site-packages/django/utils/functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/mayureshkadam/Learning-Django/lib/python3.7/site-packages/django/urls/resolvers.py", line 581, in urlconf_module
return import_module(self.urlconf_name)
File "/usr/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/mayureshkadam/PycharmProjects/Learning-Django/myproject/myproject/urls.py", line 23, in <module>
url('myapp/', include('myapp.url'))
File "/home/mayureshkadam/Learning-Django/lib/python3.7/site-packages/django/urls/conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "/usr/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/mayureshkadam/PycharmProjects/Learning-Django/myproject/myapp/url.py", line 3, in <module>
urlpatterns = ['', url('hello/', 'views.hello', name='hello'),]
File "/home/mayureshkadam/Learning-Django/lib/python3.7/site-packages/django/conf/urls/__init__.py", line 13, in url
return re_path(regex, view, kwargs, name)
File "/home/mayureshkadam/Learning-Django/lib/python3.7/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().

我读过一些教程,代码是从https://www.tutorialspoint.com/django/django_url_mapping.htm引用的但不确定为什么我会收到错误。

最佳答案

只需将 url 替换为 path:

# main urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('myapp/', include('myapp.url'))
]

# app urls.py
from django.urls import path
from .view import hello

urlpatterns = [
path('hello/', hello, name='hello'),
]

正如您在 documetation 中看到的那样, path 已经被引入(从 Django>=2.0 开始),所以这里不必使用 url 。您遵循的教程适用于旧版本,因此它使用 url 和正则表达式来生成 URL。

关于python - Django新手错误: - TypeError: view must be a callable or a list/tuple in the case of include(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60274886/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com