Error: 'login' not found. 'login' is not a valid view function or pattern name

Viewed 7001

This is the first time I am using Django login template and I ran into a problem. It keeps saying that login is not a valid view function but I define it in the urls.py in my users app:

enter image description here

enter image description here

Not sure what I am doing wrong.

main urls.py

urlpatterns = [
    path('', include('blogging_logs.urls', namespace='blogging_logs')),
    path('users/', include('users.urls', namespace='users')),
    path('admin/', admin.site.urls),
]

app: blogging_logs: base.html

<p>
  <a href="{% url 'blogging_logs:index' %}">Home</a>
  <a href="{% url 'blogging_logs:categories' %}">Categories</a>
  {% if user.is_authenticated %}
    Hello, {{ user.username }}.
    {% else %}
      <a href="{% url 'users:login' %}"> login in</a>
  {% endif %}
</p>

{% block content %}{% endblock content %}

app: users: urls.py

from django.contrib import admin
from django.urls import re_path, path
from django.contrib.auth import authenticate, login
from django.contrib.auth import views as auth_views

app_name = 'users'

urlpatterns = [
    # Login Page
    path('login/', auth_views.LoginView.as_view(template_name='login.html')),
]

app:users: login.html

{% extends "blogging_logs/base.html" %}

{% block content %}
  {% if form.errors %}
    <p>Your username and password din't match. Please try again.</p>
  {% endif %}

  <form class="" method="post" action="{% url 'users:login' %}" >
    {% csrf_token %}
    {{ form.as_p }}
    <button name='submit'> Login in </button>
    <input type="hidden" name="next" value="{% url 'blogging_logs:index' %}" />
  </form>
{% endblock content %}
1 Answers

You are missing the name argument for the path function:

Change the following line

path('login/', auth_views.LoginView.as_view(template_name='login.html')),

to

path('login/', auth_views.LoginView.as_view(template_name='login.html'), name='login'),
Related