3 回答

TA貢獻1812條經驗 獲得超5個贊
所以我猜您的第一選擇是創建一個用于搜索的表單,然后呈現帶有結果的頁面。當我們正在討論如何擴展它時,很高興看到您如何實現這一目標。
在沒有看到您對搜索功能的實現的情況下,我認為一個很好的例子是將搜索從基于表單的搜索轉移到查詢語言。
但我們不要超前!畢竟,讓我們以簡單的方式處理表單吧!
假設這是你的表格
from django import forms
class MyForm(forms):
title #
author #
genre #
現在,每次標題、作者、流派選擇都會隨請求一起發送。那么簡單的方法就是向第二個表單添加更多內容并在渲染時將當前狀態傳遞給它!
from django import forms
class SecondForm(forms):
title
author
genre
language # new stuff!
因此,當您在函數處理程序/類視圖中獲取當前數據時,您可以從 MyForm 數據創建一個新表單 SecondForm,您可以在此處閱讀更多信息。
def refine_search(request):
# the form has been submitted so it's a safe assumption to have request.method == 'POSt'
# but this will make it harder to share a link to a search page
# load the search results
# prepare the second form to be rendered on the result page
form = SecondForm(request.POST)
# now your can render your result page passing the form
# and it will be rendered with the state from the previous!

TA貢獻1780條經驗 獲得超1個贊
我不確定您是否想要在擴展過濾器中選擇流派?因為如果是的話,我看不到問題......?如果不是,為什么不使用隱藏輸入來傳遞流派呢? forms.CharField(widget=forms.HiddenInput())
添加回答
舉報