NoReverseMatch in Django

After debugging for a while, I found out what happened, but I don't know how to fix it.

  • I have a urlConf named ' ver_caja ' that takes the id of a caja object as an argument and then calls the generic object_detail .
  • The request rule is correct: correctly return all caja objects.
  • In the template, I have a call: {% ver_caja caja.id %}
  • The caja object is correctly accepted by the template.
  • I am using MySQL.

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

This 1L throws an error because urlconf ( ver_caja ) expects an integer rather than the alphanumeric value " <int>L ".

All the information I received on the django docs site is (as an example in the tutorial), and this does not help:

 ... >>> p = Poll(question="What 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 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 get caja.id=1 instead of caja.id=1L ?

Thanks in advance.

Pedro

EDIT: Here you have all the files.

template error:

Rendering exception showing: Reverse for 'ver_caja_chica' with arguments '(1L,)' and keyword Arguments '{}' not found.

Kaha / 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.') 

Kaha / 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 the wrong urlconf (as intended), this is the URL for this application:

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

Perhaps the final URL was created incorrectly by django.

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

Some clues?

+4
source share
2 answers

Did you really hook this URL configuration up to your main URL configuration?

In your urls.py project urls.py make sure you have something like:

 urlpatterns = patterns('', #... url(r'^cajas/', include('caja.urls')), ) 
+8
source

The problem is not at all what you think. The arguments are shown as '(1L,)' , so the value in the tuple is an integer, albeit a long one, and not a string that would be displayed as '('1L',)' . (The explanation for L is shown in the comment on the code you posted).

Actually the problem is that your URL expects a named keyword argument, not an unnamed positional. This is because you called the regex group: (?P<object_id>\d+) . Thus, the URL tag should be:

 {% url ver_caja_chica object_id=obj.pk %} 
+2
source

All Articles