【问题标题】:How can I link my DetailView and my ListView together?如何将 DetailView 和 ListView 链接在一起?
【发布时间】:2016-12-07 07:44:34
【问题描述】:

我正在为当地的珠宝店建立一个演示网站,我正在尝试创建一个珠宝品牌列表 (ListView) 并将每个品牌链接到另一个页面中的描述 (DetailView)。我花了整整 15 个小时试图将我的 ListView 和我的 DetailView 链接在一起,但我没有修复任何东西。这些是我正在使用的视图:

观看次数

class BrandView(ListView):  
    template_name = 'products.html'
    queryset = models.Brand.objects.order_by('brand_name')
    context_object_name = 'brand_list'

对于第一个视图,我创建了一个模板,该模板将查询集中的每个对象显示为指向其相应详细信息页面的链接,该详细信息页面应由下一个视图表示:

class TextView(DetailView):
    template_name = 'brands/brand_text.html'    
    context_object_name = 'brand'

    def get(self, request, slug):
        # Grabs the Brand object that owns the given slug
        brand = models.Brand.objects.get(slug = slug)

        # renders self.template_name with the given context and model object
        return render(request, self.template_name, self.context_object_name)

我也尝试将最后一个视图编写为常规函数,但这也没有完成任何事情:

def text_view(request, slug):
    brand = models.Brand.objects.get(slug = slug)
    return render(request, 'brands/brand_text.html', {'brand': brand,})

基本上,当我从 ListView 中单击一个对象时,该对象的 slug 会添加到 url,但页面不会改变。那么如何才能成功链接我的两个视图,以便 DetailView 获取从 ListView 提供的信息?


也许我的模板可能会很方便:

模板

brand_text.html

{% block content %}
    <div class= "brands" style="animation: fadein 1.5s 1;">
            <p>
                <a class = "nav_link" href="{% url 'products' %}">Back</a>
            </p>
    </div>

    <div class= "brand_descriptions">
        <p>{{ brand.description }}</p>
    </div>

{% endblock %} 

products.html

{% block content %}
    <div class= "brands" style="animation: fadein 1.5s 1;">
        {% for item in brand_list %}
            <p>
                <a class = "nav_link" href="{% url 'brand_text' item.slug %}">{{ item.brand_name }}</a>
            </p>
        {% endfor %}
    </div>

{% endblock %}

2016 年 8 月 2 日更新:

网址格式

url(r'^products/', BrandView.as_view(), name = 'products'),
url(r'^products/(?P<slug>[-\w]+)', TextView.as_view(), name = 'brand_text'),

(这是我的第一个问题,如果太长,我很抱歉!)

【问题讨论】:

  • 你可以把它作为一个建议,看看我会将每个 itteam 存储在列表模型中,并使用它的 id 和相应的 id,我会将它的详细信息存储在每个 itteam 的 DetailsModel 中。所以在发送列表时iteam 的我会发送它的 id 以及我想要显示的任何基本信息。单击该特定iteam 后,我会将与该iteam 对应的ID 传递给detailsView 并获取信息。我将使用Ajax 调用来请求,这样它就不会刷新我的页面。
  • 请显示您的网址格式。
  • @Alasdair 感谢您的提及,我更新了我的帖子,以便显示 url 模式。
  • @Piyush 如何进行 Ajax 调用?

标签: python django listview detailview


【解决方案1】:

您的问题在于您的网址格式。您在正则表达式结束时错过了美元。这意味着/products/my-slug/BrandViewTextView 的正则表达式匹配。将其更改为:

url(r'^products/$', BrandView.as_view(), name = 'products'),
url(r'^products/(?P<slug>[-\w]+)$', TextView.as_view(), name = 'brand_text'),

请注意,您可以将详细视图简化为:

class TextView(DetailView):
    template_name = 'brands/brand_text.html'

您不需要设置context_object_name,因为默认值已经是“品牌”。对于基于通用类的视图,覆盖 get 通常不是一个好主意——您要么丢失要么必须复制视图的大部分功能。

【讨论】:

  • 哇,真不敢相信丢失的美元符号会做到这一点。非常感谢!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-14
  • 1970-01-01
  • 2017-05-08
  • 2012-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多