При регистрации пользователя пароль должен быть таким же, как имя пользователя, и пользователь должен изменить пароль позже.
вид :
@login_required
def member_create(request):
if request.method == "POST":
form = CreateUserForm(request.POST)
if form.is_valid():
form.save()
messages.add_message(request, messages.INFO, 'ok')
return redirect("member_index")
else:
messages.add_message(request, messages.warning, 'eror')
return redirect("member_index")
else:
form = CreateUserForm()
context = {
'form': form
}
return render(request, 'member/register.html', context)
формы:
class CreateUserForm(UserCreationForm):
class Meta:
model = Profile
fields = [
'username',
'codemeli',
'first_name',
'last_name',
]
модели:
class Profile(AbstractUser):
codemeli = models.CharField(
max_length=10, verbose_name='کد ملی', unique=True)
fname_en = models.CharField(
max_length=150, verbose_name='نام لاتین', null=True, blank=True)
lname_en = models.CharField(
max_length=150, verbose_name='نام خانوادگی لاتین', null=True, blank=True)
def save(self):
self.password=self.username
super().save()
что я могу сделать ????
использовать функцию для пароля
user.set_password(username)
вид :
from django.contrib.auth.hashers import make_password
`if request.method == "POST":
form = CreateUserForm(request.POST)
username = request.POST['username']
first_name = request.POST['first_name']
last_name = request.POST['last_name']
register = Profile(
username=username,
first_name=first_name,
last_name=last_name,
)
register.password = make_password(username)
register.codemeli = username
register.save()
messages.add_message(request, messages.INFO, 'ثبت با موفقیت انجام شد')
return redirect("member_index")
else:
form = CreateUserForm()`
вы можете использовать сигнал pre_save или сигнал post_save. например,
@receiver(pre_save, sender=Profile)
def update_password(sender, instance, *args, **kwargs):
instance.password = instance.username
Вы можете попробовать эту функцию просмотра
@login_required
def member_create(request):
if request.method == "POST":
form = CreateUserForm(request.POST)
if form.is_valid():
user=form.save(commit=False)
user.password=user.username
user.save()
messages.add_message(request, messages.INFO, 'ok')
return redirect("member_index")
else:
messages.add_message(request, messages.warning, 'eror')
return redirect("member_index")
else:
form = CreateUserForm()
context = {
'form': form
}
return render(request, 'member/register.html', context)
вид :
from django.contrib.auth.hashers import make_password
if request.method == "POST":
form = CreateUserForm(request.POST)
username = request.POST['username']
first_name = request.POST['first_name']
last_name = request.POST['last_name']
register = Profile( username=username, first_name=first_name, last_name=last_name,)
register.password = make_password(username)
register.codemeli = username
register.save()
Поделитесь своей
CreateUserForm
формой.