我创建了一个自定义UserProfile模型,并希望允许用户编辑他或她的个人资料的字段。其他字段是可编辑的,但ImageField不是。我能够将上传的图像存储到我的目标目录(media / profile_pics)中,但我不确定如何将其链接到当前登录的用户,以便他或她可以导航回配置文件页面并查看新图片。
我的views.py中的代码非常混乱。我希望有人可以教我一个更好的方法,因为它与部分理解一起被黑客攻击。
models.py
#custom user profile
class UserProfile(models.Model):
user = models.OneToOneField(User)
description = models.CharField(max_length=200, default='')
city = models.CharField(max_length=100, default='')
profile_pic = models.ImageField(upload_to='profile_pics', blank=True)
def __str__(self):
return self.user.username
def create_profile(sender, **kwargs):
#if user object has been created, we want to create user profile
if kwargs['created']:
#returns the instance of our user object
user_profile = UserProfile.objects.create(user=kwargs['instance'])
#connecting to the post_save signal. Pass in a function that should run
post_save.connect(create_profile, sender=User)
当用户导航到他们可以编辑其个人资料的地方时,使用视图功能“edit_profile”。它位于 views.py :
中def edit_profile(request):
user_profile = UserProfile.objects.get(user=request.user)
profile_form = UserProfileForm(instance=user_profile)
if request.method == 'POST':
update_profile_form = UserProfileForm(data=request.POST, instance=user_profile)
#so we know the user object
form = EditProfileForm(request.POST, instance=request.user)
if form.is_valid() and update_profile_form.is_valid():
user = form.save()
update_profile_form.save()
handle_uploaded_file(request, request.FILES['profile_pic'])
#return redirect(reverse('userapp:profile'))
args = {'profile_form': update_profile_form}
return render(request, 'userapp/profile.html', args)
else:
form = EditProfileForm(instance=request.user)
args = {'form': form, 'profile_form': profile_form}
return render(request, 'userapp/edit_profile.html', args)
edit_profile使用以下函数将文件保存到目标目录中。这取自Django文档:
def handle_uploaded_file(request, f):
with open(os.getcwd() + "/media/profile_pics/" + f.name, 'wb+') as destination:
for chunk in f.chunks():
destination.write(chunk)
forms.py
class UserProfileForm(forms.ModelForm):
# profile_pic = forms.ImageField()
class Meta:
model = UserProfile
fields = ('profile_pic','city', 'description',)
class EditProfileForm(UserChangeForm):
#template_name='/something/else'
class Meta:
model = User
fields = (
'email',
'first_name',
'last_name',
'password'
#'profile_pic'
)
我显示以下HTML,“edit_profile.html”供用户更新字段:
{% extends 'base.html' %}
{% block title %} Profile {% endblock %}
{% block body %}
<div class="container-fluid">
<form method="post" enctype='multipart/form-data'>
{% csrf_token %}
{{ form.as_p }}
{{ profile_form.as_p }}
<button type="submit">Submit</button>
</form>
<br>
</div>
{% endblock %}
以下是用户个人资料HTML。更新后,当用户导航回配置文件时,他们新上传的图片应显示:
{% extends 'base.html' %}
{% block title %} Profile {% endblock %}
{% block body %}
<div class="container-fluid">
<h2>Profile</h2>
<p>{{ user }}</p>
<p>First name: {{ user.first_name }}</p>
<p>Last name: {{ user.last_name }}</p>
<p>Email: {{ user.email }}</p>
<p> City: {{ profile_form.instance.city }}</p>
<p> Description: {{ profile_form.instance.description }}</p>
<p>
{% if profile_form.instance.profile_pic %}
<img src="{{ profile_form.instance.profile_pic.url}}" border-radius=10px style="width:256px;height:256px;" />
{% endif %}
</p>
</div>
{% endblock %}
答案 0 :(得分:1)
您必须在表单中传递request.FILES。见https://docs.djangoproject.com/en/1.10/topics/http/file-uploads/