【问题标题】:Problem Extending Django-Oscar's layout.html扩展 Django-Oscar 的 layout.html 的问题
【发布时间】:2021-04-02 19:20:54
【问题描述】:

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

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

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

我试图确保我有最简单的项目来显示问题。在README.md 中,我显示了在网络浏览器中显示的完整错误消息。

# /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

【问题讨论】:

    标签: django django-oscar


    【解决方案1】:

    问题出在这里:

    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() 的参数不太正确。路径的signature 是:

    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 配置来解决此问题,该配置当前设置为[]

    【讨论】:

    • 哇,迫不及待想明天试试这个。你是怎么想到的?
    • 只看代码哈哈。如果你和我一样花费大量时间在 Django 上工作,你往往会形成一种在哪里寻找问题的感觉。
    • 似乎re_path(和path)不遵循如何在Python 3中使用*args和**kwargsOrdering Arguments部分中描述的约定>
    猜你喜欢
    • 2021-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-09
    • 2012-10-09
    • 2011-10-14
    • 1970-01-01
    相关资源
    最近更新 更多