【发布时间】:2017-03-03 01:20:46
【问题描述】:
我一直在寻找在视图/模板中显示来自 Django 模型的数据的方法。我仍然只是一个初学者,但已经阅读了很多 stackoverflow 以获得知识。
我正在尝试做的事情的实现我从这个线程中挑选出来的:Display table of objects django
输入 template.html 和 views.py 后(稍微修改我的 views.py - 将 MyModel 更改为 Environment Model)我收到错误:
"Invalid block tag on line 5: 'get_verbose_name', expected 'empty' or 'endfor'. Did you forget to register or load this tag?"
这个错误是指<th>{% get_verbose_name field %}</th>
在模板.html中
我尝试为模型中的每个字段添加详细名称,但这不是明显的问题。我的views.py、models.py和template.html附加在一个pastebin中。
我的views.py 和template.html 与在链接的stackoverflow 线程中检查的解决方案几乎完全相同。我的问题基本上是理解为什么会出现这个错误,以及如何解决它
感谢您的帮助
tables.html
<table>
<thead>
<tr>
{% for field in cached_fields %}
<th>{% get_verbose_name field %}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for d in data %}
<tr>
{% for field in fields %}
<td>{% get_value_from_key d field %}</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
Views.py
from __future__ import unicode_literals
from django.views import generic
from django.shortcuts import render, render_to_response, get_object_or_404, redirect
from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import HttpRequest, HttpResponse
from django.template import RequestContext
import simplejson as json
#from appDatabase.forms import *
from . import forms;
from . import models;
class AppTable(generic.TemplateView):
template_name = "appDatabase/tables.html"
def index(request) :
fields = Environment._meta.fields
data = json.loads(serializers.serialize("json", Environment.objects.all()))
def parse_data(data):
result = []
# flatten the dictionary
def flatten_dict(d):
"""
Because the only nested dict here is the fields, let's just
remove the 'fields' suffix so that the fields can be loaded in
template by name
"""
def items():
for key, value in d.items():
if isinstance(value, dict):
for subkey, subvalue in flatten_dict(value).items():
yield subkey, subvalue
else:
yield key, value
return dict(items())
for d in data:
# change the 'pk' key name into its actual name in the database
d[Environment._meta.pk.name] = d.pop('pk')
# append the flattend dict of each object's field-value to the result
result.append(flatten_dict(d))
return result
context_instance = RequestContext(request, {
'data' : parse_data(data),
'fields' : fields, })
return TemplateResponse(request, 'tables.html', context_instance)
模型.py
from __future__ import unicode_literals
from django.utils.encoding import python_2_unicode_compatible
import uuid
from django.contrib.auth.models import User
from django.db import models
from django import forms
from django.forms import ModelForm;
# Create your models here.
class Environment(CommonInfo):
environment_Type = models.CharField(verbose_name ="Environment Type", max_length=10)
environment_Name = models.CharField(verbose_name ="Environment Name", max_length=10)
【问题讨论】:
-
没有标签叫
get_verbose_name -
您需要注册新标签
-
您需要创建一个自定义模板标签并使其在您的模板中可用。 docs.djangoproject.com/en/1.10/howto/custom-template-tags/…
标签: python django django-templates