我一直在尝试添加注册/登录/注销功能。用户创建的用户似乎运行良好,因为创建的用户显示在Django admin中并显示在数据库中。但是,每当我尝试实现登录功能时,它就永远无法工作。我认为我已将问题缩小到#Convert time to POSIXct
df[3:4] <- lapply(df[3:4], as.POSIXct)
#Get min start.time
df1 <- aggregate(start.time~species + date, df, min, na.rm = TRUE,
na.action = "na.pass")
#Get max end.time
df2 <- aggregate(end.time~species + date, df, max, na.rm = TRUE,
na.action = "na.pass")
#Get combined remarks
df3 <- aggregate(remarks~species + date, df, function(x)
toString(unique(na.omit(x))), na.action = "na.pass")
#merge into one dataset
Reduce(merge, list(df1, df2, df3))
。
我已经尝试了一切,无论我做什么,df <- structure(list(species = structure(c(1L, 1L, 1L, 1L), .Label = "A",
class = "factor"), date = structure(c(1L, 1L, 2L, 2L), .Label = c("2020-02-01",
"2020-02-02"), class = "factor"), start.time = structure(c(1L,
2L, 1L, NA), .Label = c("2020-02-018:00:00", "2020-02-019:00:00"
), class = "factor"), end.time = structure(c(2L, 3L, NA,
1L), .Label = c("2020-02-0111:30:00", "2020-02-0112:00:00",
"2020-02-0200:00:00"), class = "factor"), remarks = structure(c(2L,
NA, 1L, NA), .Label = c("medium", "slow"), class = "factor")),
class = "data.frame", row.names = c(NA, -4L))
函数始终返回authenticate()
。我提供的用户名和密码显然已存储,但似乎并不能正确获取密码,因为在authenticate()
视图中打印出None
会得到raw_password = form.cleaned_data.get('password1')
。>
如何使登录功能正常工作?
在 urls.py
中login_user
在 views.py
中None
在 register.html
urlpatterns = [
path('register', views.register_user, name='register'),
path('login', views.login_user, name='login'),
path('logout', views.logout_user, name='logout'),
]
在 login.html
中def register_user(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
raw_password = form.cleaned_data.get('password1')
user = authenticate(username=username, password=raw_password)
print(f'User is: {user}')
login(request, user)
return redirect('/')
else:
form = UserCreationForm(request.POST)
context = {
'form': form
}
return render(request, 'register.html', context)
def login_user(request):
if request.method == 'POST':
form = AuthenticationForm(request=request, data=request.POST)
if form.is_valid():
username = form.cleaned_data.get('username')
raw_password = form.cleaned_data.get('password1')
user = authenticate(username=username, password=raw_password)
print(f'User is: {user}')
print(f'username is: {username}')
print(f'password is: {raw_password}')
if user is not None:
if user.is_active:
login(request, user)
print(f'You are now logged in as {username}')
messages.info(request, f"You are now logged in as {username}")
return redirect('/')
else:
form = AuthenticationForm()
return render(request = request,
template_name = "login.html",
context={"form":form})
def logout_user(request):
logout(request)
return redirect('/')
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Registration</title>
</head>
<body>
<h2>Register</h2>
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Register</button>
</form>
</body>
</html>
和<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Log in</title>
</head>
<body>
<h2>Log in</h2>
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Log in</button>
</form>
</body>
</html>
视图工作正常。在register_user
视图中,我这样做了,因此它可以在注册后自动记录用户。奇怪的是,登录在这种情况下有效。但是,由于上述原因,登录不适用于logout_user
视图。
答案 0 :(得分:0)
raw_password = form.cleaned_data.get('password1')
,这里您必须获得password
本身而不是password1
。