0

models.py查询并在模板显示数据

class ReportType(models.Model): 
    report = models.ForeignKey(Report) 
    title = models.CharField('Incident Type', max_length=200) 
    type = models.ForeignKey(Types, null=False, default=False) 

class Types(models.Model): 
    user = models.ForeignKey(User, null=True) 
    title = models.CharField('Incident Type', max_length=200) 
    parent_type_id = models.CharField('Parent Type', max_length=100, null=True, blank=True) 
    is_active = models.BooleanField('Is Active', default=True) 

views.py

def method(request): 
    report_types = ReportType.objects.filter(report=int(report_id)).select_related("type")[:3] 
    return{'what_tab': report_types,} 

template.html

{% if leftbar.what_tab.0.type.title%}{{ leftbar.what_tab.0.type.title}}{%endif%} 

我存储在类型列中的整数值REPORTTYPE模型。

我能够独显的第一个项目到template.I不知道如何来显示所有已保存的项目为模板。

需要帮助。

感谢

回答

1

我不知道什么是leftbar,但假设你得到了所有其他的东西吧,

{% for tab in leftbar.what_tab %} 
    {% if tab.type.title %} 
     {{ tab.type.title}} 
    {% endif %} 
    {% ifnotequal forloop.counter leftbar.what_tab %},{% endnotifequal %} 
{% endfor %} 

由于title不能为空,{% if tab.type.title %}不应该是这样的。

+0

karthikr,代码被沃金,是有可能以逗号(,)的第一和第二项分离。我简单把一个逗号在循环后,它示出了第三项,其中不需要后的逗号。 –

+0

刚刚编辑答案 – karthikr

相关问题