繁体   English   中英

Django模板unicode字符串比较

[英]Django template unicode String comparison

我已经在这里停留了几个小时,希望这里有人可以帮助我。 我想比较Django模板中的字符串。 我的观点是为模板提供了两个字符串列表:

latest_app_list = App.objects.all().order_by('name')[:5]
context = {'latest_app_list': latest_app_list}
if not request.body == "":
    xml = ET.fromstring(request.body)
    interfaces = []
    for x in xml.getchildren(): 
        interfaces.append(unicode(x.text, 'utf-8'))                                                       
    context = {'latest_app_list': latest_app_list, 'xml': interfaces}
if("format" in request.GET.iterkeys() and request.GET['format'] == "xml"):
    return render(request, 'appstore/appstore.xml', context, content_type="application/xml")

现在使用appstore.xml进行比较应该比较列表。

 {% if latest_app_list %}
  {% for app in latest_app_list %}
    {% if xml %}
      {% for interface in xml %}
        {% for app_interface in app.interfaces.all %}
          {% ifequal interface app_interface %}
            <app>
              <uri>{{app.ip}}</uri>
              <id>{{  app.id }}</id>
              <name>{{ app.name }}</name>
            </app>
          {% endifequal %}
        {% endfor %}
     {% endfor %}
    {% endif %} 
  {% endfor %}
{% endif %} 

因此,模板应仅显示s,它们在给定的Interface-List中具有接口。 Ich已经了解了类型。 两者都是unicode字符串。 供参考,这是我的models.py:

from django.db import models
class Interface(models.Model):
 title = models.CharField(max_length=150)
 def __unicode__(self):
    return self.title

class App(models.Model):
 id      = models.AutoField(primary_key=True)
 name    = models.CharField(max_length=100)
 description = models.TextField(max_length=3000)
 ip      = models.CharField(max_length=150)
 interfaces  = models.ManyToManyField(Interface)
 file    = models.FileField(upload_to='documents/%Y/%m/%d')                                                    
 def __unicode__(self):
    return self.name

提前致谢。

您似乎并没有将字符串与字符串进行比较。 您正在将字符串与Interface对象进行比较。 也许您是说{% ifequal interface app_interface.title %}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM