我正在尝试在django中扩展用户个人资料,以便用户可以添加个人资料照片和生日日期,我正在使用django-allauth进行用户身份验证。我目前正在关注一个参考,但是为了参考,使用了新的用户注册而没有涉及django-allauth。因此,我已经实现了代码,但在一个点上存货,我无法确定将这一特定代码行放在何处
# Create the user profile
profile = Profile.objects.create(user=new_user)
下面是配置文件编辑代码 的模型
class Profile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL)
date_of_birth = models.DateField(blank=True, null=True)
photo = models.ImageField(
upload_to= upload_location,
null = True,
blank = True,
height_field = "height_field",
width_field = "width_field")
def __str__(self):
return 'Profile for user {}'.format(self.user.username)
Form.py
class UserEditForm(forms.ModelForm):
username = forms.CharField(required=True)
email = forms.EmailField(required=True)
first_name = forms.CharField(required=False)
last_name = forms.CharField(required=False)
class Meta:
model = User
fields = ['first_name', 'last_name', 'username', 'email']
class ProfileEditForm(forms.ModelForm):
"""docstring for ProfileEditForm."""
class Meta:
model = Profile
fields = ['date_of_birth', 'photo']
查看
def edit(request):
if request.method == 'POST':
user_form = UserEditForm(instance = request.user, data = request.POST)
profile_form = ProfileEditForm(instance = request.user.profile, data = request.POST, files = request.FILES)
if user_form.is_valid() and profile_form.is_valid():
user_form.save()
profile_form.save()
else:
user_form = UserEditForm(instance= request.user)
profile_form = ProfileEditForm(instance=request.user.profile)
return render(request, 'account/edit.html',
{'user_form': user_form, 'profile_form': profile_form})
如果我尝试运行代码,我会收到错误User has no profile.
,根据请求会提供更多代码。
网址
url(r'^profile/edit/$', views.edit, name='update_profile'),
模板
<form method="POST" action="." class="" enctype="multipart/form-data"/>
{% csrf_token %}
{{ user_form.as_p }}
{{ profile_form.as_p }}
<input type="submit" name="submit" value="update">
</form>
答案 0 :(得分:1)
在您的视图中,添加(或添加到其他地方,然后导入):
def load_profile(user):
try:
return user.profile
except: # this is not great, but trying to keep it simple
profile = Profile.objects.create(user=user)
return profile
然后更改您的查看功能,使用load_profile(request.user)
代替request.user.profile
。
查看强>
def edit(request):
profile = load_profile(request.user)
if request.method == 'POST':
user_form = UserEditForm(
instance=request.user,
data=request.POST,
)
profile_form = ProfileEditForm(
instance=profile,
data=request.POST,
files=request.FILES,
)
if user_form.is_valid() and profile_form.is_valid():
user_form.save()
profile_form.save()
else:
user_form = UserEditForm(instance=request.user)
profile_form = ProfileEditForm(instance=profile)
return render(
request,
'account/edit.html',
{'user_form': user_form, 'profile_form': profile_form}
)
答案 1 :(得分:1)
由于您的错误告诉用户个人资料不存在:
在view
:
def edit(request):
if request.method == 'POST':
profile_form = ProfileEditForm(request.POST, request.FILES)
if profile_form.is_valid():
instance = profile_form.save(commit=False)
instance.user = request.user
instance.save()
return # add here
else:
profile_form = ProfileEditForm()
return render(request, 'account/edit.html', {'profile_form': profile_form})
它会起作用。