我在我的开发机器上修改了以下方法,它在那里工作得很好:
def profile_search(request):
args = {}
qs=[]
if request.method == 'GET':
page = request.GET.get('page')
print 'page is:' + str(page)
if 'search-profiles-post' in request.session:
request.POST = QueryDict('').copy()
request.POST.update(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_country: qs.append(Q(country__icontains = s_country))
if s_province: qs.append( Q(province__icontains=s_province))
if s_city: qs.append( Q(city__icontains=s_city))
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) # << This line is problematic
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))
但是,当我将视图复制到我的产品中(在修改之前工作正常)时,我收到了这个错误:
UnboundLocalError at /search/
local variable 'profiles' referenced before assignment
追溯:
File "/root/.djenv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "./userprofile/views.py" in profile_search
739. paginator = Paginator(profiles,12)
Exception Type: UnboundLocalError at /search/
Exception Value: local variable 'profiles' referenced before assignment
据我所知,我的生产和开发之间唯一不同的代码是 settings.py (使用不同的数据库)。
由于搜索查询要保存到会话中,我怀疑它与生产中的会话处理有关,但我不知道如何。
可能相关的一点:虽然我在生产机器上安装了调试工具栏,但即使在调试模式下我也看不到它的选项卡。
感谢您提供解决此问题的提示。