2010-12-22 103 views
0

我有這個簡單的HTML表單:友好的URL用GET請求表單

<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/第一/第二/第三

我該怎麼做?

回答

3

用戶在填寫完表格並點擊「繼續」後最終取決於您在form標記中設置的action屬性。您目前已將其設置爲test/

如果你希望他們在test/<first_val>/<second_val>/<third_val>/結束,那麼您可以使用一些JavaScript(每pythonFoo的答案),也可以在認爲test/點重定向,使用HttpResponseRedirect

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

不幸的是,這意味着用戶最終會進行2次GET往返旅行,而不是1次。似乎必須有更好的方式,但我沒有看到它,而且我一直在尋找一個小時。 – g33kz0r 2013-07-02 21:17:57

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> 

這應該有效。它不檢查是否所有輸入都已填滿。

+0

這隻如果用戶啓用了JavaScript,則可以工作 – g33kz0r 2013-07-02 21:21:16