回答編集履歴
1
form情報をQueryDict → Dict →jsonにしてsessionに格納しsession→dict→QueryDictに戻してformに設定する。
test
CHANGED
@@ -2,55 +2,63 @@
|
|
2
2
|
|
3
3
|
|
4
4
|
|
5
|
-
|
5
|
+
# formをQueryDict→Dict→jsonにしてsessionに格納
|
6
6
|
|
7
|
-
from django.shortcuts import redirect
|
8
7
|
|
9
|
-
|
10
8
|
|
11
|
-
|
9
|
+
form_QueryDict = self.request.GET
|
12
10
|
|
13
|
-
|
11
|
+
form_Dict = dict(form_QueryDict)
|
14
12
|
|
15
|
-
|
13
|
+
form_json = json.dumps(form_Dict)
|
16
14
|
|
17
|
-
|
15
|
+
self.request.session['form_data'] = form_json
|
18
16
|
|
19
|
-
form = HogeForm(request.session.get('form_data'))
|
20
17
|
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
===================================================================================
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
# sessionよりform情報のjsonを取得してDict→QueryDictに変換してformに戻す。
|
26
|
+
|
27
|
+
form_data = request.session.get('form_data')
|
28
|
+
|
29
|
+
if form_data is None:
|
30
|
+
|
21
|
-
|
31
|
+
form = forms.FilterForm()
|
22
32
|
|
23
33
|
else:
|
24
34
|
|
25
|
-
|
35
|
+
form_data_json = json.loads(form_data)
|
26
36
|
|
27
|
-
|
37
|
+
qdict = QueryDict('', mutable=True)
|
28
38
|
|
29
|
-
|
39
|
+
for key, value in form_data_json.items():
|
30
40
|
|
31
|
-
|
41
|
+
d = {key: value}
|
32
42
|
|
33
|
-
|
43
|
+
qdict.update(MultiValueDict(d) if isinstance(value, list) else d)
|
34
44
|
|
35
|
-
|
45
|
+
form = forms.FilterForm(qdict)
|
36
46
|
|
37
|
-
|
47
|
+
return render(
|
38
48
|
|
39
|
-
|
49
|
+
request,
|
40
50
|
|
41
|
-
|
51
|
+
'search.html',
|
42
52
|
|
43
|
-
|
53
|
+
{
|
44
54
|
|
45
|
-
|
55
|
+
'form': form,
|
46
56
|
|
47
|
-
|
57
|
+
'is_admin': is_admin
|
48
58
|
|
49
|
-
|
59
|
+
}
|
50
60
|
|
51
|
-
|
61
|
+
)
|
52
|
-
|
53
|
-
return render(request, 'hoge_form.html', context)
|
54
62
|
|
55
63
|
|
56
64
|
|