python - 在 Python Django 中渲染基本 html 模板内的子页面

标签 python django python-3.x django-templates django-views

我正在尝试在基本 html 页面内呈现模板。基本 html 具有整个站点通用的页眉、页脚、菜单。页面的中间部分必须来自子页面。我认为我一切都正确,200 OK,但是它不起作用,我看不到子页面、表单和div的内容。我只看到所有请求的 base.html 。我缺少什么?

project
   |
    ---templates/base.html
    ---templates/childpage.html

base.html

{% load static %}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>

<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="{% static 'css/custom.css' %}">
</head>

<body>

    {% block content %}

    {% endblock %}

</body>

</html>

childpage.html

{% extends "base.html" %}
{% block content %}
<form action="" method="">

</form>
<br><br><br>

<div class="something">
<a>some content</a>
</div>

{% endblock content %}

观看次数

def home(request):
    return render(request, 'base.html')

def askforchild(request):
    return render(request, 'childpage.html')

网址

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url('', views.home, name='Site Home Page'),
    url('askforchild', views.askforchild, name='child page'),
]

最佳答案

主页 "" 的 url 列表必须移至 url 列表的底部。正则表达式正在捕获所有请求作为主页请求。总而言之,URL 列表的底部应该有一个空模式。

关于python - 在 Python Django 中渲染基本 html 模板内的子页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59476576/

相关文章:

python - 为 docker-compose 文件设置预 Hook

django - 如何使用 sorl-thumbnail 调整源大小?

python - 为什么 pandas 不能处理绘图中的小数?

python - 从Python中的字符串中提取日期

python - 为 windows 配置 sqlalchemy

python - 无法弄清楚如何停止返回列表内的列表(leetcode问题)

python - 为什么我的代码找不到数组中存在的某个字符串?

python - 使用 ctypes 数组作为 numpy 数组时的 PEP 3118 警告

mysql - 删除 django 使用的数据库中的表

python - 如何在 Python 中混合使用多个可选参数?