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

标签 django django-views

灵感来自this博客文章中,我试图通过将搜索参数保存到 session 中来创建一个处理配置文件搜索的 View ,以便可以通过分页保留查询。

以下是观点:

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'])

关于Django 'dict' 对象没有属性 'getlist',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28801731/

相关文章:

django:如何将主页链接添加到管理员

Python:如何使一个变量在 40% 的时间内为真

python Django : how to select the index type?

python - 类在 Django 中没有 'objects' 成员

python - “initial”是此函数的无效关键字参数

python - 在 django vanilla CreateView 上设置当前用户

python - django ssl登录重定向到非ssl页面

python - 图库应用程序的管理界面

login_required() 装饰器中的 Django 新手 : try to reverse auth. views.login

javascript - Django:如果url是用户手动编写的,则重定向