Django ListView在每个元素后插入空白<li>

时间:2018-01-10 18:45:16

标签: python django

我是Django的新手,并试图了解为什么或如何减轻额外的<li>并且还没有找到任何东西。这是输出正在做什么的图像。 编辑:我尝试按照下面的建议清理字符串,但仍然没有运气。即使我在浏览Django教程时也会发生这种情况。我知道我可以使用JS或JQuery删除它们,但是就像这样基本,并且知道模型只有2个对象,我想知道它为什么插入这个额外的

< LI>

li elements

以下是我的模板片段:

{% extends "base_templates/stations.html" %}

{% block station_content %}
    <h2>Stations</h2>
    <ul>
        {% for station in object_list %}
            {% if station.name.strip != "" %}
            <li>{{ station.name }}<li>
            {% endif %}
        {% endfor %}
    </ul>
{% endblock %}

编辑:以下是我对此模板的看法

from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.urls import reverse
from django.views import generic
from django.views.generic import ListView
from django.utils import timezone
from .models import Customer, CustomerContact, Station, SubcontractorContact

# Create your views here.

class CustomerView(ListView):
    model = Customer

class StationView(ListView):
    model = Station

3 个答案:

答案 0 :(得分:0)

如果您只是检查它是否为空,则可能无法验证空白区域。在检查字符串是否为空之前修剪字符串,因为“”和“”不相等

尝试:

if(station.name.strip()!="")

答案 1 :(得分:0)

如果要检查空白/无值,则

  

无!=“”(无不等于“”为真)

将始终保持正确,因此在循环中它正在打印

  • 在你的空白子弹中。 正确的检查方法是 {% if station.name%} <li>station.name</li> {% endif%}

    答案 2 :(得分:0)

    好的,非常感谢所有贡献的人......但是,如果我们看看那个精彩的结束<li>,我们就会看到没有/ ...所以再次感谢大家,祝你有美好的一天!

    相关问题