错误:NoReverseMatch:找不到“索引”的反向 “索引”不是有效的视图函数或模式名称

问题描述

我正在用Django开发网络系统,当我编写登录功能时出现此错误

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

我检查了一百次代码,但是我缺少了一些东西,找不到解决方案。我已经坚持了一段时间,似乎无法解决错误。 该网站的其他部分工作正常。

views.py

@login_required
def index(request):
    return render(request,'dashboard.html')

def loginPage(request):
    form = AuthenticationForm()
    if request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')

        user = authenticate(username=username,password=password)
        
        if user is not None:
            login(request,user)
            if request.GET.get('next'):
                return redirect(request.GET.get('next'))
            else:
                return redirect('index')

    return render(request,'login.html',{'form': form})

urls.py:

urlpatterns = [
    path('login/',views.loginPage,name="login"),path('logout/',views.logoutUser,name="logout"),path('',views.index,name="index"),]

login.html

<body>
    <div class="container h-100">
        <div class="d-flex justify-content-center h-100">
            <div class="user_card">
                <div class="d-flex justify-content-center">


                    <h3 id="form-title">Login</h3>
                </div>
                <div class="d-flex justify-content-center form_container">
                    <form method="POST" action="">
                        {% csrf_token %}
                        {{form.as_p}}
                            <div class="d-flex justify-content-center mt-3 login_container">
                                <input class="btn login_btn" type="submit" value="Login">
                            </div>
                    </form>

                </div>

</body>

dashboard.html

{%  extends 'main.html' %}

{% block content %}

<br>


<div class="row">
    <div class="col-md-10">
        <h5>Patients:</h5>
        <hr>
        <a class="btn btn-sm" href="">+ Create Patient</a>

        <div class="card card-body">
            <table class="table table-sm">
                <tr>
                    <th></th>
                    <th>Patient</th>
                    <th>E-mail</th>
                    <th>ID</th>
                    <th>Language</th>
                    <th>Comment</th>
                    <th>Remove</th>
                </tr>

            </table>

        </div>

    </div>

</div>

{% endblock %}

有人可以看到我所缺少的吗?

解决方法

我想您应该更改此行

    return redirect('index')

    return redirect('APPNAME:index')

参考