【发布时间】:2021-05-06 13:47:16
【问题描述】:
在应用程序中创建会员记录后,只有用户名反映在管理面板中,但我也想获取姓名和姓氏。我在代码中指定了这一点,但没有反映。
views.py
from django.shortcuts import render,redirect
from .forms import RegisterForm
from django.contrib.auth.models import User
from django.contrib.auth import login
from django.contrib import messages
def register(request):
form=RegisterForm(request.POST or None)
if form.is_valid():
name=form.cleaned_data.get("name")
lastname=form.cleaned_data.get("lastname")
username=form.cleaned_data.get("username")
password=form.cleaned_data.get("password")
#newUser=User(name=name)
newUser = User(username=username)
newUser.set_password(password)
newUser.save()
login(request,newUser)
messages.info(request,"Başarıyla Kayıt Oldunuz...")
return redirect("index")
context={
"form":form
}
return render(request,"register.html",context)
Form.py
from django import forms
from django.contrib.auth.forms import UserCreationForm
class RegisterForm(forms.Form):
name=forms.CharField(max_length=50,label="Adı ")
lastname=forms.CharField(max_length=50,label="Soyadı ")
username=forms.CharField(max_length=50,label="Kullanıcı Adı ")
password=forms.CharField(max_length=20,label="Parola",widget=forms.PasswordInput)
confirm=forms.CharField(max_length=20,label="Parolayı Doğrula ",widget=forms.PasswordInput)
def clean(self):
name=self.cleaned_data.get("name")
lastname=self.cleaned_data.get("lastname")
username=self.cleaned_data.get("username")
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şmiyor...")
values = {
"name" : name,
"lastname" :lastname,
"username" : username,
"password" : password,
}
return values
虽然我指定了name和name字段,但是并没有体现在面板中
【问题讨论】:
-
您正在收集数据,但并未将其保存到用户对象中的
newUser = User(username=username)行或其下方的任何位置。 -
你知道如何保存吗?
标签: python django django-admin blogs user-profile