NoReverseMatch at /login/ [duplicate] - django

This question already has answers here:
Error "Reverse for 'django.contrib.auth.views.login' not found" [duplicate]
(2 answers)
Closed 5 years ago.
NoReverseMatch at /login/ is the error I'm getting from the console, I have a feeling this is something to do with the linking that is going on between urls.py but I can't figure out which part of it is wrong. Any help would be greatly appreciated on this.
The full error from the console is beneath
Reverse for 'django.contrib.auth.views.login' not found.
'django.contrib.auth.views.login' is not a valid view function or pattern name.
urls.py
from django.contrib import admin
from polls import views as polls_views
from django.conf.urls import url, include
from django.contrib.auth import views as auth_views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^calendar/', include('calendarium.urls')),
url(r'^polls/', include('polls.urls')),
url(r'^$', polls_views.home, name='home'),
url(r'^login/$', auth_views.login, {'template_name': 'login.html'}, name='login'),
url(r'^logout/$', auth_views.logout, {'next_page': 'login'}, name='logout'),
url(r'^signup/$', polls_views.signup, name='signup'),
]
base.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>{% block title %}OnNote{% endblock %}</title>
</head>
<body>
<header>
<h1>My Site</h1>
{% if user.is_authenticated %}
logout
{% else %}
login / signup
{% endif %}
<hr>
</header>
<main>
{% block content %}
{% endblock %}
</main>
</body>
</html>
login.html
{% extends 'base.html' %}
{% block title %}Login{% endblock %}
{% block content %}
<div id="content-container" class="container p-none">
<div class="lgn-container col-lg-8">
<form id="login-form" method="post"
action="{% url 'django.contrib.auth.views.login' %}">
{% csrf_token %}
<table class="table">
<tr>
<td><label for="id_username">Username</label></td>
<td><input id="id_username" name="username"
type="text" class="form-control"></td>
</tr>
<tr>
<td><label for="id_password">Password</label></td>
<td><input id="id_password" name="password"
type="password" class="form-control"></td>
</tr>
</table>
{% if form.errors %}
<p class=" label label-danger">
Your username and password didn't match.
Please try again.
</p>
{% endif %}
<input type="submit" value="Login"
class="btn btn-primary pull-right" />
<input type="hidden" name="next" value="{{ next }}" />
</form>
</div>
</div>
{% endblock content %}
Can anyone point out what I'm doing wrong here? I think it's something to do with my urls.py but I can't figure out what, any help would be greatly appreciated

<form id="login-form" method="post" action="">
in login.html
you dont need the action, simply keeping it to post to itself will work

You can either use form with no action or change {% url 'django.contrib.auth.views.login' %} to {% url 'login' %}
By the way, standard login view is deprecated and you will get a warning.
It's better to use LoginView.as_view() or subclass it if you want to change the default template_name (which is 'registration/login.html').

Related

NoReverseMatch at /account/login/ (Trying to use Class based authentication views)

I am trying to use Django's class based authentication views and am getting the following error when attempting to access the login view:
NoReverseMatch at /account/login/
Reverse for 'register' not found. 'register' is not a valid view function or pattern name.
Error during template rendering
In template /Users/justin/Desktop/Programming/Python/django_book/social/website/account/templates/base.html, error at line 0
All authentication templates are stored at account/templates/registration/ and dashboard.html is stored at account/templates/account/, here is the code:
account/urls.py:
from django.urls import path
from . import views
from django.contrib.auth import views as auth_views
urlpatterns = [
path('', views.dashboard, name = 'dashboard'),
path('login/', auth_views.LoginView.as_view(), name = 'login'),
path('logout/', auth_views.LogoutView.as_view(), name = 'logout'),
]
login.html:
{% extends "base.html" %}
{% block title %}Log-in{% endblock %}
{% block content %}
<h1>Log-in</h1>
{% if form.errors %}
<p>
Your username and password didn't match.
Please try again.
</p>
{% else %}
<p>Please, use the following form to log-in. If you don't have an account register here</p>
{% endif %}
<div class="login-form">
<form action="{% url 'login' %}" method="post">
{{ form.as_p }}
{% csrf_token %}
<input type="hidden" name="next" value="{{ next }}" />
<p><input type="submit" value="Log-in"></p>
</form>
</div>
{% endblock %}
base.html:
{% load static %}
<!DOCTYPE html>
<html>
<head>
<title>{% block title%}{% endblock %}</title>
<link rel="stylesheet" href="{% static 'css/base.css' %}">
</head>
<body>
<div id="header">
<span class = 'logo'>Bookmarks</span>
{% if request.user.is_authenticated %}
<ul class = 'menu'>
<li {% if section == "dashboard" %}class="selected"{% endif %}>
My dashboard
</li>
<li {% if section == "images" %}class="selected"{% endif %}>
Images
</li>
<li {% if section == "people" %}class="selected"{% endif %}>
People
</li>
</ul>
{% endif %}
<span class = 'user'>
{% if request.user.is_authenticated %}
Hello {{ request.user.first_name }},
Logout
{% else %}
Login
{% endif %}
</span>
</div>
<div id="content">
{% block content %}
{% endblock %}
</div>
</body>
</html>
Note sure if this is needed but the account/views.py with the dashboard view:
from django.contrib.auth.decorators import login_required
#login_required
def dashboard(request):
return render(request,
'account/dashboard.html',
{'section': 'dashboard'})
and dashboard.html:
{% extends "base.html" %}
{% block title %}Dashboard{% endblock %}
{% block content %}
<h1>Dashboard</h1>
<p>Welcome to your dashboard.</p>
{% endblock %}
I am following the book 'Django 2 By Example' and at this point I believe I have directly copy and pasted the code from here (https://github.com/PacktPublishing/Django-2-by-Example/tree/master/Chapter04) to try and fix this error, but I am still getting. I should note that I am using Django 3.2.6 and am not sure if this is causing it. Thanks for any help with this.

PasswordChangeView not redirecting to PasswordChangeDoneView (or changing the password)

I am trying to use the Django authentication views. My login/logout views are working without a problem.
urls.py:
from django.urls import path
from django.contrib.auth import views as auth_views
from account import views
urlpatterns = [
path('', views.dashboard, name = 'dashboard'),
path('login/', auth_views.LoginView.as_view(), name = 'login'),
path('logout/', auth_views.LogoutView.as_view(), name = 'logout'),
path('password_change', auth_views.PasswordChangeView.as_view(), name = 'password_change'),
path('password_change/done', auth_views.PasswordChangeDoneView.as_view(), name = 'password_change_done'),
]
password_change_form.html:
{% block body %}
<h1>Change your password</h1>
<p>Please use the following form to change your password:</p>
<div class = 'password-change-form'>
<form class="" action="{% url 'login' %}" method="post">
{{ form.as_p }}
{% csrf_token %}
<input type="submit" name="" value="Change">
</form>
</div>
{% endblock %}
password_change_done.html:
{% block body %}
<h1>Password Changed Successfully</h1>
{% endblock %}
I can navigate to /password_change and see the form, but when I fill it in and submit it in I am redirected to /login (I have made sure I was already logged in) and the password does not change. Does anyone know what the issue is?
This is because you specified {% url 'login' %} for the action="…" parameter, you need to change it with:
{% block body %}
<h1>Change your password</h1>
<p>Please use the following form to change your password:</p>
<div class = 'password-change-form'>
<form class="" action="{% url 'password_change' %}" method="post">
{{ form.as_p }}
{% csrf_token %}
<input type="submit" name="" value="Change">
</form>
</div>
{% endblock %}

why I can't get the PasswordResetDoneView page?

when I submit my Email form from PasswordResetView page the email appears via URL as following: http://127.0.0.1:8000/account/password-change/?email=medoabdin%40gmail.com and not directing me to PasswordResetDoneView page and I get no error.
how can I show PasswordResetDoneView message page
urls.py
from django.urls import path
from . import views
from django.contrib.auth.views import (
LoginView,
LogoutView,
PasswordResetView,
PasswordResetDoneView)
from django.urls import reverse_lazy
app_name = 'account'
urlpatterns = [
# /account/login/
path('login/', LoginView.as_view(template_name='account/login.html'), name='login'),
# /account/logout/
path('logout/', LogoutView.as_view(template_name='account/logout.html'), name='logout'),
# /account/register/
path('register/', views.register, name='register'),
# /account/view-profile/
path('view-profile/', views.view_profile, name='view_profile'),
# /account/password-change/
path('password-change/', PasswordResetView.as_view(template_name='account/password_change_view.html', success_url=reverse_lazy('account:password_change_done'), email_template_name='account/reset_password_email.html'), name='password_change'),
# /account/password-change/done/
path('password-chane/done/', PasswordResetDoneView.as_view(template_name='account/password_change_done.html'), name='password_change_done'),
]
password_change_done.html
{% extends 'base.html' %}
{% block title %} Success Message {% endblock %}
{% block body %}
{% if not user.is_authenticated %}
<div class="password-change-done">
<div class="container">
<h1 class="text-primary"> The Password Has Been Sent </h1>
<p class="lead text-success"> Check your email and following the rest of record until you can go back your own email. </p>
<p class="lead text-success"> You need to follow the instructions to get what you want. </p>
</div>
</div>
{% endif %}
{% endblock %}
password_change_view.html
{% extends 'base.html' %}
{% block title %} Chnage your Passowrd {% endblock %}
{% block body %}
{% if not user.is_authenticated %}
<div class="password-change">
<div class="container">
<div class="my-form">
<form class="post">
{{ form.as_p }}
<button type="submit" class="btn btn-success">Go</button>
</form>
</div>
</div>
</div>
{% endif %}
{% endblock %}
settings.py
urlpatterns = [
path('', include('index.urls')),
path('account/', include('account.urls')),
path('admin/', admin.site.urls),
path('', include('django.contrib.auth.urls')),
]
Put method="post" in form tag and also {% csrf_token %}, probably because of GET method same view is rendering and form_valid is not invoking.

NoReverseMatch at /; error at line 0

I'm getting the below error and I have no idea why... I swear everything was working fine yesterday... I'm using python 3.5 and django 1.9. I went through most of the NOReverseMatch posts, but I couldn't find anything... please help
"Reverse for 'xxx' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []"
and
In template F:\Django-tutorial\src\landing_pages\templates\landing_pages\home.html, error at line 0
home.html should be displayed as a main page
My projects url.py:
urlpatterns = [
url(r'^', include('landing_pages.urls')),
url(r'^admin/', admin.site.urls),
url(r'^in/', include('web_monitor.urls')),
]
landing_pages.urls:
from django.conf.urls import url
from landing_pages.views import home, contact, about, cost
urlpatterns = [
url(r'^$', home, name='home'),
url(r'^contact/$', contact, name='contact'),
url(r'^about/$', about, name='about'),
url(r'^cost/$', cost, name='cost'),
]
Home.html:
{% extends 'landing_pages/base.html' %}
{% load crispy_forms_tags %}
{% block jumbotron %}
<!-- Main component for a primary marketing message or call to action -->
<div class="jumbotron">
{% endblock %}
{% block content %}
<div class="row">
<div class="col-md-3">
<form method="POST" action="">
{% csrf_token %}
{{ form|crispy }}
<input class="btn btn-primary" type="submit" value="Submit">
</form>
</div>
</div>
{% endblock %}
base.html:
{% load staticfiles %}
<html lang="en">
<head>
{% include 'landing_pages/head_css.html' %}
</head>
<body>
{% include 'landing_pages/nav_bar.html' %}
<div class="container">
{% block jumbotron %}
{% endblock %}
{% block content %}
{% endblock %}
</div> <!-- /container -->
</body>
</html>
nav_bar.html is just a nav bar, where i reference links like this:
<li>Cost</li>

Django Login App Tutorial problems

I am having problems running through a tutorial and it seems the problems stem from this:
(r'^l/login/$', 'django.contrib.auth.views.login'),
It seems I have done all correct, but the forms dont show.
If I hit Login. I get back to the same page without forms.
Did I miss something?
Here the code:
urls.py:
from django.conf.urls.defaults import *
from formsapp.views import *
from login.views import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
#Forms1:
(r'^$', main_page),
#Forms2
(r'^register/', main_page1),
#FormsLogin:
(r'^l/', login_main_page),
(r'^l/login/$', 'django.contrib.auth.views.login'),
(r'^l/logout/$', 'logout_page'),
views.py:
from django.http import HttpResponse, Http404, HttpResponseRedirect
from django.contrib.auth import logout
from django.contrib.auth.models import User
from django.template import RequestContext
from django.shortcuts import render_to_response
def login_main_page(request):
return render_to_response('mainpage.html', RequestContext(request))
def logout_page(request):
logout(request)
return HttpResponseRedirect('/l/')
base.html:
<html>
<head>
<title> {% block title %}{% endblock %}</title>
</head>
<body>
<h1>{% block head %}{% endblock %}</h1>
{% block content %}{% endblock %}
<br>
<br>
<br>
Login Main Page
{% if user.is_authenticated %}
Log out
{% else %}
Log in
{% endif %}
</body>
</html>
login.html:
{% extends "base.html" %}
{% block title %}Log in{% endblock %}
{% block head %}Log in{% endblock %}
{% block content %}
{% if form.has_errors %}
<p> Username or password didn't work. Please enter them again </p>
{% endif %}
<form method="post" action=".">
<p><label for="id_username">Username:
</label>{{ form.username }}</p>
<p><label for="id_password">Password:
</label>{{ form.password }}</p>
<input type="hidden" name="next"
value="/l/" />
<input type="submit" value="Log in" />
</form>
{% endblock %}
URL pattern
(r'^l/', login_main_page),
will match any URL starting with l/ and it is placed before l/login so second one won't be called, you should terminate regexp with $:
(r'^l/$', login_main_page),