【发布时间】:2019-08-17 12:40:55
【问题描述】:
我是 django 的新手。我正在尝试在更新表单中获取用户数据。 这是我的代码:
models.py
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, default=1)
image = models.ImageField(upload_to='profile_images', default='default.jpg')
def __str__(self):
return f'{self.user}'
forms.py
class UpdateUserForm(forms.ModelForm):
first_name = forms.CharField(max_length=100, widget=forms.TextInput(attrs={'class': 'uk-input', 'placeholder': 'Last Name'}))
last_name = forms.CharField(max_length=100, widget=forms.TextInput(attrs={'class': 'uk-input', 'placeholder': 'Last Name'}))
class Meta:
model = User
fields = ['first_name', 'last_name']
class UpdateProfileForm(forms.ModelForm):
image = forms.ImageField(widget=forms.FileInput(attrs={'class': 'uk-button uk-button-default uk-button-medium uk-width-1-1', 'type': 'file'}))
class Meta:
model = Profile
fields = ['image']
views.py
@verified_email_required
def profile_view(request):
u_form = UpdateUserForm(request.user)
p_form = UpdateProfileForm(request.user.profile)
title = 'Profile Page'
template_name = 'profile.html'
context = {
'title': title,
'u_form': u_form,
'p_form': p_form
}
return render(request, template_name, context)
我得到这个错误: 'User' object has no attribute 'get'
【问题讨论】:
标签: django django-allauth