NoReverseMatch at / .Reverse for 'login' not found. 'login' is not a valid view function or pattern name.

Hello, I’m building a website using Django and I keep get this error. Here is my urls.py fil:

from django.urls import path, include
from  . import views
import sys

app_name = "myapp"

urlpatterns = [
    path('', views.index, name='index'),
    path('register', views.register, name='register'),
    path('login', views.login, name='login'),
    path('logout', views.logout, name='logout'),
    path('wordsearch', views.wordsearch, name='wordsearch'),
    path('generate', views.generate, name="generate")
] 

My views.py file:

from django.shortcuts import render, redirect # redirect: chuyen huong user toi page khac
from django.contrib.auth.models import User, auth
from django.contrib import messages
from django.http import HttpResponse
from .models import Feature
from myproject.main import *
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm

# Create your views here.
# gửi request tới thư mục templates để tìm file index.html
def index(request):
    # dictionary 
    return render(request, 'login.html')


# log in and sign up function
def register(request):  
    # check if the page is rendered with a post method
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            form.save()
            # log the user in
            return redirect('login')
    else: 
        form = UserCreationForm()
    return render(request, 'register.html', {'form': form})


def login(request):
    if request.method == "POST":
        form = AuthenticationForm(data = request.POST)
        if form.is_valid():
            return redirect('wordsearch')
    else:
        form = AuthenticationForm()
    return render(request, 'login.html', {'form': form})

# log out
def logout(request):
    auth.logout(request) # log all user out of platform
    return redirect('/')

def wordsearch(request):
    return render(request, 'wordsearch.html')
    

and my login.html

<h1> Login Now </h1>

{% for message in messages %}
<h3>{{message}}</h3>
{% endfor %}
<form action="{% url 'login' %}" method="POST">
    {% csrf_token %}
    {{ form }}
    <input type="submit" value="Login">
</form>

Can someone please tell me where the problem is? Thank you

So this:

defines a namespace for these URLs.

That means you need to reference these URLs specifying that namespace.

In this case, then it would be myapp:login.