回答編集履歴

7

修正

2020/06/08 09:02

投稿

ForestSeo
ForestSeo

スコア2720

test CHANGED
@@ -34,13 +34,17 @@
34
34
 
35
35
  {% load widget_tweaks %}
36
36
 
37
- <label for="load">
37
+ <form method="post" ・・・enctype="multipart/form-data">
38
38
 
39
- <img src="{{request.user.image.url}}" alt="" id="preview">
39
+ <label for="load">
40
40
 
41
- <div style="display:none;">{% render_field form.image id="load" %}</div>
41
+ <img src="{{request.user.image.url}}" alt="" id="preview">
42
42
 
43
+ <div style="display:none;">{% render_field form.image id="load" %}</div>
44
+
43
- </label>
45
+ </label>
46
+
47
+ </form>
44
48
 
45
49
  <!-- jsを使う -->
46
50
 

6

修正

2020/06/08 09:02

投稿

ForestSeo
ForestSeo

スコア2720

test CHANGED
@@ -4,28 +4,64 @@
4
4
 
5
5
  ----追記欄----
6
6
 
7
+ こんな感じにいけませんか?
8
+
9
+ [widget_tweaks](https://wonderwall.hatenablog.com/entry/2018/03/18/210000)を使い、
10
+
11
+ インストール
12
+
13
+ ```
14
+
15
+ pip install django-widget-tweaks
16
+
17
+ ```settings.py の INSTALLED_APPS に追加
18
+
19
+ ```Python
20
+
21
+ INSTALLED_APPS = [
22
+
23
+ "django.contrib.admin",
24
+
25
+ ・・・
26
+
27
+ "widget_tweaks", # 追加
28
+
29
+ ]
30
+
31
+ ```
32
+
33
+ ```HTML
34
+
35
+ {% load widget_tweaks %}
36
+
37
+ <label for="load">
38
+
39
+ <img src="{{request.user.image.url}}" alt="" id="preview">
40
+
41
+ <div style="display:none;">{% render_field form.image id="load" %}</div>
42
+
43
+ </label>
44
+
7
- > 個別でフォーム表示
45
+ <!-- jsを使う -->
46
+
47
+ <script type="text/javascript">
48
+
49
+ function preview_image(obj) {
50
+
51
+ var fileReader = new FileReader();
52
+
53
+ fileReader.onload = (function() {
54
+
55
+ document.getElementById("preview").src = fileReader.result;
56
+
57
+ });
58
+
59
+ fileReader.readAsDataURL(obj.files[0]);
60
+
61
+ }
62
+
63
+ </script>
8
64
 
9
65
 
10
66
 
11
- そうしてから、{{form.icon}}ごと消さないと多分「現在:」みたいなのが残るので、
12
-
13
- ```HTML
67
+ ```
14
-
15
- <div style="display:none;">
16
-
17
- {{form.icon}}
18
-
19
- </div>
20
-
21
- ```非表示にして、
22
-
23
- ```HTML
24
-
25
- <img src="{{request.user.icon.url}}" alt="">
26
-
27
- ```これを重ねれば、現在の写真をクリックしたら、ファイルが開いて参照できるみたいになります。
28
-
29
- 写真を選択した瞬間、画像を変えるにはJavaScriptが必要です。
30
-
31
- (短いコードでいけると思う)

5

修正

2020/06/08 00:11

投稿

ForestSeo
ForestSeo

スコア2720

test CHANGED
@@ -12,7 +12,7 @@
12
12
 
13
13
  ```HTML
14
14
 
15
- <div style="opacity:0;">
15
+ <div style="display:none;">
16
16
 
17
17
  {{form.icon}}
18
18
 

4

修正

2020/06/07 23:59

投稿

ForestSeo
ForestSeo

スコア2720

test CHANGED
@@ -12,7 +12,7 @@
12
12
 
13
13
  ```HTML
14
14
 
15
- <div style="opacity:0">
15
+ <div style="opacity:0;">
16
16
 
17
17
  {{form.icon}}
18
18
 

3

修正

2020/06/07 23:22

投稿

ForestSeo
ForestSeo

スコア2720

test CHANGED
@@ -12,7 +12,7 @@
12
12
 
13
13
  ```HTML
14
14
 
15
- <div style="display: none;">
15
+ <div style="opacity:0">
16
16
 
17
17
  {{form.icon}}
18
18
 

2

修正

2020/06/07 23:19

投稿

ForestSeo
ForestSeo

スコア2720

test CHANGED
@@ -1,4 +1,4 @@
1
- UpdateViewでは、デフォルト画像は表示されず、
1
+ UpdateViewでは、デフォルトでは画像は表示されず、
2
2
 
3
3
  URLしか表示されません。
4
4
 

1

修正

2020/06/07 21:21

投稿

ForestSeo
ForestSeo

スコア2720

test CHANGED
@@ -1,3 +1,31 @@
1
1
  UpdateViewでは、デフォルト画像は表示されず、
2
2
 
3
3
  URLしか表示されません。
4
+
5
+ ----追記欄----
6
+
7
+ > 個別でフォーム表示
8
+
9
+
10
+
11
+ そうしてから、{{form.icon}}ごと消さないと多分「現在:」みたいなのが残るので、
12
+
13
+ ```HTML
14
+
15
+ <div style="display: none;">
16
+
17
+ {{form.icon}}
18
+
19
+ </div>
20
+
21
+ ```非表示にして、
22
+
23
+ ```HTML
24
+
25
+ <img src="{{request.user.icon.url}}" alt="">
26
+
27
+ ```これを重ねれば、現在の写真をクリックしたら、ファイルが開いて参照できるみたいになります。
28
+
29
+ 写真を選択した瞬間、画像を変えるにはJavaScriptが必要です。
30
+
31
+ (短いコードでいけると思う)