Django 'dict' 对象没有属性 'getlist'

Django 'dict' object has no attribute 'getlist'

this 博客 post 的启发,我试图通过将搜索参数保存到会话中来制作一个处理配置文件搜索的视图,以便可以通过分页保留查询。

观点如下:

def profile_search(request):
    args = {}
    qs=[]
    if not request.method == 'POST':        
        if 'search-profiles-post' in request.session: 
            request.POST = request.session['search-profiles-post'] 
            request.method = 'POST' 


    if request.method == "POST":
        form = AdvancedSearchForm(request.POST)
        request.session['search-profiles-post'] = request.POST


        if form.is_valid():
            cd = form.cleaned_data

            s_country=cd['country']
            s_province=cd['province']
            s_city = cd['city']

        if s_city: qs.append( Q(city__in=s_city))
            if s_country: qs.append(Q(country__icontains = s_country))
            if s_province: qs.append( Q(province__icontains=s_province))    



            f = None
            for q in qs:
                if f is None: 
                    f=q                                  
                else: f &=q
            print f

            if f is not None:
                profiles = UserProfile.objects.filter(f).order_by('-created_at') 

        else:
            form = AdvancedSearchForm()
            profiles = UserProfile.objects.all().order_by('-created_at') 

    paginator = Paginator(profiles,12)

    page= request.GET.get('page')
    try:
        results = paginator.page(page)
    except PageNotAnInteger:
        results = paginator.page(1)  

    except EmptyPage:
            results = paginator.page(paginator.num_pages)        

    args.update(csrf(request))    
    args['form'] = form  
    args['results'] = results
    return render_to_response('userprofile/advanced_search.html', args,
                              context_instance=RequestContext(request)) 

表格如下:

<form action="/search/" method="post">{% csrf_token %}

          <ul class="list-unstyled">

            <li><h3>Country</h3></li>
            <li>{{form.country}}</li><br> 
            <h4>Province</h4>
            <li>{{form.province}}</li>
              <h4>City</h4>
            <li>{{form.city}}</li>


          </ul>

<input  type="submit" name="submit"  value="search" />

 </form>
     Search Results:
{% for p in results %}

            <div">
                  <div>
                      <br>
                       <strong><a href="/profile/{{p.username}}" >{{p.username}}</a></strong>
                         {{p.country}} <br>
                         {{p.province}} <br>
                         {{p.city}} <br>

                     </div>
                  </div>
{% endfor %}



<div>
    <div class="pagination">
      {% if results.has_previous %}
          <a href="?page={{ results.previous_page_number }}"> << Prev </a>&nbsp;&nbsp
      {% endif %}

       {% if results.has_next %}
          <a href="?page={{ results.next_page_number }}"> Next >> </a>
      {% endif %}
    </div>
  </div>

</div>

和urls.py

url(r'^search/', 'userprofile.views.profile_search'),

这是我得到的神秘错误:

Traceback:

    File "/home/supermario/.djenv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
      204.                 response = middleware_method(request, response)
    File "/home/supermario/.djenv/local/lib/python2.7/site-packages/debug_toolbar/middleware.py" in process_response
      89.             new_response = panel.process_response(request, response)
    File "/home/supermario/.djenv/local/lib/python2.7/site-packages/debug_toolbar/panels/request.py" in process_response
      31.             'post': [(k, request.POST.getlist(k)) for k in sorted(request.POST)],

    Exception Type: AttributeError at /search/
    Exception Value: 'dict' object has no attribute 'getlist'

我对此有一段时间的了解,非常感谢您的提示。

request.POST 应该是类似字典的 QueryDict 而不是简单的 python dict:

from django.http import QueryDict

request.POST = QueryDict('').copy()
request.POST.update(request.session['search-profiles-post'])