如何使Django模板包括工作[关闭]
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使Django模板包括工作[关闭]相关的知识,希望对你有一定的参考价值。
我有三页主页,一页和两页。我的要求是,当我访问页面主页时,我需要查看第一页和第二页。
html:
main.html
{% extends 'base.html' %}
{% load staticfiles %}
{% load my_tag %}
{% block title %}
<title>Main TAB</title>
{% endblock %}
{% block body %}
<div>
{% include ‘test/one/’ %}
</div>
<div class="container">
{{name}}
</div>
<script type="text/javascript" src="{% static "js/testquery.js"%}">
</script>
{% endblock %}
one.html
<div class="container">
{{ name }}
</div>
two.html
<div class="container">
{{ name }}
</div>
view.py
def main_page(request):
try:
main_data = {'name':"main"}
return render(request, 'task/main.html', {'name': main_data})
except Exception as e:
raise
def one_page(request):
try:
main_data = "one"
return render(request, 'task/one.html', {'name': main_data})
except Exception as e:
raise
def two_page(request):
try:
main_data = "Two"
return render(request, 'task/two.html', {'name': main_data})
except Exception as e:
raise
url.py
urlpatterns = [
url(r'^$', taskpage,name="task_master"),
path('Task/<int:taskid>', show_task,name='show_task'),
path('Task/<int:taskid>/update',
updatetaskpage,name='updatetask_page'),
path('Test/Main/', main_page,name='main'),
path('Test/one/', one_page,name='one'),
path('Test/two/', two_page,name='two'),
]
我收到以下错误:
TemplateSyntaxError at /taskmaster/Test/Main/
Could not parse the remainder: '‘test/one.html’' from '‘test/one.html’'
Request Method: GET
Request URL: http://localhost:8000/taskmaster/Test/Main/
Django Version: 2.0
Exception Type: TemplateSyntaxError
Exception Value:
Could not parse the remainder: '‘test/one.html’' from '‘test/one.html’'
Exception Location: C:Program Files (x86)Python36-32libsite-packagesdjango emplatease.py in __init__, line 668
Python Executable: C:Program Files (x86)Python36-32python.exe
Python Version: 3.6.1
Python Path:
['C:\Users\i326707\PycharmProjects\opsboard',
'C:\Program Files (x86)\Python36-32\python36.zip',
'C:\Program Files (x86)\Python36-32\DLLs',
'C:\Program Files (x86)\Python36-32\lib',
'C:\Program Files (x86)\Python36-32',
'C:\Program Files (x86)\Python36-32\lib\site-packages',
'C:\Users\i326707\PycharmProjects\opsboard\commonpage',
'C:\Users\i326707\PycharmProjects\opsboard\taskmaster']
Server time: Sat, 23 Dec 2017 08:10:28 +0000
答案
您的include标记中有引号,这些引号无效。用直的单引号替换它们。
另一答案
Daniel Roseman's answer正确指出您使用的是引号。这应该是固定的。
还有另一个问题 - 您错误地将URL传递给include
。相反,应该将文件路径传递给您包含的模板(相对于templates
目录)。即:
{% include 'task/one.html' %}
见docs for the include
template tag。
以上是关于如何使Django模板包括工作[关闭]的主要内容,如果未能解决你的问题,请参考以下文章