実現したいこと
作成したアプリでログイン機能とサインアップ機能の際にログインしようとしたり、サインアップしようとしたらエラーが発生してしまいました。
ログインとサインアップが出来るようにしたいです。
CSRFに関するエラーなのですが、見て貰えないでしょうか?
発生している問題・エラーメッセージ
アクセス禁止 (403) CSRF検証に失敗したため、リクエストは中断されました。 Help Reason given for failure: Origin checking failed - https://***********************.vfs.cloud9.ap-northeast-1.amazonaws.com does not match any trusted origins. In general, this can occur when there is a genuine Cross Site Request Forgery, or when Django’s CSRF mechanism has not been used correctly. For POST forms, you need to ensure: Your browser is accepting cookies. The view function passes a request to the template’s render method. In the template, there is a {% csrf_token %} template tag inside each POST form that targets an internal URL. If you are not using CsrfViewMiddleware, then you must use csrf_protect on any views that use the csrf_token template tag, as well as those that accept the POST data. The form has a valid CSRF token. After logging in in another browser tab or hitting the back button after a login, you may need to reload the page with the form, because the token is rotated after a login. You’re seeing the help section of this page because you have DEBUG = True in your Django settings file. Change that to False, and only the initial error message will be displayed. You can customize this page using the CSRF_FAILURE_VIEW setting.
該当のソースコード
templates/accounts/signup.html
1{% extends 'layout.html' %} 2 3{% block content %} 4<h1>ユーザー登録</h1> 5<section class='common-form'> 6 <form method='post'> 7 {% csrf_token %} ←CSRFの記述 8 {{ form.as_p }} 9 <button type='submit'>登録</button> 10 </form> 11</section> 12{% endblock %}
templates/registration/login.html
1{% extends '../layout.html' %} 2 3{% block content %} 4<h1>Login</h1> 5<section class='common-form'> 6 {% if form.errors %} 7 <p class='error-msg'>ユーザー名とパスワードが一致しません。もう一度入力しなおしてください。</p> 8 {% endif %} 9 10 {% if next %} 11 {% if user.is_authenticated %} 12 <p class='error-msg'>このアカウントではアクセスできません。</p> 13 {% else %} 14 <p class='error-msg'>このページにアクセスするためにはログインしてください。</p> 15 {% endif %} 16 {% endif %} 17 18 <form action='{% url "login" %}' method='post'> 19 {% csrf_token %} ←CSRFの記述 20 <input type='hidden' name='next' value='{{ next }}'/> 21 {{ form.as_p }} 22 <button type='submit'>ログイン</button> 23 </form> 24</section> 25{% endblock %}
試したこと
CSRFについて調べて、フォームの書き方が間違いかと思い、確認したのですが、
間違っている様には見えませんでした。
またPythonのバージョンを上げた事に伴い、djangoのバージョンも上がったようで、それによって影響しているのかなとも、考えていますがどうでしょうか?
補足情報(FW/ツールのバージョンなど)
python: Python 3.11.0
django: 4.2.4

回答1件
あなたの回答
tips
プレビュー