首先,我的代码。
HTML
<form action= "/" onSubmit= "return validate(this);" method= "post">
<!--irrelevant from this point-->
Javascript,稍后在文件中
<script language= "JavaScript">
function validate(form){
if(form.text_box.value == ""){
alert("Please enter something");
return false;
}
search_string = form.text_box.value;
var encoded = encodeURIComponent(search_string).replace(/%20/g,'+');
form.action = "/search_results/" + encoded;
return true;
}
</script>
最后,python/flask 后端。
@maverick.route('/search_results/<search>', methods= ['GET', 'POST'])
def generateSearchResults(search = None):
search_string = urllib.unquote_plus(search)
为了解释,我试图在将字符串放入我的 url 之前引用它,因为该字符串将包含斜杠。但是,当我进行搜索时,页面没有加载,我看到这是 url 栏。
http://localhost:7666/search_results/%2Fthis%2Fis%2Fmy%2Fsearch
有人看到我的设置有任何问题吗?