简体   繁体   中英

Django checking an object's attribute's value

I have two similar models that have type attributes that equals "taxi" or "bus" and I want to check if an object is taxi or bus in my context processor. I tried

if element.type == "bus":

but it returned none. This is because it's not that straight forward, I know. I also tried to transform the element.type to str , it showed the problem but I don't know how to fix.

models.py:

class Element(models.Model):
    user = models.ForeignKey(User, null=True, blank=True)
    name = models.CharField(max_length=20)
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
    type = models.CharField(max_length=20)

user only can add "taxi" or "bus" as strings to type field.

views:

def home(request, id):
    site = Site.objects.get(id=id)
    elements = Element.objects.filter(site_name=site)

    return render_to_response("base.html", locals(), context_instance=RequestContext(request))

base.html:

{% load ttag %}
{% for e in elements %}
{% elements request e.id %}
{% endfor %}

context processor:

def elements(request, id):
    element = Element.objects.get(id=id)
    if element.type == "taxi":
        list = {'element':element}
        return render_to_string("taxi.html", list)
    elif element.type == "bus":
        list = {'element':element}
        return render_to_string("bus.html", list)

How can I check whether it's a bus or taxi? Thanks.

I think it probably is that straightforward and I sense that you're working a lot harder than you need to. This should work:

In view code:

elements = Element.objects.filter(site_name=site)  # or whatever

In template code:

{% for e in elements %}
    {% if e.type == 'taxi' %}
       <!-- do taxi stuff -->
    {% elif e.type == 'bus' %}
       <!-- do bus stuff -->
    {% endif %}
{% endfor %}

If that doesn't do the trick, what's going wrong? Are you certain that all "element" objects have a "type" attribute?

I don't know how you got the idea to use a context processor, but that can't be used as a function within a template. A context processor changes the context within a template before you start to render the template.

An easy solution would be to add a method to Element and include the template dynamically:

class Element(models.Model):
    user = models.ForeignKey(User, null=True, blank=True)
    name = models.CharField(max_length=20)
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
    type = models.CharField(max_length=20)

    def get_template_name(self):
        if self.type == "bus":
            return 'bus.html'
        if self.type == "taxi":
            return 'taxi.html'

In your template:

{% for element in elements %}
    {% include element.get_template_name with element=element %}
{% endfor %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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