0
我是新手,也許這是一個愚蠢的問題,所以先謝謝你提前援助。 我想在模板中看到由某位導演執導的電影列表。以下文件是我迄今爲止創建的,請在文件中添加更正。Django:如何顯示與模型給出的單個關聯的數據列表.ManyToManyField
這裏的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, })
這裏的template.html:
enter code here