Friday, May 10, 2013

how make a template's link using get_object_or_404 by pk=id

Hello everyone thanks in advance on the aid, I'm new in programming in general. I would like to create a link (in Film.html) and using the ID to open a new page (Attore.html) where to load only the data associated with that ID but it doesn't do that since it loads all data of attori instead of only one but I don't understand where it's the error.

Here is the code a little simplified.

models.py :

from django.db import models    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 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 )      data_inserimento = models.DateField( null=True, verbose_name="data d'inserimento" )      attori = models.ManyToManyField( Attore )      def __unicode__(self):          return self.titolo + " " + self.trama + " " + self.locandina + " " + self.copertina      class Meta:          verbose_name_plural = "Film"

views.py :

from django.shortcuts import render_to_response, get_object_or_404  from django.template import RequestContext  from models import *    def film(request):      film = Film.objects.order_by("titolo")      return render_to_response('Film.html', { 'film': film, })    def film_attore(request, id):      get_attore_id = get_object_or_404( Attore, pk=id )      return render_to_response('Attore.html', { 'film': Film.objects.filter( attori=get_attore_id ), 'attor': get_attore_id })

urls.py

from django.conf.urls.defaults import *    urlpatterns = patterns('',          (r'^Film$', 'Database.views.film'),      (r'^Attore/(\d+)/$', 'Database.views.film_attore'),  )

Template:

Film.html :

{% extends "Base.html" %}    {% block titolo %}Film{% endblock %}    {% block contenuto %}    {% for dato in film %}      {% for attore in dato.attori.all %}        <a href="/Database/Attore/{{ attore.id }}">{{ attore.nome }} {{ attore.cognome }}</a>      {% endfor %}    {% endfor %}  {% endblock %}

Attore.html :

{% extends "Base.html" %}    {% block titolo %}Attore{% endblock %}    {% block contenuto %}    {% for dato in film %}      {% for attore in dato.attori.all %}        <h2>{{ attore.nome }} {{ attore.cognome }}</h2>      {% endfor %}    {% endfor %}  {% endblock %}

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate