gpt4 book ai didi

python - Django urlpatterns - 太多值无法解压

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

我是 Python 和 Django 的初学者,正在研究书中找到的简单 IoT 教程。该项目是从 Django 应用程序控制 LED 电路。我复制代码后,很少出现错误,因为书中的Python和Django版本是旧版本。我正在使用 Python 3.7.2 和 Django 2.1.4,经过搜索,我找不到修复代码的方法。

我的项目目录如下

项目1

  • 项目1
    • __init__.py
    • 设置.py
    • url.py
    • wsgi.py
  • 第一应用
    • admin.py
    • apps.py
    • Controller .py
    • index.html
    • 模型.py
    • 序列化器.py
    • 测试.py
    • views.py

我面临的问题是在 urls.py 中,其代码是:

from django.conf.urls import *
from django.contrib import admin
from rest_framework import routers
from FirstApp import views

admin.autodiscover()

router = routers.DefaultRouter()
router.register(r'mode',views.ModeViewSet)
router.register(r'state',views.StateViewSet)

urlpatterns = [
url(r'^', router.urls),
url(r'^api-auth/', 'rest_framework.urls',namespace='rest_framework'),
url(r'^admin/', admin.site.urls),
url(r'^home/', 'FirstApp.views.home')
]

错误回溯是这样的

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7593e6a8>
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/django/utils/autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run
self.check(display_num_errors=True)
File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 379, in check
include_deployment_checks=include_deployment_checks,
File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 366, in _run_checks
return checks.run_checks(**kwargs)
File "/usr/local/lib/python3.7/site-packages/django/core/checks/registry.py", line 71, in run_checks
new_errors = check(app_configs=app_configs)
File "/usr/local/lib/python3.7/site-packages/django/core/checks/urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "/usr/local/lib/python3.7/site-packages/django/core/checks/urls.py", line 23, in check_resolver
return check_method()
File "/usr/local/lib/python3.7/site-packages/django/urls/resolvers.py", line 396, in check
for pattern in self.url_patterns:
File "/usr/local/lib/python3.7/site-packages/django/utils/functional.py", line 37, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python3.7/site-packages/django/urls/resolvers.py", line 533, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/local/lib/python3.7/site-packages/django/utils/functional.py", line 37, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python3.7/site-packages/django/urls/resolvers.py", line 526, in urlconf_module
return import_module(self.urlconf_name)
File "/usr/local/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/pi/Desktop/Cpeg389project/Cpeg389project/urls.py", line 28, in <module>
url(r'^', router.urls),
File "/usr/local/lib/python3.7/site-packages/django/conf/urls/__init__.py", line 13, in url
return re_path(regex, view, kwargs, name)
File "/usr/local/lib/python3.7/site-packages/django/urls/conf.py", line 61, in _path
urlconf_module, app_name, namespace = view
ValueError: too many values to unpack (expected 3)

最佳答案

您需要使用 include 添加路由器 URL 以及身份验证 URL:

urlpatterns = [
url(r'^', include(router.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
url(r'^admin/', admin.site.urls),
url(r'^home/', views.home)
]

请参阅DRF Routers docs .

关于python - Django urlpatterns - 太多值无法解压,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54163339/

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