简体   繁体   中英

Django Template Foreign Key Check

In model:

class Example
    text

class Share
    example (foreign_key)
    user    (foreign_key)

In view:

def page:
    items = Example.objects.get(user=user)
    user_shares = Example.objects.get(user=user)
    return render(page.html, {'items': items, 'user_shares': user_shares})

In the template I can show items in rows. But for the shared ones I want to put for example additional buttons. How can I use something like {% if item in shares %} in for loop? Or do you have better ideas ?

in the template:

{% for item in items %}
<td>{{item.text}}</td>
{%if item.shares.count > 0 %}
<td><!-- additional buttons here --></td>
{% endif %}
{% endfor %}

you need to modify the ForeignKey in the Example model:

class Share(models.Model):
    example = models.ForeignKey(Example, related_name='shares')
    ...

Hope this helps.

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