带有get请求表单的友好URL

2024-03-28 13:02:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个简单的html表单:

<form action="test/" method="get" accept-charset="utf-8">

    <input type="text" name="first" value="" id="first">
    <input type="text" name="second" value="" id="second">
    <input type="text" name="third" value="" id="third">

    <p><input type="submit" value="Continue &rarr;"></p>
</form>

当用户单击“提交”按钮时,我希望获得如下友好的URL:

在www.site.com/test/first/second/third在

我该怎么做?在


Tags: textnametestformid表单inputvalue
2条回答
    <input type="text" name="first" value="" id="first">
    <input type="text" name="second" value="" id="second">
    <input type="text" name="third" value="" id="third">

    <p><input type="button" value="Continue &rarr;" onclick="submitFriendly();"></p>

<script type="text/javascript">
function submitFriendly() {
window.location.href = window.location.href + document.getElementById('first').value + '/' + document.getElementById('second').value + '/' + document.getElementById('third').value;
}
</script>

这应该行得通。它不会检查是否所有输入都已填充。在

用户在填写完表单并单击“继续”后的结束位置取决于您将form标记中的action属性设置为什么。您当前已将其设置为test/。在

{{3}你可以使用{cd4}在视图中使用{1},或者你可以使用^ 3}来重定向{

def test_view(request):
  return HttpResponseRedirect('/test/%s/%s/%s/' % request.POST['first'],
                                                  request.POST['second'],
                                                  request.POST['third])

相关问题 更多 >