2014-01-17 148 views
0

我想通過url傳遞我的輸入文本值,當我點擊我的提交按鈕。通過url傳遞變量來查看

views.py

def profile(request,username): 
     current_user = User.objects.filter(username=username) 
    return render_to_response ('profiles/search.html', {'current_user':current_user, 
    context_instance=RequestContext(request),) 

search.html

<form action="{%url 'profiles:profile' username=user.username %}" id="search_by_username" method ="get"> 
<input type="text" name = "searchname" class="text" placeholder="Search by username" /> 
<input type="submit" value="button" /> 
</form> 

urls.py

url(r'^view/(?P<username>\w+)/$', views.profile, name="profile"), 

點擊提交按鈕後,蔭得到這樣的網址:

192.168.1.33:8000/profiles/view/abraham/?searchname=merlin 

這裏abraham是登錄用戶,但實際上我需要搜索merlin。所以我怎樣才能用merlin代替亞伯拉罕。

回答

1

你不能得到一個表單域來自動把它的值放到表單提交到的URL中(除非用JavaScript中的表單來搞砸,否則會很糟糕)。

但正如你所看到的,你確實在請求參數中獲得了這些數據:因爲你正在做一個GET,它在request.GET['searchname']。那麼你爲什麼不首先將username參數放到URL /視圖中,然後使用它呢?

+0

非常感謝我的question.iam新的python和django.so是否有任何錯誤,請原諒我。現在我編輯我的表單動作是這樣的:'

' – Thameem

+0

什麼?我沒有說你犯過什麼錯誤。我給了你一些解決問題的建議。 –