社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

Michael King

Michael King 最近创建的主题
Michael King 最近回复了
6 年前
回复了 Michael King 创建的主题 » 不确定如何计算要显示的特定值(python/django)

Documentation

传递给render的上下文变量必须是字典,因此可以在views.py中计算总权重,将该值放入字典中,然后在模板中获取总权重键的值。

例如:

def checkout(request):
    try:
        current_order = Order.objects.filter(owner=1).get(status="pre-place")
    except Order.DoesNotExist:
        return HttpResponse("Your current order is empty<br><a href=\"browse\">Go back</a>")
    else:
        total_weight = 0
        items = OrderDetail.objects.filter(orderID=current_order)
        template_name = 'store/checkout.html'
        order_details = []
        for item in items:
            weight = item.supplyID.weight * item.quantity
            order_details.append((item, weight))
            total_weight +=weight
        return render(request, template_name, {'order_details': order_details, 'current_order': current_order, 'Total Weight' : total_weight})

然后在模板中使用该变量:

<h1>Your current order</h1>
<a href="{% url 'Store:browse' %}">return to selecting supplies</a><br><br>
<table>
    <tr>
        <th>name</th><th>item weight(kg)</th><th>qty</th><th>total weight(kg)</th>
    </tr>
    {% for order_detail, weight in order_details %}
        <tr>
            <td>{{ order_detail.supplyID.name }}</td>
            <td>{{ order_detail.supplyID.weight }}</td>
            <td>{{ order_detail.quantity }}</td>
            <td>{{ weight }}</td>
        </tr>
    {% endfor %}
</table>
<p>The total weight of your order is:</p>
<p>{{Total Weight}}</p>
6 年前
回复了 Michael King 创建的主题 » Django Statics不可访问(数字海洋)[副本]

这是您第一次尝试将网站部署到生产环境中吗?

如果您使用“python manage.py runserver”在本地运行站点,那么只要文件路径匹配,您的设置看起来就可以,但是如果您在生产环境中尝试为静态文件提供服务,则需要以不同的方式为它们提供服务,我个人使用whitenoise从web服务器本身执行此操作。

文档: https://djangobook.com/serving-files-production/