状況:検索に使用するフォームがあり、ユーザーが結果をフィルタリングできるように、結果ページに同じフォームを返します。ガベージ入力を取り除くために、clean_xxxメソッドを実装しました。
残念ながら、フォームはクリーンアップされていても、ガベージ入力とともに結果ページに返されます。クリーンなデータを表示するにはどうすればよいですか?
ここにいくつかのアイデアがあります:
- clean_xxxメソッドで、self.data.xxx=cleaned_xxx値を設定します
- cleaned_dataを使用して新しいフォームを再初期化します。
forms.py:
SearchForm:
def clean_q(self):
q = self.cleaned_data.get('q').strip()
# Remove Garbage Input
sanitized_keywords = re.split('[^a-zA-Z0-9_ ]', q)
q = "".join(sanitized_keywords).strip()
#TODO: Fix
self.data['q'] = q
return q
views.py
search_form = SearchForm(params, user=request.user)
if search_form.is_valid():
# Build the Query from the form
# Retrieve The Results
else:
# For errors, no results will be displayed
_log.error('Search: Form is not valid. Error = %s' %search_form.errors)
response = {
'search_form': search_form...
}
ご協力いただきありがとうございます。