Django urlpatterns-太多值无法解压缩

时间:2019-01-12 19:56:47

标签: python django

我是Python和Django的初学者,致力于一本书中的简单IoT教程。该项目是通过Django应用程序控制LED电路。复制代码后,由于书中的Python和Django版本是旧版本,因此几乎没有出现任何错误。我正在使用Python 3.7.2和Django 2.1.4进行搜索,但是找不到修复代码的方法。

我的项目目录如下

Project1

  • Project1
    • __ init __。py
    • settings.py
    • urls.py
    • wsgi.py
  • FirstApp
    • admin.py
    • apps.py
    • controller.py
    • index.html
    • models.py
    • serializers.py
    • tests.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)

2 个答案:

答案 0 :(得分:2)

您需要使用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

答案 1 :(得分:0)

您必须使用include()添加在路由器中注册的路径:

from django.urls import path, include .  # <---------- include()
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'^', include(router.urls)),   # <--------- include()
    url(r'^api-auth/', 'rest_framework.urls',namespace='rest_framework'),
    url(r'^admin/', admin.site.urls),
    url(r'^home/', 'FirstApp.views.home')
]