【问题标题】:How to make the Django template include work [closed]如何使 Django 模板包含工作 [关闭]
【发布时间】:2017-12-23 08:26:41
【问题描述】:

我有三个主要页面,一个和两个。我的要求是当我访问主页面时,我需要查看第一页和第二页。

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-32\lib\site-packages\django\template\base.py in __init__, line 668
Python Executable:  C:\Program Files (x86)\Python36-32\python.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

【问题讨论】:

    标签: python django django-templates


    【解决方案1】:

    您的包含标签中包含无效的大引号。用单引号替换它们。

    【讨论】:

      【解决方案2】:

      Daniel Roseman's answer 正确指出您使用的是大引号。这应该是固定的。

      还有另一个问题 - 您错误地将 URL 传递给 include。相反,应该将文件路径传递给您包含的模板(相对于templates 目录)。即:

      {% include 'task/one.html' %}
      

      请参阅docs for the include template tag

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-05-16
        • 2012-03-16
        • 1970-01-01
        • 2015-07-30
        • 1970-01-01
        • 2019-02-28
        • 2019-08-11
        • 2013-06-15
        相关资源
        最近更新 更多