0

我是新手,也许这是一个愚蠢的问题,所以提前感谢您的帮助。我想在模板中看到由特定导演执导的电影列表。以下文件是我目前创建的,请在文件中添加更正。

这是models.py:

from django.db import models

class Genere_Film( models.Model ):
    genere = models.CharField( max_length=25 )
    def __unicode__(self):
        return self.genere
    class Meta:
        verbose_name_plural = "Generi_Film"

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

class Durata( models.Model ):
    durata = models.CharField( max_length=5 )
    def __unicode__(self):
        return  self.durata
    class Meta:
        verbose_name_plural = "Durata"

class Attore( models.Model ):
    nome = models.CharField( max_length=30 )
    cognome = models.CharField( max_length=30 )
    foto = models.CharField( max_length=100 )
    data_inserimento = models.DateField( null=True, verbose_name="data d'inserimento" )
    def __unicode__(self):
        return self.nome + " " + self.cognome + " " + self.foto
    class Meta:
        verbose_name_plural = "Attori"

class Regista( models.Model ):
    nome = models.CharField( max_length=30 )
    cognome = models.CharField( max_length=30 )
    foto = models.CharField( max_length=100 )
    data_inserimento = models.DateField( null=True, verbose_name="data d'inserimento" )
    def __unicode__(self):
        return self.nome + " " + self.cognome + " " + self.foto
    class Meta:
        verbose_name_plural = "Registi"

class Studio( models.Model ):
    nome = models.CharField( max_length=30 )
    foto = models.CharField( max_length=100 )
    data_inserimento = models.DateField( null=True, verbose_name="data d'inserimento" )
    def __unicode__(self):
        return self.nome #+ " " + self.foto
    class Meta:
        verbose_name_plural = "Studi"

class Trailer( models.Model ):
   trailer = models.CharField( max_length=100 )
   data_inserimento = models.DateField( null=True, verbose_name="data d'inserimento" )
   def __unicode__(self):
        return self.trailer
        class Meta:
            verbose_name_plural = "Trailers"

class Film( models.Model ):
    titolo = models.CharField( max_length=39 )
    trama = models.CharField( max_length=1000 )
    locandina = models.CharField( max_length=100 )
    copertina = models.CharField( max_length=100 )
    trailer = models.ForeignKey( Trailer )
    data_inserimento = models.DateField( null=True, verbose_name="data d'inserimento" )
    anno = models.ForeignKey( Anno )
    durata = models.ForeignKey( Durata )
    attori = models.ManyToManyField( Attore )
    registi = models.ManyToManyField( Regista )
    studi = models.ManyToManyField( Studio )
    generi = models.ManyToManyField( Genere_Film )
    def __unicode__(self):
        return self.titolo + " " + self.trama + " " + self.locandina + " " + self.copertina
    class Meta:
        verbose_name_plural = "Film"

这是 urls.py:

from django.conf.urls.defaults import *

urlpatterns = patterns('',    
    (r'^Film$', 'Database.views.film'),
)

这是views.py:

from django.shortcuts import render_to_response
from django.template import RequestContext
from models import *

def film(request):
    film = Film.objects.all()
    return render_to_response('Film.html', { 'film': film, })

这是模板.html:

enter code here
4

1 回答 1

0

您需要将导演 ID 传递给 url:

urlpatterns = patterns('',    
    url(r'^film/$', 'Database.views.film'),
    url(r'^film/director/(?P<director_id>\d+)/$', 'Database.views.film', name="films_by_director"),
)

然后在视图中按它过滤:

def film(request, director_id=None):
    film = Film.objects
    if director_id:
        director = get_object_or_404(Regista, id=director_id)
        film = film.filter(registi=director)
    else:
        film = film.all()
    return render_to_response('Film.html', { 'film': film, })

在模板列表中,例如:

<ul>
{% for f in film %}
    <li>{{ f }}</li>
{% endfor %}
</ul>
于 2013-04-18T15:26:59.007 回答