#問題点
path('auth/', include('django.contrib.auth.urls')),
を
path('auth/', include(('django.contrib.auth.urls', 'auth'))),
に変えて、
{% url 'auth:login'}
のリンクからジャンプしたら、
localhost:8080/auth/loginでエラーが出ました。
変更前ではエラーは出ませんでした。ログインページへのリンクを付けたいのですが、どうしたらいいですか。
##エラー画面
NoReverseMatch at /auth/login/
Reverse for 'login' not found. 'login' is not a valid view function or pattern name.
Request Method: GET
Request URL: http://127.0.0.1:8000/auth/login/
Django Version: 3.0.1
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'login' not found. 'login' is not a valid view function or pattern name.
...
Error during template rendering
In template C:\Users***\Documents\python\django\demo\schoolar\template\base.html, error at line 9
Reverse for 'login' not found. 'login' is not a valid view function or pattern name. 1 {% load static %} 2 <!DOCTYPE html> 3 <html lang="en"> 4 <head> 5 <meta charset="UTF-8"> 6 <link rel="stylesheet" type="text/css" href="{% static 'css/bootstrap/bootstrap.min.css' %}"> 7 <link rel="stylesheet" type="text/css" href="{% static 'css/bootstrap/bootstrap-grid.min.css' %}"> 8 <link rel="stylesheet" type="text/css" href="{% static 'css/bootstrap/bootstrap-reboot.min.css' %}"> 9 {% block pageStyle %}<link rel="stylesheet" type="text/css" href="{% static 'css/base.css' %}">{% endblock %} 10 <title>{% block title %}Scholar{% endblock %}</title> 11 </head> 12 <body> 13 <header> 14 <a href="{% url 'home' %}">HOME</a> 15 <a href=" {% url 'about' %}">ABOUT</a> 16 <a href="{% url 'post' %}">POSTS</a> 17 </header> 18 <div class="logo"> 19 <img src="{% static 'img/school-entrance.jpg' %}" width="100%" alt="正面">
##ソース
app/urls.py
python3
1"""scholar URL Configuration 2 3The `urlpatterns` list routes URLs to views. For more information please see: 4 https://docs.djangoproject.com/en/3.0/topics/http/urls/ 5Examples: 6Function views 7 1. Add an import: from my_app import views 8 2. Add a URL to urlpatterns: path('', views.home, name='home') 9Class-based views 10 1. Add an import: from other_app.views import Home 11 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') 12Including another URConf 13 1. Import the include() function: from django.urls import include, path 14 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 15""" 16from django.contrib import admin 17from django.urls import path, include 18 19urlpatterns = [ 20 path('admin/', admin.site.urls), 21 path('', include('pages.urls'), name="home"), 22 path('post/', include('posts.urls')), 23 path('about/', include('about.urls')), 24 path('auth/', include(('django.contrib.auth.urls', 'auth'))), 25]
template/base.html
{% load static %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <link rel="stylesheet" type="text/css" href="{% static 'css/bootstrap/bootstrap.min.css' %}"> <link rel="stylesheet" type="text/css" href="{% static 'css/bootstrap/bootstrap-grid.min.css' %}"> <link rel="stylesheet" type="text/css" href="{% static 'css/bootstrap/bootstrap-reboot.min.css' %}"> {% block pageStyle %}<link rel="stylesheet" type="text/css" href="{% static 'css/base.css' %}">{% endblock %} <title>{% block title %}Scholar{% endblock %}</title> </head> <body> <header> <a href="{% url 'home' %}">HOME</a> <a href=" {% url 'about' %}">ABOUT</a> <a href="{% url 'post' %}">POSTS</a> </header> <div class="logo"> <img src="{% static 'img/school-entrance.jpg' %}" width="100%" alt="正面"> <p>{% block logoTitle %}Djan高校{% endblock %}</p> </div> {% block content %} {% endblock content %} </body> </html>
template/registration/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> {# Assumes you setup the password_reset view in your URLconf #} <p><a href="{% url 'password_reset' %}">Lost password?</a></p> {% endblock %}
template/home.html
{% extends 'base.html' %} {% block title %}Djan高校ホームページ{% endblock %} {% block content %} <div class="container row"> {% if user.is_authenticated %} <div class="col-4"> <div class="row"> {{ user.first_name }}さん、ようこそ。 </div>' <span><a href={% url 'auth:logout' %}>ログアウト</a></span> </div> {% else %} <div class="col-4"> <span><a href="{% url 'auth:login' %}">ログイン</a></span> </div> {% endif %} <div class="col-6"> <p>お知らせ</p> </div> </div> {% endblock content %}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/11/03 01:32