Django,上下文字典没有传递给模板? (for 循环 returns 没有)
Django, context dict not passing through to template? (for loop returns nothing)
我搜索了这个但找不到任何东西,所以就在这里..
我的视图是这样设置的,status.html for 循环工作正常,但 tasks.html 根本没有注册我的 for 循环,我也不知道我在做什么比较 2 视图时做错了 functions/templates.
观看次数
from django.shortcuts import render
from todo.models import List, Item
# Create your views here.
def current_tasks(request):
tasks = List.objects.all()
tasks_dict = {'tasks': tasks}
return render(request, 'tasks.html', tasks_dict)
def status_report(request):
todo_listing = []
for todo_list in List.objects.all():
todo_dict = {}
todo_dict['list_object'] = todo_list
todo_dict['item_count'] = todo_list.item_set.count()
todo_dict['items_complete'] = todo_list.item_set.filter(completed=True).count()
todo_dict['percent_complete'] = int(float(todo_dict['items_complete']) / todo_dict['item_count'] * 100)
todo_listing.append(todo_dict)
return render(request, 'status.html', {'todo_listing': todo_listing})
tasks.html
{% extends 'base.html' %}
{% block content %}
{% for tasks in tasks_dict %}
<ul><li>{{tasks.title}}</li><
<li>{{tasks.created_date}}</li>
<li>{{tasks.priority}}</li>
<li>{{tasks.completed}}</li></ul>
{% endfor %}
{% endblock %}
status.html
{% extends 'base.html' %}
{% block content %}
<h1>Task List Status Report</h1>
{% for list_dict in todo_listing %}
<h2>{{ list_dict.list_object.title }}</h2>
<ul>
<li>Number of items: {{ list_dict.item_count }}</li>
<li>Number completed: {{ list_dict.items_complete }} ({{ list_dict.percent_complete }}%)</li>
</ul>
{% endfor %}
{% endblock %}
</html>
您没有向您的模板发送任何名为 tasks_dict
的内容;那只是上下文字典的本地名称。您发送到包含任务的模板的内容称为 tasks
.
我搜索了这个但找不到任何东西,所以就在这里..
我的视图是这样设置的,status.html for 循环工作正常,但 tasks.html 根本没有注册我的 for 循环,我也不知道我在做什么比较 2 视图时做错了 functions/templates.
观看次数
from django.shortcuts import render
from todo.models import List, Item
# Create your views here.
def current_tasks(request):
tasks = List.objects.all()
tasks_dict = {'tasks': tasks}
return render(request, 'tasks.html', tasks_dict)
def status_report(request):
todo_listing = []
for todo_list in List.objects.all():
todo_dict = {}
todo_dict['list_object'] = todo_list
todo_dict['item_count'] = todo_list.item_set.count()
todo_dict['items_complete'] = todo_list.item_set.filter(completed=True).count()
todo_dict['percent_complete'] = int(float(todo_dict['items_complete']) / todo_dict['item_count'] * 100)
todo_listing.append(todo_dict)
return render(request, 'status.html', {'todo_listing': todo_listing})
tasks.html
{% extends 'base.html' %}
{% block content %}
{% for tasks in tasks_dict %}
<ul><li>{{tasks.title}}</li><
<li>{{tasks.created_date}}</li>
<li>{{tasks.priority}}</li>
<li>{{tasks.completed}}</li></ul>
{% endfor %}
{% endblock %}
status.html
{% extends 'base.html' %}
{% block content %}
<h1>Task List Status Report</h1>
{% for list_dict in todo_listing %}
<h2>{{ list_dict.list_object.title }}</h2>
<ul>
<li>Number of items: {{ list_dict.item_count }}</li>
<li>Number completed: {{ list_dict.items_complete }} ({{ list_dict.percent_complete }}%)</li>
</ul>
{% endfor %}
{% endblock %}
</html>
您没有向您的模板发送任何名为 tasks_dict
的内容;那只是上下文字典的本地名称。您发送到包含任务的模板的内容称为 tasks
.