0
我一直呆在這裏好幾個小時了,我希望這裏有人能幫助我。 我想比較Django模板中的字符串。 我的觀點是給字符串的兩份名單的模板:Django模板unicode字符串比較
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的tempate現在的列表應該進行比較。
{% 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,它在給定的接口列表中有一個接口。 我已經看過類型。兩者都是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
在此先感謝。
它總是這些小東西,對吧?非常感謝您睜開我的眼睛。 – schorschel