[HTML テンプレート] ← textarea に入力
│
▼
[ai_comment.js] ← 「AIで柔らかくする」ボタン押下
│ fetch(text) で Django API に送信
▼
[views.py / soften_comment()] ← Django 側API
│
▼
[ai_filters.py] ← 誹謗中傷チェック(3段階)
│ ① NGワード判定(独自辞書)
│ ② OpenAI Moderation(自動誹謗中傷検出)
│ ③ OpenAI ChatCompletion(弱攻撃検出)
│
▼
AI で柔らかくした文章(soft_text)を JSON で返す
│
▼
[ai_comment.js] ← textarea に書き換え結果を反映
│
▼
ユーザーが最終的なコメントで「投稿(投票)」する
│
▼
[forms.py] clean_comment()
│ ← ai_filters.py の is_offensive() を再度実行(最終バリデーション)
▼
投稿確定
{{ form.comment }} <!-- textarea を生成 -->
<div class="edit-button text-center mb-4">
<button id="ai_soften_btn" type="button" class="btn btn-secondary px-5">
AIで言い回しをやさしくする
</button>
</div>
<div id="ai_result" class="text-muted mb-4"></div>
widgets = {
"comment": forms.Textarea(
attrs={
"id": "comment_input", # ← JSで取得するID
"class": "form-control",
"rows": 3,
}
),
}
id="comment_input" によって
JS の以下のコードが確実に動く:
document.getElementById("comment_input").value
def clean_comment(self):
comment = self.cleaned_data.get("comment", "").strip()
if is_offensive(comment):
raise ValidationError("攻撃的・不適切な内容が含まれています。")
return comment