如何使Django模板包括工作

时间:2017-12-23 08:26:42

标签: python django django-templates

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

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

2 个答案:

答案 0 :(得分:1)

您的包含标记中有引号,这些引号无效。用直的单引号替换它们。

答案 1 :(得分:0)

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

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

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

请参阅docs for the include template tag