我成功注册了用户,但当ot将我重定向到登录页面时,我100%确信密码是正确的,但它并没有让我登录,所以我检查了该特定用户的管理页面,用户名存在,但它说密码还没有创建。
这是我的观点.py
#from selectors import EpollSelector
from django.shortcuts import render, redirect
from django.contrib import messages
from django.http import HttpResponseRedirect
from .forms import UserRegisterForm, UserUpdateForm, ProfileUpdateForm
from django.contrib.auth.decorators import login_required
# Create your views here
def register(request):
if request.method == "POST":
form = UserRegisterForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get("username")
messages.success(request, f'You are now able to log in!')
return redirect('login')
else:
form = UserRegisterForm()
return render(request, 'Users/register.html', {'form': form})
@login_required
def profile(request):
if request.method == "POST":
u_form = UserUpdateForm(request.POST, instance = request.user)
p_form = ProfileUpdateForm(request.POST,
request.FILES,
instance=request.user.profile)
if u_form.is_valid() and p_form.is_valid():
u_form.save()
p_form.save()
messages.success(request, f'You account has been updated!')
return redirect('profile')
else:
u_form = UserUpdateForm(instance = request.user)
p_form = ProfileUpdateForm(instance=request.user.profile)
context = {
'u_form' :u_form,
'p_form' :p_form
}
return render(request, 'Users/profile.html', context)
这是我的表格。py代码
from socket import fromshare
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserChangeForm
from .models import Profile
class UserRegisterForm(UserChangeForm):
email = forms.EmailField()
password1 = forms.CharField(label = "Password", widget = forms.PasswordInput)
password2 = forms.CharField(label = "Repeat password", widget = forms.PasswordInput)
class Meta:
model = User
fields = ['username', 'email', 'password1', 'password2']
class UserUpdateForm(forms.ModelForm):
email = forms.EmailField()
class Meta:
model = User
fields = ['username', 'email']
class ProfileUpdateForm(forms.ModelForm):
class Meta:
model = Profile
fields = ['image']
请提出一些解决方案。我在学习科里·谢弗的教程。
尝试阅读django.contrib.auth和密码。https://docs.djangoproject.com/en/4.1/topics/auth/default/#changing-密码
Django不会将密码更改为hash,您应该在user.save((之前自己更改
在您的情况下:
... # your staff
form = UserRegisterForm(request.POST)
if form.is_valid():
user = form.save(commit=False)
user.set_password(form.cleaned_data['password'])
user.save()
username = user.username
messages.success(request, f'You are now able to log in!')
... # other staff