Friday, May 10, 2013

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



On Friday, 10 May 2013 10:29:21 UTC+1, Federico Erbea wrote:

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 %}

I'm not quite sure what you're trying to do in the Attore view. You get an actor, but then iterate through all actors in all films that that actor is in.

If you just want to show the details for a single actor, why iterate at all? Just get the actor as you're doing with `get_attore_id` (which by the way is a strange name, because `get_` implies it's a function, which it isn't, and you're not getting the ID, you're getting the Attore object: it should probaby just be called `attore`.), and don't bother with the film. And remove the for loops from the template, and just output the details of the single actor.
--
DR.

--
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