基本的には、検索フォームと結果を表示するテーブルを持つテンプレートがあります。ヘッダー列をクリックすると、結果を並べ替えることができます。 私はget
メソッドを検索フォームに使用し、テーブルの結果を最後のURLを取得することによって並べ替えます。htmlページでURLパラメタをdjangoと動的に置き換えるにはどうすればいいですか?
views.py
if sort_by == "desc":
order_by = order_by
sort_by = "asc"
else:
sort_by = "desc"
url = request.META.get('HTTP_REFERER')
if (url != None):
search_params = {}
param = urlparse.urlparse(url)
if (not 'search_button' in request.GET):
get_request = re.split('&',param.query)
for single_request in get_request:
search_query = re.split('=',single_request)
search_params[search_query[0]] = search_query[1]
userlist = show_data(order_by,sort_by,search_params)
else:
userlist = show_data(order_by, sort_by, request.GET)
def show_data(order_by, sort_by, get_data):
//the function get the value from db
return row
template.py
<table id="user-table">
<tr>
<th><a href="?order_by=userId&sort={{sort}}">User id</a></th>
<th><a href="?order_by=userName&sort={{sort}}">Name</a></th>
<th><a href="order_by=userAddr&sort={{sort}}">Address</a></th>
</tr>
<tbody>
{% for item in table_data %}
<tr>
<td>{{ item.userId|default_if_none:"" }}</td>
<td>{{ item.userName|default_if_none:"" }}</td>
<td>{{ item.userAddr|default_if_none:"" }}</td>
</tr>
フィルタリングし、テーブル結果がデータをソートすることができることができる検索フォーム。ここで は、サンプルのURLです:列ヘッダを
http://localhost:8080/userlist/?order_by=userAddr&sort=asc
をクリックすると、検索ボタンが
http://localhost:8080/userlist/?userId=&userName=Jane&userAddr=&search_button=search
をクリックすると、URLが変更以来
しかし、それは再びソートすることができませんし、 search_param
は入手できませんでしたuserName=Jane
urlパラメータをhtmlページに動的に置き換えることは可能ですか列見出しをクリックすると低いですか?
http://localhost:8080/userlist/?userId=&userName=Jane&userAddr=&search_button=search&order_by=userAddr&sort=desc
ありがとうございます。どんな提案も考慮されます。
それは働きます!非常に@ruddaありがとう – natadecoco