0

I playing around with a simple addressbook application and I'd like to show the company of a contact in a DetailView of a contact.

In my template:

<table class="table table-bordered table-condensed" style="width:50%;">
    {% for company in companies %}
    {% if company.name == contact.company %}
    <tr>
      <td>{{ company.name }}</td>
      <td>{{ company.street }}</td>
      <td>{{ company.plz }}</td>
      <td>{{ company.city }}</td>
    </tr>
    {% endif %}
    {% endfor %}
</table>

My view:

class ContactView(DetailView):
    model = Contact
    template_name = 'contact.html'
    def get_context_data(self, **kwargs):
        context = super(DetailView, self).get_context_data(**kwargs)
        # Companies                                                                                                                               
        context['companies'] = Company.objects.all()
        # Return                                                                                                                                    
        return context

In my models:

class Company(models.Model):
    name = models.CharField(max_length=255,)

and

class Contact(models.Model):
    first_name = models.CharField(max_length=255, blank=True, null=True)
    last_name = models.CharField(max_length=255,)
    company = models.ForeignKey(Company, blank=True, null=True)

What is wrong with the if statement in my template?

Thanks for your help in advance!

1 Answer 1

2

You should compare the company itself, not the name.

Change

{% if company.name == contact.company %}

to

{% if company == contact.company %}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.