Django Reverse Query in Template

前端 未结 2 1591
感情败类
感情败类 2020-12-08 11:28

I have models like this

class Blog(models.Model):
    name = models.CharField(max_length=100)
    tagline = models.TextField()

    def __unicode__(self):
           


        
相关标签:
2条回答
  • 2020-12-08 11:53

    To access blog entries (Related Manager): blog.entry_set.all

    To do other actions if blog have no entries, you have the {% empty %} tag that is executed when the set is empty.

    {% block content %}
         {% for blog in blog_list %}
              {{ blog.tagline }}
              {% for entry in blog.entry_set.all %}
                  {{entry.name}}
              {% empty %}
                 <!-- no entries -->
              {% endfor %}
         {% endfor %}
    {% endblock %}
    
    0 讨论(0)
  • 2020-12-08 11:58

    based on your code you could do the following.

    {% block content %}
         {% for blog in blog_list %}
              {{ blog.tagline }}
              {% for entry in blog.entry_set.all %}
                  {{entry.name}}
              {% endfor %}
         {% endfor %}
    {% endblock %}
    
    0 讨论(0)
提交回复
热议问题