1

我正在浏览“beginning Django ecommerce”一书,我一直试图在所有页面上显示“go to cart”链接。

我明白了这个error: 'str' object has no attribute 'session'和这条线

"{% cart_box request%}"在 HTML 中突出显示此错误。

这是 HTML

{% load catalog_tags %}

<div class="cart_box">

    {% cart_box request %}

</div>

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

Order time = {{hour}}

这是视图。

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 %}
4

1 回答 1

3

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

于 2012-07-27T13:18:19.913 回答