没有slug的基于类的视图 - 使用ID:FieldError

时间:2014-03-08 02:38:53

标签: python django

我试图使用基于类的视图显示模型Transaction的结果。 由于某种原因,系统要求一个'slug'字段,模型没有,它在堆栈跟踪中看起来django中的默认值是slug,我希望它通过对象的id进行查找而不是

FieldError at /oferto/transaction_detail/6/
Cannot resolve keyword u'slug' into field. Choices are: accepted_by, amount, creator, id

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/oferto/transaction_detail/6/

Django Version: 1.5.4
Python Version: 2.7.4
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'django.contrib.admindocs',
 'django.contrib.comments',
 'django.contrib.sitemaps',
 'zinnia',
 'tagging',
 'mptt',
 'south',
 'registration',
 'blogs',
 'turtle',
 'ofertoj',
 'petoj',
 'x',
 'profiles')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "/home/talisman/virt_env/tempilo/local/lib/python2.7/site-packages/Django-1.5.4-py2.7.egg/django/core/handlers/base.py" in get_response
  115.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/talisman/virt_env/tempilo/local/lib/python2.7/site-packages/Django-1.5.4-py2.7.egg/django/views/generic/base.py" in view
  68.             return self.dispatch(request, *args, **kwargs)
File "/home/talisman/virt_env/tempilo/local/lib/python2.7/site-packages/Django-1.5.4-py2.7.egg/django/views/generic/base.py" in dispatch
  86.         return handler(request, *args, **kwargs)
File "/home/talisman/virt_env/tempilo/local/lib/python2.7/site-packages/Django-1.5.4-py2.7.egg/django/views/generic/detail.py" in get
  108.         self.object = self.get_object()
File "/home/talisman/virt_env/tempilo/local/lib/python2.7/site-packages/Django-1.5.4-py2.7.egg/django/views/generic/detail.py" in get_object
  42.             queryset = queryset.filter(**{slug_field: slug})
File "/home/talisman/virt_env/tempilo/local/lib/python2.7/site-packages/Django-1.5.4-py2.7.egg/django/db/models/query.py" in filter
  669.         return self._filter_or_exclude(False, *args, **kwargs)
File "/home/talisman/virt_env/tempilo/local/lib/python2.7/site-packages/Django-1.5.4-py2.7.egg/django/db/models/query.py" in _filter_or_exclude
  687.             clone.query.add_q(Q(*args, **kwargs))
File "/home/talisman/virt_env/tempilo/local/lib/python2.7/site-packages/Django-1.5.4-py2.7.egg/django/db/models/sql/query.py" in add_q
  1271.                             can_reuse=used_aliases, force_having=force_having)
File "/home/talisman/virt_env/tempilo/local/lib/python2.7/site-packages/Django-1.5.4-py2.7.egg/django/db/models/sql/query.py" in add_filter
  1139.                     process_extras=process_extras)
File "/home/talisman/virt_env/tempilo/local/lib/python2.7/site-packages/Django-1.5.4-py2.7.egg/django/db/models/sql/query.py" in setup_joins
  1337.                             "Choices are: %s" % (name, ", ".join(names)))

Exception Type: FieldError at /oferto/transaction_detail/6/
Exception Value: Cannot resolve keyword u'slug' into field. Choices are: accepted_by, amount, creator, id

models.py

class Transaction(models.Model):
    creator = models.IntegerField()
    amount = models.IntegerField()
    accepted_by = models.IntegerField()

    def __unicode__(self):
        return self.id


    def create(self, **kwargs):
        new_transaction = Transaction(
                creator = kwargs['creator'],
                amount = kwargs['amount'],
                accepted_by = kwargs['accepted_by']
            )
        new_transaction.save()
        return

    def get_absolute_url(self):
        return reverse('transaction_detail', kwargs={'id': self.id})

views.py

class TransactionDetailView(DetailView):
    model = Transaction
    template_name = "ofertoj/transaction_detail.html"

1 个答案:

答案 0 :(得分:1)

Pit在网址中 已更改P<slug> to P<pk>

url(
    regex=r"transaction_detail/(?P<pk>[-_\w]+)/$",
    view=TransactionDetailView.as_view(),
    name="transaction_detail"
    ),