我正在阅读“开始Django电子商务”一书,我一直试图在所有页面上显示“去购物车”链接。
我得到了这个error: 'str' object has no attribute 'session'
和行
"{% 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}}
这是观点。
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 %}
答案 0 :(得分:3)
这是因为我没有将字典中的“请求”传递给模板。