2012-07-27 77 views
0

我正在阅读“开始Django电子商务”一书,我试图在所有页面上显示“去购物车”链接。'str'对象没有属性'会话'

我得到这个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 %} 
+0

你调用'session'的唯一对象是请求,在def menu_hour的第一行。这表明请求是一个字符串。你在哪里调用这个函数? – Lenna 2012-07-27 04:00:18

+0

但我通过“请求”时,我打电话给render_to_response,应该发送请求到我的html页面。没有? – anc1revv 2012-07-27 04:14:35

+0

从哪里调用menu_hour视图?你是手动调用还是从urls.py调用? – Babu 2012-07-27 04:52:04

回答

1

这是因为我没有在我的字典里传递“请求”的模板。

相关问题