我有一個簡單的搜索欄,我想保持數據的用戶submited並顯示在表單提交後的搜索欄。我怎樣才能做到這一點 ?提交後,如何在模板表單上保留GET實例?
我使用GET
的搜索方法,但我不保存任何搜查任何模型項目,我不喜歡,我想知道是否有另一種方式來顯示它不使用數據庫存儲。
這裏是我的代碼如下所示:
views.py
def index(request):
allGigs = Gig.objects.filter(status=True)
context = {'gigs': allGigs}
return render(request, 'index.html', context)
def search_gigs(request):
title = request.GET['title']
request.session['title'] = title #a try with session, but the data is kept once the user returns to front page...
gigs = Gig.objects.filter(title__contains=title)
return render(request, 'index.html', {"gigs": gigs})
models.pyGig
模型title
CharField。
的index.html
<form role="search" method="GET" action="{% url 'search' %}">
<input type="text" name="title">
<input type="submit" value="submit">
</form>
urls.py
url(r'^search/$', views.search_gigs, name='search'), #example : /search/?title=my_search_word
url(r'^$', views.index, name='index'),
我想過使用Django的會話,但問題是,用戶只能看到他迴歸後的搜索內容索引頁面,任何建議?