[英]extend a user profile in django to include profile photo
我正在尝试在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)
表格
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>
在您的视图中,添加(或添加到其他位置,然后导入):
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}
)
如您的错误所言,用户个人资料不存在:在您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})
它会工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.