【问题标题】:Render a dictionary with object list as values in django but template not showing values将带有对象列表的字典作为 django 中的值呈现,但模板不显示值
【发布时间】:2026-01-05 09:15:02
【问题描述】:

我正在尝试在我的模板中显示从我的视图文件中呈现的字典值。但它没有显示任何价值。在我的 html 模板中显示我只是空白或没有任何错误。

views.py

class BuyView(TemplateView):
     template_name = 'shop/buy.html'
     red_templateName = 'shop/receipt.html'

     def get(self, request, product_id):
          product = get_object_or_404(Product, pk=product_id)
          args = {'product':product}
          return render(request, self.template_name, args)

     def post(self, request, product_id):
          product = Product.objects.get(id=product_id)
          if request.POST['address'] and request.POST['quantity']:
               order = Order()
               order.or_proName = product.pro_name
               order.or_companyName = product.companyName
               order.or_quatity = request.POST['quantity']
               order.or_quatity = int( order.or_quatity)
               orderPrice = order.or_quatity*product.Sale_Price
               order.or_bill = 100 + orderPrice
               order.pub_date = timezone.datetime.now()
               product.Quantity -= order.or_quatity
               product.save()
               order = order.save()
               args = {'order':order}
               return render(request, self.red_templateName, args)

          else:
               return render(request, self.template_name, {'error':'Please Fill the address field'})

template.html

{% extends 'base.html' %}
{% block content %}
<div>

  <div class="container p-5" style="border:1px solid gray;">
    <small class="lead" >Your Order Receipt: </small>
        {{ order.or_id }}
        {{order.or_proName}}
        {{order.or_companyName}}
        {{order.or_quatity}}
        {{order.pub_date}}
        Deliever Chargers=100
        -----------------------
        Total = {{or_bill}}
  </div>
</div>

{% endblock %}

【问题讨论】:

  • if request.POST['address'] and request.POST['quantity'] 绝对是真的吗?
  • 是的@RHSmith159。一切正常,但 args = {'order':order} 这不是工作客栈模板

标签: python django python-3.x dictionary view


【解决方案1】:

您的模板文件名应与以下内容匹配

red_templateName = 'shop/receipt.html'

【讨论】:

    最近更新 更多