Python 如何解决Django auth中的重定向不起作用
这是我的密码。一切正常,但当我尝试登录并点击登录按钮时,什么也没发生。我的登录页面没有在任何页面上重定向我。 登录页面代码:Python 如何解决Django auth中的重定向不起作用,python,django,authentication,Python,Django,Authentication,这是我的密码。一切正常,但当我尝试登录并点击登录按钮时,什么也没发生。我的登录页面没有在任何页面上重定向我。 登录页面代码: {% extends 'authenticate/base.html' %} {% block content%} <h1 class="text-center">Login </h1> <div class="col-md-6 offset-md-3" <form method="POST"> {% csrf_toke
{% extends 'authenticate/base.html' %}
{% block content%}
<h1 class="text-center">Login </h1>
<div class="col-md-6 offset-md-3"
<form method="POST">
{% csrf_token %}
<div class="form-group">
<input type="text" class="form-control" placeholder="Enter UserName"
name="username">
</div>
<div class="form-group">
<input type="password" class="form-control" placeholder="Password"
name="password">
</div>
<button type="submit" class="btn btn-secondary">Login</button>
</form>
</div>
{% endblock %}`
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login, logout
def home(request):
return render(request, 'authenticate/home.html',{})
def login_user(request):
if request.method == 'POST':
username = request.POST['username']
password = request.POST['password']
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect('home')
else:
return redirect('login')
else:
return render(request, 'authenticate/login.html', {})
Base.py页面代码:
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link" href="{%url 'login'%}">Login</a>
</li>
</ul>
</div>
-
尝试将href=“{%url”登录“%”替换为href=“{%url”登录“%”?下一步={{request.path}“什么都没有发生”是指页面没有发送post请求,还是页面在身份验证后加载到同一登录页面?hello mohit thanx for Answers我是否应该从代码中删除这些行?其他:返回重定向(“登录”)否则:删除该行后,我在/login/处出现未绑定本地错误,请修复indentation@RashidChaudharysame错误。现在,当我在主页上单击“登录”按钮时,我得到了未绑定的本地错误/登录/
def login_user(request):
form = LoginForm(request.POST)
if request.method == 'POST':
username = request.POST['username']
password = request.POST['password']
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect('home')
return render(request, 'authenticate/login.html', {'form':form})