如何 link 我的 DetailView 和我的 ListView 在一起?
How can I link my DetailView and my ListView together?
我正在为当地一家珠宝店建立一个演示网站,我正在尝试创建一个珠宝品牌列表 (ListView) 并 link 每个品牌在另一个页面中的描述 (DetailView) .我花了整整 15 个小时试图 link 我的 ListView 和我的 DetailView 在一起,但我没有修复任何东西。这些是我正在使用的视图:
观看次数
class BrandView(ListView):
template_name = 'products.html'
queryset = models.Brand.objects.order_by('brand_name')
context_object_name = 'brand_list'
对于第一个视图,我创建了一个模板,将查询集中的每个对象显示为 link 到其对应的详细信息页面,该页面应由下一个视图表示:
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,但页面没有改变。那么我怎样才能成功 link 我的两个视图,以便 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 模式
url(r'^products/', BrandView.as_view(), name = 'products'),
url(r'^products/(?P<slug>[-\w]+)', TextView.as_view(), name = 'brand_text'),
(这是我的第一个问题,如果太长请见谅!)
您的问题出在您的 url 模式中。您错过了正则表达式末尾的美元。这意味着 /products/my-slug/
与 BrandView
或 TextView
的正则表达式匹配。将其更改为:
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
,因为默认值已经是 'brand'。对于基于 class 的通用视图覆盖 get
通常不是一个好主意 - 您要么丢失要么必须复制视图的大部分功能。
我正在为当地一家珠宝店建立一个演示网站,我正在尝试创建一个珠宝品牌列表 (ListView) 并 link 每个品牌在另一个页面中的描述 (DetailView) .我花了整整 15 个小时试图 link 我的 ListView 和我的 DetailView 在一起,但我没有修复任何东西。这些是我正在使用的视图:
观看次数
class BrandView(ListView):
template_name = 'products.html'
queryset = models.Brand.objects.order_by('brand_name')
context_object_name = 'brand_list'
对于第一个视图,我创建了一个模板,将查询集中的每个对象显示为 link 到其对应的详细信息页面,该页面应由下一个视图表示:
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,但页面没有改变。那么我怎样才能成功 link 我的两个视图,以便 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 模式
url(r'^products/', BrandView.as_view(), name = 'products'),
url(r'^products/(?P<slug>[-\w]+)', TextView.as_view(), name = 'brand_text'),
(这是我的第一个问题,如果太长请见谅!)
您的问题出在您的 url 模式中。您错过了正则表达式末尾的美元。这意味着 /products/my-slug/
与 BrandView
或 TextView
的正则表达式匹配。将其更改为:
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
,因为默认值已经是 'brand'。对于基于 class 的通用视图覆盖 get
通常不是一个好主意 - 您要么丢失要么必须复制视图的大部分功能。