1

我想通过“anno”订购模板的一些元素,但我不知道如何或在哪里(如果来自视图或模板)。我尝试了 whit |dictsort: 但我认为我错过了一些东西或错了一些东西。请问你能帮帮我吗...???!!!谢谢...

我的模型.py:

class Anno( models.Model ):
    anno = models.CharField( max_length=4 )
    def __unicode__(self):
        return self.anno
    class Meta:
        verbose_name_plural = "Anni"

class Sequel_Film( models.Model ):
    saga = models.CharField( max_length=30 )
    def __unicode__(self):
        return self.saga
    class Meta:
        verbose_name_plural = "Sequels"

class Film( models.Model ):
    titolo = models.CharField( max_length=39 )
    trama = models.TextField( max_length=1000 )
    locandina = models.FilePathField( path="C:/Users/Federico/Google Drive/Umore/apps/Database/static/Film/Locandina", recursive=True )
    copertina = models.FilePathField( path="C:/Users/Federico/Google Drive/Umore/apps/Database/static/Film/Copertina", recursive=True )
    trailer = models.FilePathField( path="C:/Users/Federico/Google Drive/Umore/apps/Database/static/Film/Trailer", recursive=True )
    data_inserimento = models.DateField( null=True, verbose_name="data d'inserimento" )
    anno = models.ForeignKey( Anno )
    durata = models.IntegerField( max_length=5 )
    attori = models.ManyToManyField( Attore )
    registi = models.ManyToManyField( Regista )
    studi = models.ManyToManyField( Studio )
    umori = models.ManyToManyField( Umore )
    generi = models.ManyToManyField( Genere_Film )
    sequels = models.ManyToManyField( Sequel_Film )
    def __unicode__(self):
        return self.titolo + " " + self.trama
    class Meta:
        verbose_name_plural = "Films"

我的意见.py:

def film(request, id):
    film = get_object_or_404( Film, pk=id )
    return render_to_response('Film.html', { 'film': film })

我的模板 Film.html :

  {% for sequel in film.sequels.all %}
    {% for film in sequel.film_set.all %}
    {% endfor %}
  {% endfor %}

但是模板中的标签之间有...

  {% for sequel in film.sequels.all %}
  {% for film in sequel.film_set.all %} #{% for sequel_film in sequel.ordered_film_set %}
  <div class="clear paddingT15">&nbsp;</div>
  <div class="grid_11">
    <div class="grid_9">
      <h3><a class="black hover" href="/Database/Film/{{ film.id }}">{{ film.titolo }}</a></h3>
    </div>
    <div class="grid_2 right">
      <p class="marginB10"><img class="h20 w20 marginT-4" src="stella.jpg"><img class="h20 w20 marginT-4" src="stella.jpg"><img class="h20 w20 marginT-4" src="stella.jpg"><img class="h20 w20 marginT-4" src="stella.jpg"><img class="h20 w20 marginT-4" src="stella.jpg"></p>
    </div>
    <div class="clear">&nbsp;</div>
    <div class="grid_2 paddingR15">
      <a href="/Database/Film/{{ film.id }}"><img class="w120 h165" src="Eagle-eye.jpg" ></a>
    </div>
    <div class="grid_3">
      <p class="marginB10 bold color">{% for umori in film.umori.all %}{{ umori.umore }}<br>{% endfor %}</p>
    </div>
    <div class="grid_3">
      <p class="marginB10 bold color">{% for generi in film.generi.all %}{{ generi.genere }}<br>{% endfor %}</p>
    </div>
    <div class="grid_1">
      <p class="marginB10 bold color">{{ film.anno }}<br>{{ film.durata }} min.</p>
    </div>
    <div class="grid_2 bordoL2 floatR marginR-30">
      <div class="grid_2 Lheight10">
    <p class="color marginL5 marginB10 size12">Visto:<span class="bold">No</span></p>
      </div>
      <div class="grid_2">
    <p class="color marginL5 marginB10 size12">Umore:<span class="bold"></span></p>
      </div>
      <div class="grid_2">
    <p class="color marginL5 marginT-5 size12">Vota: <img class="h10 w10" src="nessuno.jpg"><img class="h10 w10" src="nessuno.jpg"><img class="h10 w10" src="nessuno.jpg"><img class="h10 w10" src="nessuno.jpg"><img class="h10 w10" src="nessuno.jpg"></p>
      </div>
    </div>
    <p class="recensione_a">Iauhc iewohafiuhedpidheas wpid wei eiageiweg wiieadfef iuadgqef iupi hi eri haiu reiugsi4fri sireehre efi iviviu rier ef uviu li elu riwf iuh ger iugeiu Iauhc iewohafiuhedpidheas wpid wei eiageiweg wiieadfef iuadgqef iupi hi eri haiu reiugsi4fri sireehre efi iviviu rier ef uviu li elu riwf iuh ger iugeiu</p>
    <div class="clear">&nbsp;</div>
  </div>
  <div class="clear paddingT15">&nbsp;</div>
  {% endfor %}
  {% endfor %}

我必须如何使用 {{sequel_film}} ??? 对不起,但我不明白...

4

1 回答 1

2

You can add a helper to your model:

class Sequel_Film( models.Model ):
    ...

    def ordered_film_set(self):
        return self.film_set.all().order_by('anno__anno')

In template:

{% for sequel in film.sequels.all %}
    {% for sequel_film in sequel.ordered_film_set %}
        {{sequel_film}}
    {% endfor %}
{% endfor %}
于 2013-06-13T14:11:27.487 回答