Django:通过表单更改用户配置文件
我正在使用 UserProfile 保存我创建的一些字段。它工作正常。但我想创建一个新视图来让用户更改(更新)这些字段值。但是,这些价值观并没有以形式表现出来。有人知道如何修复它吗?
view.py
@login_required
def atualizar_cadastro_usuario(request):
if request.method == 'POST':
form = cadastrousuarioForm(request.POST,instance=request.user.get_profile())
if form.is_valid():
new_user = form.save()
return render_to_response("registration/cadastro_concluido.html",{})
else:
form = cadastrousuarioForm(request.POST,instance=request.user.get_profile())
return render_to_response("registration/registration.html", {'form': form})
form.py
class cadastrousuarioForm(UserCreationForm):
username = forms.EmailField(label = "Email",widget=forms.TextInput(attrs={'size':'60','maxlength':'75'}))
email = forms.EmailField(label = "Digite o Email novamente",widget=forms.TextInput(attrs={'size':'60','maxlength':'75'}))
nome = forms.CharField(label = 'Nome',widget=forms.TextInput(attrs={'size':'30','maxlength':'100'}))
cpf = BRCPFField(label='CPF')
data_nascimento=forms.DateField(widget=forms.DateInput(format = '%d/%m/%Y'), input_formats=('%d/%m/%Y',))
endereco = forms.CharField(label = 'Endereço',widget=forms.TextInput(attrs={'size':'30','maxlength':'100'}))
cidade = forms.CharField(label = 'Cidade')
estado = forms.CharField(widget=BRStateSelect(), label='Estado', initial = 'SP')
telefone = forms.CharField(label = "Telefone",widget=forms.TextInput(attrs={'size':'12','maxlength':'12'}))
escolaridade = forms.ChoiceField(choices=UserProfile.ESCOLARIDADE_ESCOLHAS)
profissao = forms.CharField(label = 'Profissão')
empresa = forms.CharField(label = 'Empresa',required=False)
receber_emails = forms.ChoiceField(choices=UserProfile.QUESTIONARIO_ESCOLHAS)
#captcha = CaptchaField(label = 'Digite as letras a seguir')
class Meta:
model = User
fields = ("username","email")
def save(self, commit=True):
user = super(cadastrousuarioForm, self).save(commit=False)
...
在 bash 中,它工作正常:
>>> from django.contrib.auth.models import User
>>> from cadastro.models import UserProfile
>>> u = User.objects.get(username='[email protected]')
>>> u.get_profile().telefone
u'123123455'
I am using UserProfile to save some fields that I created. Its working fine. But I would like to create a new view to let user change (update) theses fields values. But, theses values arent being showing in form. Anyone have any idea how to fix it?
view.py
@login_required
def atualizar_cadastro_usuario(request):
if request.method == 'POST':
form = cadastrousuarioForm(request.POST,instance=request.user.get_profile())
if form.is_valid():
new_user = form.save()
return render_to_response("registration/cadastro_concluido.html",{})
else:
form = cadastrousuarioForm(request.POST,instance=request.user.get_profile())
return render_to_response("registration/registration.html", {'form': form})
form.py
class cadastrousuarioForm(UserCreationForm):
username = forms.EmailField(label = "Email",widget=forms.TextInput(attrs={'size':'60','maxlength':'75'}))
email = forms.EmailField(label = "Digite o Email novamente",widget=forms.TextInput(attrs={'size':'60','maxlength':'75'}))
nome = forms.CharField(label = 'Nome',widget=forms.TextInput(attrs={'size':'30','maxlength':'100'}))
cpf = BRCPFField(label='CPF')
data_nascimento=forms.DateField(widget=forms.DateInput(format = '%d/%m/%Y'), input_formats=('%d/%m/%Y',))
endereco = forms.CharField(label = 'Endereço',widget=forms.TextInput(attrs={'size':'30','maxlength':'100'}))
cidade = forms.CharField(label = 'Cidade')
estado = forms.CharField(widget=BRStateSelect(), label='Estado', initial = 'SP')
telefone = forms.CharField(label = "Telefone",widget=forms.TextInput(attrs={'size':'12','maxlength':'12'}))
escolaridade = forms.ChoiceField(choices=UserProfile.ESCOLARIDADE_ESCOLHAS)
profissao = forms.CharField(label = 'Profissão')
empresa = forms.CharField(label = 'Empresa',required=False)
receber_emails = forms.ChoiceField(choices=UserProfile.QUESTIONARIO_ESCOLHAS)
#captcha = CaptchaField(label = 'Digite as letras a seguir')
class Meta:
model = User
fields = ("username","email")
def save(self, commit=True):
user = super(cadastrousuarioForm, self).save(commit=False)
...
In bash, it works fine:
>>> from django.contrib.auth.models import User
>>> from cadastro.models import UserProfile
>>> u = User.objects.get(username='[email protected]')
>>> u.get_profile().telefone
u'123123455'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将表单中的模型从
User
更改为UserProfile
。该表单还需要子类化ModelForm
而不是UserCreationForm
。此外,您似乎只使用了 ModelForm 中的部分字段(用户名
和电子邮件
)。最后,由于您使用的是 ModelForm,因此无需在表单中定义模型的所有字段。尝试将您的cadastrousuarioForm
更改为以下内容:You need to change your model in the form from
User
toUserProfile
. The form also needs to subclassModelForm
rather thanUserCreationForm
. Also, it looks like you're only using a subset of fields in your ModelForm (username
andemail
). Finally, since you're using a ModelForm you don't need to define all of the model's fields in your form. Try changing yourcadastrousuarioForm
to the following: