首页 > 解决方案 > 扩展 Django-Oscar 的 layout.html 的问题

问题描述

在我将模板添加到最小的 web 应用程序后,问题出现了。模板extendsdjango-oscar 的layout.html. 项目中没有其他内容extends layout.html

我的目标只是能够使用 django-oscar 模板在我的 webapp 中形成网页的基础。出于某种原因,我的问题没有尽头。这只是最新的错误消息。这几天我一直在为此苦苦挣扎!当我解决一个问题时,会出现另一个问题。

我为这个问题做了一个最小的 git 仓库:https ://github.com/mslinn/django_oscar_problem 该仓库有一个requirements.txt文件,以防有人想要安装运行程序所需的 PIP 模块。

我试图确保我有一个显示问题的最简单的项目。在README.mdI 中显示了 Web 浏览器中显示的完整错误消息。

# /templates/welcome.html

{% extends 'oscar/layout.html' %}
... etc ...
# main/urls.py

from django.urls import include, path
from django.contrib import admin
from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, 'home'),
    path('admin/', admin.site.urls, 'admin'),
    path('hello/', views.hello, 'hello'),

    path('', include(apps.get_app_config('oscar').urls[0])),
]
# main/views.py

from django.shortcuts import render

def home(request):
    return render(request, "welcome.html", {})

然后我运行了 webapp:

$ ./manage.py runserver

访问http://localhost:8000后浏览器中的错误信息是:

ValueError at /

dictionary update sequence element #0 has length 1; 2 is required

Request Method:     GET
Request URL:    http://localhost:8000/
Django Version:     3.1.6
Exception Type:     ValueError
Exception Value:

dictionary update sequence element #0 has length 1; 2 is required

标签: djangodjango-oscar

解决方案


问题在这里:

urlpatterns = [
    path('', views.home, 'home'),
    path('admin/', admin.site.urls, 'admin'),
    path('hello/', views.hello, 'hello'),

    path('', include(apps.get_app_config('oscar').urls[0])),
]

您传递给的论点path()并不完全正确。路径的签名是:

path(route, view, kwargs=None, name=None)

即,第三个位置参数应该被kwargs传递给视图函数——但是你已经传递了一个字符串,这就是导致有点模糊的错误的原因。我认为您打算将其作为name,在这种情况下,您需要将其作为命名参数提供,即:

urlpatterns = [
    path('', views.home, name='home'),
    path('admin/', admin.site.urls, name='admin'),
    path('hello/', views.hello, name='hello'),

    path('', include(apps.get_app_config('oscar').urls[0])),
]

请注意,这会导致TemplateDoesNotExist示例存储库中出现新错误 - 这是因为templates目录的位置不是 Django 知道要查找的位置。您可以通过将该目录的路径添加到模板设置中的DIRS配置(当前设置为[].


推荐阅读