回答編集履歴

1

修正

2018/05/16 19:54

投稿

toritoritorina
toritoritorina

スコア972

test CHANGED
@@ -1,6 +1,4 @@
1
- ContactFormを作成する際に幾つかの引数を渡すこともできますが、
2
-
3
- 素直にviews.pyから送信するほうが柔軟に思います。
1
+ 素直にviews.pyから送信するか、
4
2
 
5
3
 
6
4
 
@@ -26,6 +24,38 @@
26
24
 
27
25
 
28
26
 
27
+ 又は、ContactFormのsend_mailメソッドに引数をつけて呼び出すようにします。
29
28
 
30
29
 
30
+
31
+ ```python
32
+
33
+ def form_valid(self, form):
34
+
35
+ to = [self.object.mailAddress]
36
+
37
+ form.send_email(to)
38
+
39
+ return super().form_valid(form)
40
+
41
+ ```
42
+
43
+
44
+
45
+
46
+
47
+ ```python
48
+
49
+ def send_email(self, to):
50
+
51
+ subject = self.cleaned_data['title']
52
+
53
+ from_email = self.cleaned_data['email']
54
+
31
- これはメールのタイトルや本文をテンプレートを使って組み立てたい、といったケースにも簡単に対応できます。
55
+ contents = self.cleaned_data['contents']
56
+
57
+ message = '\n\n内容:\n' + contents + '\n\n返信先:\n' + from_email
58
+
59
+ send_mail(subject, message, settings.EMAIL_HOST_USER, to)
60
+
61
+ ```