改变我们

2024-05-13 01:07:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我坚持做同样的任务。我有一个视图更改用户注册数据(用户名、职业等),但我不知道如何更改用户头像(下载和设置)。在

编辑用户数据的视图:

def edit_personal_information(request):
if request.method == "POST" :
    username = request.POST["username"]
    profession = request.POST["search_specialist"]
    coordinate_x = request.POST["coordinate_x"]
    coordinate_y = request.POST["coordinate_y"]
    profile_vkontakte = request.POST["profile_vkontakte"]
    request.user.username = username
    request.user.profession = profession
    request.user.coordinate_x = coordinate_x
    request.user.coordinate_y = coordinate_y
    request.user.profile_vkontakte = profile_vkontakte
    request.user.save()
    return redirect("/personal_information")
c = {}
c.update(csrf(request))
return render(request, "edit_personal_information.html"

用户窗体

^{pr2}$

有什么想法吗?伙计们,救命。在


Tags: 数据用户视图coordinatereturninformationrequestusername
1条回答
网友
1楼 · 发布于 2024-05-13 01:07:52

在模板中,将其添加到表单标记中

enctype="multipart/form-data"

创建一个名为user_profile的新模型

^{pr2}$

现在在这个字段中保存用户的图像请求.用户.保存()

from django.core.files import File
new_user_profile = user_profile()
new_user_profile.user = request.user
new_user_profile.avatar.save(avatars_file_name,File(handle_upload_file(request.FILES['your_file_input_name_in_template_form'])


from django.core.files.temp import NamedTemporaryFile
def handle_upload_file(f):
        img_temp = NamedTemporaryFile()
        for chunk in f.chunks():
                img_temp.write(chunk)
        img_temp.flush()
        return img_temp

你也可以参考这个问题来获得另一种方法和想法Add image/avatar to users in django

相关问题 更多 >