django DetailView如何显示详细信息页面

时间:2018-08-02 14:15:57

标签: django

我想显示添加的产品的详细信息。

我点击了此链接,我可以创建一个添加产品页面

https://simpleisbetterthancomplex.com/tutorial/2018/01/29/how-to-implement-dependent-or-chained-dropdown-list-with-django.html

一旦添加了产品,我想在新页面中显示添加的产品的详细信息。

所以我尝试了以下操作:

class ProductDetailView(DetailView):
    model = Product
    context_object_name = 'product'
    queryset = Product.objects.filter(pk)

我真的不知道要在过滤器中放入什么,或者使用过滤器是正确的。我曾尝试使用Latest and Order by而不是filter,但是它们不起作用。

我的urls.py如下:

urlpatterns = [
    path('', views.ProductDetailView.as_view(), name='product_changelist'),
    #path('', views.ProductListView.as_view(), name='product_changelist'),
    path('add/', views.ProductCreateView.as_view(), name='product_add'),
    path('<int:pk>/', views.ProductUpdateView.as_view(), name='product_change'),
    path('ajax/load-subcategory/', views.load_subcategory, name='ajax_load_subcategory'),
    #path('<int:product_id>', views.detail, name='detail'),

]

当前我遇到错误

AttributeError at /product/
Generic detail view ProductDetailView must be called with either an object pk or a slug.

我读到我们必须在urls.py中提供pk,因此我尝试按以下方式提供pk:

path('<int:pk>', views.ProductDetailView.as_view(), name='product_changelist'),

但随后出现如下错误:

NoReverseMatch at /product/add/
Reverse for 'product_changelist' with no arguments not found. 1 pattern(s) tried: ['product\\/(?P<pk>[0-9]+)$']

我们非常感谢您为解决此问题提供的任何帮助。我是django的新手,所以上面可能犯了很多错误。

编辑1:

我尝试了@Radico给出的建议,但仍然存在相同的错误:我所做的如下:

将我的ProductDetailView更改如下:

class ProductDetailView(DetailView):
    model = Product
    context_object_name = 'product'

product_list.html现在具有以下内容

{% extends 'base.html' %}

{% block content %}
  <br />
  <br />
  <br />
  <br />
 <br />

This is the page getting displayed121

{% url 'product_changelist' pk=object.pk %}

{% endblock %}

仍然出现与

相同的错误
AttributeError at /product/
Generic detail view ProductDetailView must be called with either an object pk or a slug

我没有更改urls.py中的任何内容。...我也必须更改其中的任何内容吗?

编辑2:

这是我的urls.py

from django.urls import include, path
from . import views

urlpatterns = [

    path('<int:pk>', views.ProductDetailView.as_view(), name='product_changelist'),
    #path('', views.ProductDetailView.as_view(), name='product_changelist'),
    #path('', views.ProductListView.as_view(), name='product_changelist'),
    path('add/', views.ProductCreateView.as_view(), name='product_add'),
    path('', views.ProductUpdateView.as_view(), name='product_change'),
    path('ajax/load-subcategory/', views.load_subcategory, name='ajax_load_subcategory'),
    #path('<int:product_id>', views.detail, name='detail'),
]

我的views.py如下:

from django.shortcuts import render
from django.urls import reverse_lazy
from django.views.generic import CreateView, UpdateView, ListView, DetailView

from category.models import Subcategory
from product.forms import ProductForm
from product.models import Product

class ProductListView(ListView):
    model = Product
    context_object_name = 'product'
    queryset = Product.objects.filter()

class ProductDetailView(DetailView):
    model = Product
    context_object_name = 'product'
   # queryset = Product.objects.filter()


# class ProductDetailView(DetailView):
#     template_name = 'product/product_list.html'
#     #model = User
#         #context_object_name = 'foo'
#
#     def get_object(self):
#         #return get_object_or_404(Product, pk=request.session['product_id'])
#         return get_object_or_404(Product, pk=self.request.

class ProductCreateView(CreateView):
    model = Product
    form_class = ProductForm
    success_url = reverse_lazy('product_changelist')

def productlist(request, product_id):
    product = Product.objects.get(productid=product_id)
    return render(request, 'product/product_list.html', {'product': product})

# def productlist(request):
#     prodlist = Product.objects.order_by('-pk')[0]
#     return render(request, 'product/product_list.html', {'prodlist': prodlist})

class ProductUpdateView(UpdateView):
    model = Product
    form_class = ProductForm
    success_url = reverse_lazy('product_changelist')

def load_subcategory(request):
    category_id = request.GET.get('category')
    subcategory = Subcategory.objects.filter(category_id=category_id).order_by('name')
    return render(request, 'product/subcategory_dropdown_list_options.html', {'subcategory': subcategory})

我的product_list.html就像我之前粘贴的

{% extends 'base.html' %}

{% block content %}
<br />
<br />
<br />
<br /><br />

This is the page getting displayed121

{% url 'product_changelist' pk=object.pk %}

{% endblock %}

1 个答案:

答案 0 :(得分:0)

首先:您不需要在detailview对象中已经获得查询集,可以在模板object中调用它,也可以在大小写{{1}中使用小写的类名}。 第二:确保您在网址中传递pk,请参考模板中的product,即detailview 我相信您在第二部分检查{% url 'product_change' pk=object.pk %}模板中的网址时遇到了错误,并且您可能忘记了传递product_changelist