質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Django

DjangoはPythonで書かれた、オープンソースウェブアプリケーションのフレームワークです。複雑なデータベースを扱うウェブサイトを開発する際に必要な労力を減らす為にデザインされました。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

1回答

1861閲覧

Django Reverse for 'login' not found.エラー

snowshink

総合スコア138

Django

DjangoはPythonで書かれた、オープンソースウェブアプリケーションのフレームワークです。複雑なデータベースを扱うウェブサイトを開発する際に必要な労力を減らす為にデザインされました。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2020/11/02 14:05

#問題点
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 %}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

login.htmlの、

HTML

1<form method="post" action="{% url 'login' %}"> 2```を、 3```HTML 4<form method="post" action="{% url 'auth:login' %}"> 5```じゃないですか?

投稿2020/11/02 22:42

ForestSeo

総合スコア2720

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

snowshink

2020/11/03 01:32

ありがとうございます。単純ミスでした。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問