重定向用户到主页django的问题



当我尝试登录时,它不会重新引导我到主页。相反,它显示了一个错误url应该是http://127.0.0.1:8000/它显示http://127.0.0.1:8000/login我尝试使用函数和路径名

urls.py

app_name = "accounts"
urlpatterns = [
path('', views.home,name="home"),
path('register/',views.register, name='register'),
path('login/',views.loginPage, name='login')]

视图.py

def loginPage(request):
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(request,username=username, password=password)
if user is not None:
login(request,user)
return redirect('home')
return render(request,'accounts/login.html')

错误

NoReverseMatch at /login/
Reverse for 'home' not found. 'home' is not a valid view function or pattern name.
Request Method: POST
Request URL:    http://127.0.0.1:8000/login/
Django Version: 3.0.4
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'home' not found. 'home' is not a valid view function or pattern name.
Exception Location: C:UsersMahmoud IshagAppDataLocalProgramsPythonPython37libsite-packagesdjangourlsresolvers.py in _reverse_with_prefix, line 677

在views.py中,url名称的工作方式与在模板中不同。

您可以尝试将return redirect('home')更改为return redirect('')return redirect('/'),具体取决于您在path('', views.home, name="home")中定义URL的方式。

最新更新