python - “str”对象没有属性 'session'

标签 python django attributes e-commerce

我正在阅读“Django 电子商务入门”一书,我一直在尝试在所有页面上显示“转到购物车”链接。

我收到此错误:'str'对象没有属性'session'和行

HTML 中的

“{% cart_box request%}” 会突出显示此错误。

这是 HTML

{% load catalog_tags %}

<div class="cart_box">

    {% cart_box request %}

</div>

{% if hour == 7 or hour == 9 or hour == 12 %}   

Order time = {{hour}}

这就是 View 。

def menu_hour(request,hour):

    #set the test cookie
    request.session.set_test_cookie()

    hour = int(hour)
    food = Food.objects.all()
    output = ', '.join([f.name for f in food])

    steak = Food.objects.get(name="Steak and Egg Burrito")
    steak.price = 15
    steak.save()

    queso = Food.objects.get(name="Queso Burrito")
    queso.time = hour

    food_dict = {"steak": steak, "queso": queso, "hour":hour}
    return render_to_response('menu_hour.html', food_dict, context_instance=RequestContext(request))

这是目录标签

from django import template 
from cart import cart

register = template.Library()

@register.inclusion_tag("tags/cart_box.html") 
def cart_box(request):
    cart_item_count = cart.cart_distinct_item_count(request) 
    return {'cart_item_count': cart_item_count }

这是目录标签 HTML

{% with cart_item_count as cart_count %} 
    <a href="{% url show_cart %}">
        Shopping Cart
        <br />
        {{ cart_count }} Item{{ cart_count|pluralize }}
    </a>
{% endwith %}

最佳答案

这是因为我没有将字典中的“请求”传递给模板。

关于python - “str”对象没有属性 'session',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11681216/

相关文章:

python Pandas : TypeError: unsupported operand type(s) for +: 'datetime.time' and 'Timedelta'

python - SSL 错误 - 证书验证失败 - AWS IOT (basicPubSub.py)

python - Django 刷新数据库直到事务结束

python - 如何使用 Python 的 zipfile 模块对 ZIP 文件中的文件设置权限(属性)?

python - 如何从 numpy ndarray 创建 numpy ndarray?

python - 自定义和预训练模型的集合给出了运行时错误

css - 我的 Django 元素没有加载 Bootstrap 字形

Django 类型提示

swift - 只请求一个实体的一个属性

python - 从 lxml 中选择属性值