django - 扩展 Django-Oscar 的 layout.html 的问题
问题描述
在我将模板添加到最小的 web 应用程序后,问题出现了。模板extends
django-oscar 的layout.html
. 项目中没有其他内容extends
layout.html
。
我的目标只是能够使用 django-oscar 模板在我的 webapp 中形成网页的基础。出于某种原因,我的问题没有尽头。这只是最新的错误消息。这几天我一直在为此苦苦挣扎!当我解决一个问题时,会出现另一个问题。
我为这个问题做了一个最小的 git 仓库:https ://github.com/mslinn/django_oscar_problem
该仓库有一个requirements.txt
文件,以防有人想要安装运行程序所需的 PIP 模块。
我试图确保我有一个显示问题的最简单的项目。在README.md
I 中显示了 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
解决方案
问题在这里:
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配置(当前设置为[]
.
推荐阅读
- javascript - 使用 jquery 从引导选择选择器文本框中获取值
- python - 在 python 中实现 Sutton 的 One-Step Actor-Critic 伪代码的问题
- encryption - AES-GCM 解密慢 JAVA
- java - Swing setLayout Bug
- php - 如何删除 URL 中的 index.php
- tcp - 进程终止后,WireMock.Net 使端口保持打开状态
- go - 根据我之前是否打印出切片,在附加切片后计算 sha256 会给出不同的结果
- cassandra - CassandraIO:插入日期字段不起作用
- python - 使用 Selenium Python Webdriver 检查我在字段中输入的文本字符串是否正确填充
- amazon-web-services - 创建 lambda 时如何获取 cloudformation 创建的角色