Django LFS - 自定义视图
对于所有闪电快店用户。我正在尝试实现我自己的首页视图,该视图将列出商店中的所有产品(在“/”地址下)。所以我有一个模板:
{% extends "lfs/shop/shop_base.html" %}
{% block content %}
<div id="najnowsze_produkty">
<ul>
{% for obj in objects %}
<li>
{{ obj.name }}
</li>
{% endfor %}
</ul>
</div>
{% endblock %}
然后我编辑了主商店视图:
from lfs.catalog.models import Category
from lfs.catalog.models import Product
def shop_view(request, template_name="lfs/shop/shop.html"):
products = Product.objects.all()
shop = lfs_get_object_or_404(Shop, pk=1)
return render_to_response(template_name, RequestContext(request, {
"shop" : shop, "products" : products
}))
但它什么也没显示。当我在 shell 中执行 Product.objects.all() 查询时,我得到结果。有什么想法可能导致问题吗?也许我应该只过滤具有“活动”状态的产品?但我不确定它是否能以任何方式影响所有物体。
For all those ligthning fast shop users. I'm trying to implement my own first page view that will list all products from shop ( under '/' address). So I have a template :
{% extends "lfs/shop/shop_base.html" %}
{% block content %}
<div id="najnowsze_produkty">
<ul>
{% for obj in objects %}
<li>
{{ obj.name }}
</li>
{% endfor %}
</ul>
</div>
{% endblock %}
and then I've edited main shop view :
from lfs.catalog.models import Category
from lfs.catalog.models import Product
def shop_view(request, template_name="lfs/shop/shop.html"):
products = Product.objects.all()
shop = lfs_get_object_or_404(Shop, pk=1)
return render_to_response(template_name, RequestContext(request, {
"shop" : shop, "products" : products
}))
but it just shows nothing. When I do Product.objects.all() query in shell I get results. Any ideas what could cause the problem ? Maybe I should filter products with 'active' status only ? But I'm not sure if it can influence all objects in any way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题似乎是您在视图代码中调用上下文变量
products
,然后在模板中将其引用为objects
。将它们修复为引用相同的名称,然后您就可以开始了。Your problem seems to be that you're calling the context variable
products
in your view code, then referring to it asobjects
in your template. Fix them to reference the same name, and you should be good to go.