Saturday 15 March 2014

python - django template access to models -



python - django template access to models -

i having issue templating scheme in django, doing film list app larn more django , have declared models following:

class pelicula (models.model): nombre = models.charfield(max_length = 30) genero = models.charfield(max_length = 20) año = models.integerfield() def __str__(self): homecoming self.nombre

then have declared view following

def index(request): pelicula = pelicula.objects.all() homecoming render_to_response("index.html", dict(pelicula = pelicula, usuario = request.user))

so far know, not having problem here

then problem starts on template

<!doctype html> <html> <head> <title>mis peliculas favoritas</title> </head> <body> <h1>mis peliculas favoritas</h1> <ol> {% pelicula in pelicula.object_list %} <li><a href ="#">{{ pelicula.nombre }}</a></li> {% endfor %} </ol> <a href="agregar.html">agregar pelicula nueva</a> </body> </html>

so see, using loop objects in db, have 3, when run server, title shows , other link bellow named "agregar" shows except movies, wondering if problem between "pelicula" name instead of using pelicula used on declaring model, anyway have changed pelicula.object_list , didn't worked anyway

i believe having problem understanding how utilize info have through django template tags

thanks in advance , please forgive grammar not native english language speaker

in view code have pelicula = pelicula.objects.all() creates list of abjects database. send template same name, means have in template iterate on them. seek changing template this:

<h1>mis peliculas favoritas</h1> {% if pelicula %} <ol> {% item in pelicula %} <li><a href ="#">{{ item.nombre }}</a></li> {% endfor %} </ol> {% else %} didn't find any... {% endif %}

part of confusion seems come fact template code , view code separate. template doesn't know view code. context dictionary sets names can used in template , in template names have available utilize ones passed via dictionary. names in view , template here same in case because set them same. aren't same variables though, have same name. in template pelicula list of objects don't need utilize in template loop.

python django templates

No comments:

Post a Comment