Q:

Django adding 0 to pk

I am experiencing strange behavior on a Django project I am working on. I have a table of items an I am using the item id in my url. I am creating the item url using get_absolute_url, however, the urls are appearing with a '0' appended to them for example http://127.0.0.1:8000/item/10/ where the url should be http://127.0.0.1:8000/item/1/ and http://127.0.0.1:8000/item/20/ instead of http://127.0.0.1:8000/item/2/
My model is as follows:
class Item(TimeStampedModel):
    name = models.CharField(_('name'), max_length=254)
    slug = AutoSlugField(populate_from='name')
    image = models.ImageField(_('image'), upload_to='items/')
    price = models.IntegerField(_('price per unit'))
    unit_type = models.IntegerField(_('unit type'), choices=UNIT_CHOICIES, default=1)
    unit_increment = models.DecimalField(_('unit increment'), max_digits=4, decimal_places=2)
    unit_min = models.DecimalField(_('unit minimum'), max_digits=4, decimal_places=2)
    unit_max = models.DecimalField(_('unit maximum'), max_digits=4, decimal_places=2)
    quantity = models.DecimalField(_('quantity'), max_digits=4, decimal_places=2, default=0.00)

    class Meta:
        verbose_name = _('item')
        verbose_name_plural = _('items')
        ordering = ('name',)

    def __unicode__(self):
        return self.name

    @models.permalink
    def get_absolute_url(self):
        return('detailed_item', (), {'pk': self.pk})

My view:
class DetailedItemView(DetailView):

    model = Item
    pk_url_kwarg = 'pk'
    template_name = 'item/item_detail.html'
    context_object_name = 'item'

and my Urls:
urlpatterns = patterns('',
    url(r'^(?P<pk>)\d+/$', DetailedItemView.as_view(), name='detailed_item'),
)

in the template I am using:
<a class="btn btn-default" href="{{ item.get_absolute_url }}">Add &raquo;</a></p>

The Django version is 1.7.1 python 2.7.6

A:

It's your URL pattern:
url(r'^(?P<pk>)\d+/$', DetailedItemView.as_view(), name='detailed_item'),

The \d is outside the ) group, resulting in an extra number. Change to
url(r'^(?P<pk>\d+)/$', DetailedItemView.as_view(), name='detailed_item'),

Django tutorial article on the URL dispatcher, and relevant section.

