Django 1.6:'instancemethod'对象没有属性'__getitem__'

时间:2014-06-19 17:28:51

标签: python django templates session

我的模板中有一个下拉菜单,我试图存储用户所做的选择以过滤医生对象。但我一直收到这个错误:

Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Library/Python/2.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
  57.         return view_func(*args, **kwargs)
File "views.py" in doclistings
  91.     if request.session.get["selection"] == "Dentist":

Exception Type: TypeError at /doclistings/
Exception Value: 'instancemethod' object has no attribute '__getitem__'

这是索引模板

<div class="signup">
          <div class="form-group">
            <form action="/doclistings/" method="get" >
            <select class="form-control" id="selection" name="selection">
              <option><b>Find a Doctor...</b></option>
              {% for value, text in form.selection.field.choices %}
                <option value="{{ value }}">{{ text }}</option>
              {% endfor %}
              {% csrf_token %}
            </select>

这里是我要尝试根据选择进行过滤的文档列表视图

def doclistings(request):
    d = getVariables(request)
    doctors = Doctor.objects.all().order_by('-likes')
    paginator = Paginator(doctors, 20) #Show 20 doctors per page
    page =  page = request.GET.get('page')
    # print request.session["selection"]
    # print request.session.get("selection", None)

    if request.session.get["selection"] == "Dentist":
        doctors = Doctor.objects.filter(specialization = "Dentist").order_by('-likes')

    try:
        doctors = paginator.page(page)
    except PageNotAnInteger:
        doctors = paginator.page(1)
    except EmptyPage:
        doctors = paginator.page(paginator.num_pages)
    d['doctors'] = doctors
    d['paginator'] = paginator

    return render_to_response('meddy1/doclistings.html',d)

医生模型具有专门化属性,我试图根据用户选择过滤医生。

1 个答案:

答案 0 :(得分:4)

get是一种方法

request.session.get('selection')