Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NoReverseMatch in django

After debugging for a while I found what the error was, but I don't know how to fix it.

  • I have an urlConf whit the name 'ver_caja' who receives as argument the id of a caja object, and then call the generic object_detail.
  • The queryset is correct: get all the caja objects correctly.
  • In the template I have the call: {% ver_caja caja.id %}
  • The object caja is correctly received by the template.
  • I'm using MySQL.

The issue is that caja.id has value "1L" instead of "1".

This 1L rises the error because the urlconf (ver_caja) waits for an integer not a alphanumeric '<int>L'.

All the info I got in django docs site is this (as an example in a tutorial), and it doesn't help:

...

>>> p = Poll(question="What's up?", pub_date=datetime.datetime.now())

# Save the object into the database. You have to call save() explicitly.
>>> p.save() 

# Now it has an ID. Note that this might say "1L" instead of "1", depending 
# on which database you're using. That's no biggie; it just means your
# database backend prefers to return integers as Python long integer
# objects.
>>> p.id

...

So, how could I fix this to receive caja.id=1 instead of caja.id=1L?

Thanks in advance.

Pedro

EDIT: Here you have all the files.

template error:

Caught an exception while rendering: Reverse for 'ver_caja_chica' with arguments '(1L,)' and keyword arguments '{}' not found.

caja/models.py

class Caja(models.Model):
    slug = models.SlugField(blank=True)
    nombre = models.CharField(max_length=20)
    saldo = models.DecimalField(max_digits=10, decimal_places=2)
    detalle = models.TextField(blank=True, null=True)

    # apertura
    fechahora_apert = models.DateTimeField(default=datetime.datetime.now, auto_now_add=True)
    usuario_apert = models.ForeignKey(Usuario, related_name=u'caja_abierta_por', help_text=u'Usuario que realizó la apertura de la caja.')

    # cierre
    fechahora_cie = models.DateTimeField(blank=True, null=True)
    usuario_cie = models.ForeignKey(Usuario, null=True, blank=True, related_name=u'caja_cerrada_por', help_text=u'Usuario que realizó el cierre de la caja.')

    def __unicode__(self):
        return u'%s,  $%s' % (self.nombre, self.saldo)

    class Meta:
        ordering = ['fechahora_apert']


class CajaChica(Caja):
    dia_caja = models.DateField(default=datetime.date.today, help_text=u'Día al que corresponde esta caja.')
    cerrada = models.BooleanField(default=False, help_text=u'Si la caja está cerrada no se puede editar.')

caja/urls.py

cajas_chicas = {
    'queryset': CajaChica.objects.all(),
}

urlpatterns = patterns('',
    url(r'^$', 'django.views.generic.list_detail.object_list', dict(cajas_chicas, paginate_by=30), name="lista_cajas_chicas"),
    url(r'^(?P<object_id>\d+)/$', 'django.views.generic.list_detail.object_detail', dict(cajas_chicas, ), name="ver_caja_chica"),
)

cajachica_list.html

...
<table>
{% for obj in object_list %}
<tr class="{% cycle 'row1' 'row2' %}">
    <td>{{ obj.nombre|capfirst }}</td>
    <td>{{ obj.fechahora_apert|timesince }}</td>
    <td>{{ obj.usuario_apert }}</td>
    <td>{{ obj.saldo }}</td>
    <td><a href="{% url ver_caja_chica obj.pk %}">Ver / Editar</a></td>
</tr>
{% endfor %}
</table>
...

EDIT-2 With a wrong urlconf (at purpose), these are the urls for this app:

... 
4. ^caja/$ ^$ 
5. ^caja/$ ^(?P<object_id>\d+)/$ 
... 

Maybe the final url is been constructed wrong by django.

These urls are inside caja/urls.py and are included by urls.py from the root directory of the project.

Some clue?

like image 310
pmourelle Avatar asked Dec 30 '22 10:12

pmourelle


1 Answers

Are you sure you have actually connected this URL configuration up to your primary URL configuration?

In your project's urls.py, ensure you have something like:

urlpatterns = patterns('',
    #...
    url(r'^cajas/', include('caja.urls')),
)
like image 111
SmileyChris Avatar answered Jan 05 '23 11:01

SmileyChris