I’m running into trouble with related model classes.
I have a model that looks like this:
class Cine(models.Model):
nombre = models.CharField(max_length=150)
ciudad = models.ForeignKey(Ciudad, db_column='ciudad')
slug = models.SlugField(unique=True, blank=True)
...
class Funcion(models.Model):
idpelicula = models.ForeignKey(Pelicula, db_column='idpelicula')
idcine = models.ForeignKey(Cine, db_column='idcine', null=True)
hora = models.TimeField(null=True)
...
My views looks like this:
def FuncionesByCine(request, id):
funcionesByCine = Funcion.objects.filter(idcine=id)
context = {'funcionesByCine': funcionesByCine}
return render_to_response('funciones-by-cine.html', context, context_instance=RequestContext(request))
def CineDetail(request, cineslug):
cine = Cine.objects.get(slug=cineslug)
context = {'cine': cine}
return render_to_response('cine-individual.html', context, context_instance=RequestContext(request))
And the url’s:
#Queryset containing all the Cine objects
cine_info = {
'queryset': Cine.objects.all(),
'template_name': 'cines-all.html',
}
url(r'^complejos/$', list_detail.object_list, cine_info),
url(r'^complejos/(?P<cineslug>.*)/$', views.CineDetail),
url(r'^complejos/(?P<cineslug>.*)/funciones/(?P<id>.*)/$', views.FuncionesByCine),
url(r'^funciones/$', views.FuncionesAll),
url(r'^funciones/(?P<id>.*)/$', views.FuncionesByCine),
This gives me the desired ‘funciones’ associated to a ‘cine’ from Funcion when I call it from the url like this: localhost:8000/funciones/1.
Now, what I want to be able to do is to call this view from a template that displays the individual movie theater(‘cine’) and with this view to be able to displays all the showtimes(‘funciones’) that are on that ‘cine’.
I’m trying to use the same view but a different url:
url(r'^complejos/(?P<cineslug>.*)/funciones/(?P<id>.*)/$', views.FuncionesByCine),
And the template calling looks like this:
{% extends 'cines-menu.html' %}
{% block content %}
<div class="cine">
<p>Nombre: <a href="funciones/{{cine.id}}">{{cine}}</a></p>
<p>Ciudad:{{cine.ciudad}}</p>
<p>Direccion:{{cine.direccion}}</p>
{% endblock %}
This is currently not working. Any ideas?
Thanks!
edit: I have added the other views and urls(also the one that use cineslug)
what specifically is not working? what errors are you receiving?
I see one that you’re attempting to pass
cineslugto your view function and as a parameter.When you used named groups they are passed as positional arguments to the view.
def FuncionesByCine(request, id):shoudl be
def FuncionesByCine(request, cineslug, id):but i do believe this will break your original url,
you could change your function definition to
def FuncionesByCine(request, cineslug, id):and pass in default value for
cineslugurl(r'^funciones/(?P<id>.*)/$', views.FuncionesByCine, {'cineslug': None})You can retrieve
Funcion‘s by slug like:funciones = Funcion.objects.filter(idcine__slug={{ your slug value here }})