Error: NoReverseMatch: Reverse for 'index' not found. 'index' is not a valid view function or pattern name
Error: NoReverseMatch: Reverse for 'index' not found. 'index' is not a valid view function or pattern name
我在 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')
参考
我在 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')
参考