异常值:通用详细信息视图TransactionDetailView必须使用对象pk或slug调用

时间:2018-11-15 18:45:40

标签: django uuid

当我尝试从“交易列表”页面访问“交易详细信息”页面时,会出现此错误。我正在使用UUID(primary = true)作为“交易详细信息”页面的PK。

memberships / models.py

class Transaction(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="user_transaction")
    order_id = models.CharField(max_length=36, blank=True, unique=True, default=uuid.uuid4, primary_key=True, editable=False)
    membership = models.ForeignKey(Membership, on_delete=models.SET_NULL, null=True, blank=True)
    amount = models.DecimalField(max_digits=100, decimal_places=2)
    success = models.BooleanField(default=True)
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)


    def __str__(self):
        return self.order_id

    class Meta:
        ordering = ['-timestamp']

urls.py

...removed imports


urlpatterns = [

    #Generic
    url(r'^django-admin/', admin.site.urls),
    url(r'^admin/', include(wagtailadmin_urls)),
    url(r'^documents/', include(wagtaildocs_urls)),
    url(r'^search/$', search_views.search, name='search'),

    #Authentication
    url(r'^accounts/', include('allauth.urls')),

    #Product
    url(r'^portal/', include(('portal.urls', 'portal'), namespace='portal')),

    #Payment
    url(r'^checkout/', include(('checkout.urls', 'checkout'), namespace='checkout')),

    #Membership
    url(r'^membership/', include(('memberships.urls', 'memberships'), namespace='memberships')),

    #Rating
    url(r'^ratings/', include('star_ratings.urls', namespace='ratings')),

    #Users
    url(r'^users/$', UserListView.as_view(), name='users_list'),
    url(r'^profile/$', userPage, name='userPage'),
    url(r'^users/~redirect/$', UserRedirectView.as_view(), name='redirect'),
    url(r'^users/(?P<username>[\w.@+-]+)/$', UserDetailView.as_view(), name='detail'),
    url(r'^users/(?P<username>[\w.@+-]+)/summary/$', SummaryDetailView.as_view(), name='summary'),
    url(r'^users/~update/$', UserUpdateView.as_view(), name='update'),
    url(r'^users/redirectprofile/$', RedirectProfileView.as_view(), name='redirectprofile'),
    # For anything not caught by a more specific rule above, hand over to
    # Wagtail's page serving mechanism. This should be the last pattern in
    # the list:
    url(r'', include(wagtail_urls)),

    # Alternatively, if you want Wagtail pages to be served from a subpath
    # of your site, rather than the site root:
    #    url(r'^pages/', include(wagtail_urls)),
]




if settings.DEBUG:
    from django.conf.urls.static import static
    from django.contrib.staticfiles.urls import staticfiles_urlpatterns

    # Serve static and media files from development server
    urlpatterns += staticfiles_urlpatterns()
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

memberships / urls.py

from django.urls import path

from .views import (
    MembershipSelectView,
    PaymentView,
    success,
    updateTransactionRecords,
    user_subscriptions_view,
    cancelSubscription,
    TransactionDetailView,
    TransactionListView,
    SubscriptionDetailView,
    SubscriptionListView,
    adminPanel,
    permission_denied
    )

app_name = 'memberships'

urlpatterns = [
    path('', MembershipSelectView.as_view(), name='select'),
    path('payment/', PaymentView, name='payment'),
    path('success/', success, name='purchase_success'),
    path('update-transactions/<subscription_id>/', updateTransactionRecords, name='update-transactions'),
    path('subscription/', user_subscriptions_view, name='user_subscription'),
    path('cancel/', cancelSubscription, name='cancel'),

    #TODO MAKE SURE ONLY SUPERUSERS CAN VIEW THESE PAGES
    path('transactions/', TransactionListView.as_view(), name='transaction_list'),
    path('transaction/<uuid:order_id>/', TransactionDetailView.as_view(), name='transaction_detail'),
    path('subscriptions/', SubscriptionListView.as_view(), name='subscription_list'),
    path('subscription/<slug:id>/', SubscriptionDetailView.as_view(), name='subscription_detail'),
    path('admin/', adminPanel, name='admin_panel'),
    path('permission_denied/', permission_denied, name='permission_denied')
]

memberships / views.py

class TransactionDetailView(DetailView):
    model = Transaction
    slug_field = 'order_id'

memberships / transaction_list.html

      {% if object_list %}

        {% for transaction in object_list %}
        <tr class="text-left">
          <td ><strong><a href="{% url 'memberships:transaction_detail' transaction.order_id %}" class="text-dark">{{ transaction.user }}</a></strong></td>
          <td  class="text-dark">{{ transaction.order_id }}</td>
          <td  class="text-dark ">{{ transaction.timestamp }}</td>
          <td  class="text-dark ">{{ transaction.success }}</td>
          <td  class="text-dark">$ {{ transaction.amount }}</td>
        </tr>
        {% endfor %}

        {% else %}
            <p>There were no transactions found.</p>
    {% endif %}

编辑:我添加了我的主要urls.py和Memberships / urls.py。交易页面位于memberships.urls下。

2 个答案:

答案 0 :(得分:1)

您已经将视图的slug_field参数设置为order_id,但是您尚未告诉它从何处获取该字段的值。正如错误所言,该视图仍期望URL赋予pkslug

事实上,您的order_id是主键,而不是子弹,并且Django已经为您的主键是pk加上了别名。因此,您应该使用pk_url_kwarg替换该属性。

class TransactionDetailView(DetailView):
    model = Transaction
    pk_url_kwarg = 'order_id'

答案 1 :(得分:1)

您需要在视图中更新slug_url_kwarg。例如:

class TransactionDetailView(DetailView):
    model = Transaction
    slug_field = 'order_id'
    slug_url_kwarg = 'order_id'
    ...

由于基于this code,根据视图的属性slug_url_kwarg从kwarg中获取块值。

也请更新网址路径。

网址:

path('transaction/<uuid:order_id>/', TransactionDetailView.as_view(), name='transaction_detail'),