英文:
Using username for password in Django
问题
当注册用户时,密码应与用户名相同,用户应稍后更改密码。
视图:
@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, 'error')
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()
你可以使用这些代码创建一个视图、表单和模型,以注册用户,其中密码与用户名相同,并允许稍后更改密码。
英文:
When registering a user, the password should be the same as the username, and the user should change the password later
> view :
@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)
> forms :
class CreateUserForm(UserCreationForm):
class Meta:
model = Profile
fields = [
'username',
'codemeli',
'first_name',
'last_name',
]
> models :
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()
what can i do ؟؟؟؟
答案1
得分: 1
Sure, here's the translated code part without the need for further translation:
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()
英文:
> view :
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()
答案2
得分: 0
使用密码功能:
user.set_password(username)
英文:
use the function for password
user.set_password(username)
答案3
得分: 0
> view :
>
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()`
英文:
> view :
>
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()`
答案4
得分: 0
@receiver(pre_save, sender=Profile)
def update_password(sender, instance, *args, **kwargs):
instance.password = instance.username
英文:
you can use pre_save signal or post_save signal. for instance,
@receiver(pre_save, sender=Profile)
def update_password(sender, instance, *args, **kwargs):
instance.password = instance.username
答案5
得分: 0
你可以尝试这个视图函数
@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, 'error')
return redirect("member_index")
else:
form = CreateUserForm()
context = {
'form': form
}
return render(request, 'member/register.html', context)
英文:
You can try this view function
@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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论