Python 如何实现自定义Django登录?
我试图在Django中开发一个自定义登录,而不是使用Django内置的登录系统。我不知道这是否可能。这可能吗?我想从登录模板表单中捕获值。我试过很多方法,但都不管用 这是我的后端代码Python 如何实现自定义Django登录?,python,django,Python,Django,我试图在Django中开发一个自定义登录,而不是使用Django内置的登录系统。我不知道这是否可能。这可能吗?我想从登录模板表单中捕获值。我试过很多方法,但都不管用 这是我的后端代码 def login(request): if request.method == 'GET': context = '' return render(request, 'mytest/login.html', {'context': context}) elif r
def login(request):
if request.method == 'GET':
context = ''
return render(request, 'mytest/login.html', {'context': context})
elif request.method == 'POST':
context = ''
username = request.POST.get('username', '')
password = request.POST.get('password', '')
print(username)
print(password)
return render(request, 'mytest/login.html', {'context': context})
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form method="post" action="{% url 'login' %}">
{% csrf_token %}
<label>Email</label><input type="text" name="username" required><br>
<label>Password</label><input type="password" name="password" required>
<input type="submit" value="Login">
</form>
</body>
</html>
这是我的登录模板
def login(request):
if request.method == 'GET':
context = ''
return render(request, 'mytest/login.html', {'context': context})
elif request.method == 'POST':
context = ''
username = request.POST.get('username', '')
password = request.POST.get('password', '')
print(username)
print(password)
return render(request, 'mytest/login.html', {'context': context})
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form method="post" action="{% url 'login' %}">
{% csrf_token %}
<label>Email</label><input type="text" name="username" required><br>
<label>Password</label><input type="password" name="password" required>
<input type="submit" value="Login">
</form>
</body>
</html>
标题
{%csrf_令牌%}
电子邮件
暗语
我怎样才能让它工作呢?这是可能的。
首先必须对用户进行身份验证,然后调用登录函数
from django.contrib.auth import authenticate, login
def my_view(request):
username = request.POST['username']
password = request.POST['password']
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
# Redirect to a success page.
...
else:
# Return an 'invalid login' error message.
...
在您的代码中,它将如下所示:
def login(request):
if request.method == 'GET':
context = ''
return render(request, 'mytest/login.html', {'context': context})
elif 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)
# Redirect to a success page?
# return HttpResponseRedirect('/')
else:
context = {'error': 'Wrong credintials'} # to display error?
return render(request, 'mytest/login.html', {'context': context})
两件事:
- 如果上下文为空,则可以复制上下文(渲染函数实际上只需要 前两个参数:请求和模板)
- 不要打印、调试代码 使用调试器:)
它不起作用了
说明不了什么。请把问题说得更具体一点,不清楚你期望发生什么。您的任何代码实际上都不会让用户登录。@LatikaAgarwal:关于您的编辑,软件和库名称不应以代码格式呈现。它们是专有名词,所以首字母大写(如Django)就可以了。