Django: how display a list of data associated with a single given by models.ManyToManyField -
i neophyte , maybe it's stupid question thank in advance aid. see in template list of films directed particular director. following files created me far, please add correction in files.
here's 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"
here's urls.py:
from django.conf.urls.defaults import * urlpatterns = patterns('', (r'^film$', 'database.views.film'), )
here's views.py:
from django.shortcuts import render_to_response django.template import requestcontext models import * def film(request): film = film.objects.all() return render_to_response('film.html', { 'film': film, })
here's template.html:
enter code here
you need pass director 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"), )
then filter in view:
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, })
and in template list like:
<ul> {% f in film %} <li>{{ f }}</li> {% endfor %} </ul>
Comments
Post a Comment