views:

336

answers:

2

Hi. 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?

+1  A: 

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

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

{% url ver_caja_chica object_id=obj.pk %}
Daniel Roseman
Acutally, that doesn't matter. You can still use positional arguments for named regex groups.
SmileyChris
@SmileyChris—What if the order of arguments don't line up with what the URLConf is expecting? I've never been able to make that work. Pass positional arguments where named arguments are expected. It's the safest, most reliable thing to do.
jcdyer
@Daniel Roseman -- Smiley is right about that isn't the problem. I made the change you proposed, and the error is still there:"Caught an exception while rendering: Reverse for 'ver_caja_chica' with arguments '()' and keyword arguments '{'object_id': 1L}' not found." :SThe mistery keeps unresolved.
pedromagnus
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?
pedromagnus
Problem solved (The url includer had a '$' at the end of the pattern). Thanks Daniel anyway for the explanation about '1L', I didn't know about that. Regards
pedromagnus
@jcd -- while it is better practice (due to that edge case), it's usually not a problem. The way Daniel had answered the question asserted that using positional arguments is impossible "because [the] URL is expecting a named keyword argument"; a false assertion. But all's good that ends good, right pedro? :)
SmileyChris
+1  A: 

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')),
)
SmileyChris
Man, this error could be easily the newbie and most stupid error in the history of django. Spended several days trying to fix this ##$@!!! issue.All of this because a f*****ing '$'!!!!!My urlconf in the root directory was: (r'^cajas/$', include('ceom.cajas.urls')),I never realised of the '$' that remained from before I made the model 'cajas'. I feel like an idiot.Thanks.
pedromagnus