Djangoデフォルトのログイン機能を実装していたのですが、エラーが出てしまいました。
エラーの内容は下記の通りです。(一部抜粋)
NoReverseMatch at /accounts/login/
Reverse for 'login' not found. 'login' is not a valid view function or pattern name.
Error during template rendering
In templateプロジェクトディレクトリ/monitor/templates/base.html, error at line 0
Reverse for 'login' not found. 'login' is not a valid view function or pattern name.
以下は、プロジェクトディレクトリ/monitor/urls.pyになります。
from django.urls import path,include app_name = 'appname' urlpatterns = [ path('accounts/', include('django.contrib.auth.urls')), ]
以下は、プロジェクトディレクトリ/monitor/templates/monitor/base.html
になります。
{% load static %} <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet"> <link href="{% static 'monitor/style.css' %}" rel="stylesheet"> <title>{% if request.path != '/' %}{% block title %}{% endblock %} | {% endif %}Blog App with Auth | Django2 Tutorial</title> </head> <body> <div class="container"> <header> <p class="site-name">Blog App</a></p> <nav> <ul> {% comment %} {% if user.is_authenticated %} <li><a href="{% url 'appname:logout' %}" class="logout">Logout</a></li> {% else %} <li><a href="{% url 'accounts:signup' %}" class="signup">Sign up</a></li> <li><a href="{% url 'appname:login' %}" class="login">Login</a></li> {% endif %} <li>Add Post</a></li> {% endcomment %} </ul> </nav> </header> <div class="content"> {% block content %}{% endblock %} </div> </div> </body> </html>
以下は、login.htmlです。
{% extends "base.html"%} {% block content %} {% if form.errors %} <p>Your username and password didn't match. Please try again.</p> {% endif %} {% if next %} {% if user.is_authenticated %} <p>Your account doesn't have access to this page. To proceed, please login with an account that has access.</p> {% else %} <p>Please login to see this page.</p> {% endif %} {% endif %} <form method="post" action="{% url 'login' %}"> {% csrf_token %} <table> <tr> <td>{{ form.username.label_tag }}</td> <td>{{ form.username }}</td> </tr> <tr> <td>{{ form.password.label_tag }}</td> <td>{{ form.password }}</td> </tr> </table> <input type="submit" value="login" /> <input type="hidden" name="next" value="{{ next }}" /> </form> {% endblock %}
どなたか、エラー解決の為、ご助言頂けましたら幸いです。
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。