gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:48:27 24 4
gpt4 key购买 nike

灵感来自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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com