我有自己的RegisterForm。我成功注册,在DBBrowser中可以看到一个新创建的用户及其加密的密码。但是,管理面板中没有加密的密码。当我在管理面板中查看密码字段时,我只会看到“未设置密码”,而且我无法使用新创建的用户的用户名和密码登录。谢谢您的帮助。
我的代码:
views.py
from django.shortcuts import render,HttpResponse,redirect
from .forms import RegisterForm, LoginForm
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth import login,authenticate,logout
from django.contrib import messages
# Create your views here.
def register(request):
if request.method == "POST":
form = RegisterForm(request.POST)
if form.is_valid():
username = form.cleaned_data.get("username")
email = form.cleaned_data.get("email")
password = form.cleaned_data.get("password")
newUser = User(username= username, email = email)
newUser.set_password(password)
newUser.save()
messages.success(request,"Kayıt İşlemi Başarılı.")
login(request,newUser)
return redirect("index")
context = {
"form" : form,
}
return render(request,"register.html", context)
else:
form = RegisterForm() #şimdilik içi boş. Sonra GET POST yapılacak
context = {
"form" : form,
}
return render(request,"register.html", context)
def loginUser(request):
form = LoginForm(request.POST or None)
context = {"form":form}
if form.is_valid():
username = form.cleaned_data.get("username")
password = form.cleaned_data.get("password")
user = authenticate(request, username=username, password = password)
if user is not None:
messages.success(request,"Giriş Başarılı Şekilde Yapıldı.")
login(request, user)
return redirect("index")
else:
messages.info(request,"Kullanıcı Adı ya da Parola Hatalı.")
return render(request,"login.html",context)
return render(request,"login.html", context)
def logoutUser(request):
logout(request)
messages.success(request, "Çıkış Başarıyla Yapıldı.")
return redirect("index")
forms.py
from django import forms
from django.contrib import messages
from django.contrib.auth import authenticate
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
class RegisterForm(forms.Form):
username = forms.CharField(min_length=3, max_length=20, label="Kullanıcı Adı")
email = forms.EmailField(min_length = 4, label = "E-mail Adresi")
password = forms.CharField(min_length=8, max_length=16, label = "Şifre", widget = forms.PasswordInput)
confirm = forms.CharField(min_length=8, max_length=16, label = "Şifre Doğrula", widget = forms.PasswordInput)
def clean(self):
username = self.cleaned_data.get("username")
email = self.cleaned_data.get("email")
password = self.cleaned_data.get("password")
confirm = self.cleaned_data.get("confirm")
if password and confirm and password != confirm:
raise forms.ValidationError("Parolalar Eşleşmedi.")
values = {
"username" : username,
"password " : password,
"email" : email}
return values
class LoginForm(forms.Form):
username = forms.CharField(label="Kullanıcı Adı")
password = forms.CharField(label= "Parola", widget= forms.PasswordInput)