回答編集履歴
6
修正
test
CHANGED
@@ -6,7 +6,7 @@
|
|
6
6
|
|
7
7
|
def clean_file(self):
|
8
8
|
|
9
|
-
file = self.cleaned_data
|
9
|
+
file = self.cleaned_data["file"]
|
10
10
|
|
11
11
|
if not file:
|
12
12
|
|
5
修正
test
CHANGED
@@ -8,7 +8,7 @@
|
|
8
8
|
|
9
9
|
file = self.cleaned_data.get("file")
|
10
10
|
|
11
|
-
if not i
|
11
|
+
if not file:
|
12
12
|
|
13
13
|
raise forms.ValidationError("サイズエラー")
|
14
14
|
|
4
修正
test
CHANGED
@@ -14,4 +14,26 @@
|
|
14
14
|
|
15
15
|
return file
|
16
16
|
|
17
|
+
```
|
18
|
+
|
19
|
+
```HTML
|
20
|
+
|
21
|
+
<!-- エラーを表示 -->
|
22
|
+
|
23
|
+
{% if form.errors %}
|
24
|
+
|
25
|
+
{% for field in form %}
|
26
|
+
|
27
|
+
{% for error in field.errors %}
|
28
|
+
|
29
|
+
<p class="error">{{error}}</p>
|
30
|
+
|
31
|
+
{% endfor %}
|
32
|
+
|
33
|
+
{% endfor %}
|
34
|
+
|
35
|
+
{% endif %}
|
36
|
+
|
37
|
+
```
|
38
|
+
|
17
|
-
|
39
|
+
とかいけますか?
|
3
修正
test
CHANGED
@@ -1 +1,17 @@
|
|
1
|
+
```Python
|
2
|
+
|
3
|
+
class Hoge(forms.Form):
|
4
|
+
|
5
|
+
file = forms.FileField()
|
6
|
+
|
1
|
-
|
7
|
+
def clean_file(self):
|
8
|
+
|
9
|
+
file = self.cleaned_data.get("file")
|
10
|
+
|
11
|
+
if not image:
|
12
|
+
|
13
|
+
raise forms.ValidationError("サイズエラー")
|
14
|
+
|
15
|
+
return file
|
16
|
+
|
17
|
+
```とかいけますか?
|
2
修正
test
CHANGED
@@ -1,47 +1 @@
|
|
1
|
-
|
1
|
+
今自分の間違えに気づいて編集してます。
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
```Python
|
6
|
-
|
7
|
-
from django.core.exceptions import ValidationError
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
def size_valid(f):
|
12
|
-
|
13
|
-
if f.size == 0:
|
14
|
-
|
15
|
-
raise ValidationError("サイズエラー")
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
class ・・・(models.Model):
|
20
|
-
|
21
|
-
logo = models.ImageField(upload_to="・・", validators=[size_valid])
|
22
|
-
|
23
|
-
```でいけなっかたら、
|
24
|
-
|
25
|
-
```Python
|
26
|
-
|
27
|
-
def size_valid(f):
|
28
|
-
|
29
|
-
if f.size is None:
|
30
|
-
|
31
|
-
raise ValidationError("サイズエラー")
|
32
|
-
|
33
|
-
```とかで、それでもいけなかったら
|
34
|
-
|
35
|
-
```Python
|
36
|
-
|
37
|
-
def size_valid(f):
|
38
|
-
|
39
|
-
try:
|
40
|
-
|
41
|
-
size = f.size
|
42
|
-
|
43
|
-
except:
|
44
|
-
|
45
|
-
raise ValidationError("サイズエラー")
|
46
|
-
|
47
|
-
```とかでしょうか。
|
1
修正
test
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
現状の`forms.py`が見たいのですが、
|
2
|
+
|
3
|
+
|
4
|
+
|
1
5
|
```Python
|
2
6
|
|
3
7
|
from django.core.exceptions import ValidationError
|