带有获取请求表单的友好URL

时间:2010-12-22 09:44:08

标签: python django

我有这个简单的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>

当用户点击提交按钮时,我希望得到这样的友好网址:

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

我该怎么做?

2 个答案:

答案 0 :(得分:3)

用户在填写表单并点击“继续”后结束,取决于您在action标记中设置form属性的内容。您目前已将其设置为test/

如果你希望它们以test/<first_val>/<second_val>/<third_val>/结束,那么你可以使用一些JavaScript(每个pythonFoo的答案),或者你可以在test/指向的视图中重定向,使用{{3 }}:

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

答案 1 :(得分: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>

这应该有效。它不会检查是否所有输入都已填满。