アクセス禁止 (403)CSRF検証に失敗したため、リクエストは中断されました。 とエラーが出ました。
index.html に
{% load staticfiles %} <html lang="en"> <head> <meta charset="UTF-8"> <title>入力ページ</title> </head> <body> <form method="post" action="response"> <p>access_key:</p> <input id="id" name="id" type="text" placeholder="id" class="form-control"> <input type="submit" value="送信する"> </form> {% csrf_token %} </body> </html>
と書きました。
{% csrf_token %}
と書いているのになぜこのエラーが出るのかわかりません。
views.pyには
from django.shortcuts import render from .forms import UserIDForm # Create your views here. def index(request): user_id_form = UserIDForm(request.POST or None) context = { 'user_id_form': user_id_form, } return render(request, 'index.html',context) def response(request): s = set() with open('app/id.txt', 'r') as f: for line in f: s |= set(map(int, line.rstrip().split(','))) print(s) inp_id = '100' inp_id = int(inp_id) if inp_id in s: print(inp_id) return render(request, 'response.html')
と書きました。
index.html の送信ボタンを押したときにこのエラーが出ます。その時のURLは/responseです。response.htmlには
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>応答</title> </head> <body> <h2>応答ページ</h2> </body> </html>
と書きました。ためしに、response.htmlに
<body> <h2>応答ページ</h2> {% csrf_token %} </body
と{% csrf_token %}を付けたのですが、エラーは消えませんでした。どう直せばよいのでしょうか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。