NoReverseMatch at /account/login/

試著忘記壹切 提交于 2021-01-07 03:08:58

问题


I'm trying to figure out Django default auth and when I log in with correct credentials it give me this error

NoReverseMatch at /account/login/
Reverse for 'dashboard' not found. 'dashboard' is not a valid view function or pattern name.
Request Method: POST
Request URL:    http://127.0.0.1:8000/account/login/
Django Version: 2.2
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'dashboard' not found. 'dashboard' is not a valid view function or pattern name.
Exception Location: C:\Users\nouma\Desktop\social_website\myenv\lib\site-packages\django\urls\resolvers.py in _reverse_with_prefix, line 660

I've seen some similar problems but couldn't understand what exactly my problem is. I'm confused with the concept of reverse not found. Here's my views.py

from django.shortcuts import render
from django.http import HttpResponse
from django.contrib.auth import authenticate, login
from .forms import LoginForm
from django.contrib.auth.decorators import login_required
# Create your views here.

def user_login(request):
    if request.method == 'POST':
        form = LoginForm(data=request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            user = authenticate(request, username=cd['username'], password=cd['password'])

            if user is not None:
                if user.is_active:
                    login(request, user)
                    return HttpResponse('Authenticated successfully')
                else:
                    return HttpResponse('Disabled account')
            else:
                return HttpResponse('Invalid login')
    else:
        form = LoginForm()
    return render(request, 'account/login.html', {'form': form})

@login_required
def dashboard(request):
    return render(request, 'account/dashboard.html', {'section': 'dashboard'})

here's my main urls.py of the project

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('account/', include('django.contrib.auth.urls')),
    path('account/', include('account.urls', namespace='account')),
]

urls.py of app named account

from django.urls import path
from . import views
from django.contrib.auth import views as auth_views

app_name = 'account'

urlpatterns = [
    # login view
    # path('login/', views.user_login, name='user_login'),

    path('login/', auth_views.LoginView.as_view(), name='login'),
    path('logout/', auth_views.LogoutView.as_view(), name='logout'),
    path('', views.dashboard, name='dashboard'),
]

My dasboard.html

{% extends 'base.html' %}

{% block title %}Dashboard{% endblock %}

{% block content %}
  <h1>Dashboard</h1>
  <p>Welcome to your dashboard</p>
{% endblock %}

login.html

{% extends "base.html" %}

{% block title %}Log-in{% endblock %}

{% block content %}
  <h1>Log-in</h1>
  {% if form.errors %}
    <p>Your username or password didn't match. Please try again</p>
  {% else %}
    <p>Please, use the following form to log-in:</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 %}

I think the problem is minor but I'm unable to configure.


回答1:


You have created a custom application account for which you do not have defined url in the app/urls.py file.

You can add account urls like

urlpatterns = [
  path('account/', include('django.contrib.auth.urls')),
  path('account/', include('account.urls', namespace='account'))
]

Now you can create reverse urls for the urls defined in the account app.

Edit 2

Also since you have defined the django.contrib.auth views in the account/urls.py file, you do not need to add the url for django.contrib.auth.urls in the app/urls.py file.

LOGIN_REDIRECT_URL

As per the documentation, It accepts path or named url pattern.

The suggest way is the use of path. For named usages you may try with the following

LOGIN_REDIRECT_URL = 'account:login'
LOGIN_REDIRECT_URL = reverse('account:login')

I'm not sure which one of the above will work, let me know which one worked for you.



来源:https://stackoverflow.com/questions/59864943/noreversematch-at-account-login

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!