简体   繁体   English

个人资料照片未更新 Django 3

[英]Profile photo is not updated Django 3

My problem is the following, I am creating users with the default Django model, creating a user automatically creates a profile associated with this user, with the date of birth and profile photo fields.我的问题如下,我正在使用默认的 Django model 创建用户,创建用户会自动创建与该用户关联的个人资料,其中包含出生日期和个人资料照片字段。 A profile photo is assigned by default, when I want to change it it is not updated, although the rest of the fields do, I have searched for solutions but none has helped me.默认情况下分配了个人资料照片,当我想更改它时它不会更新,尽管字段的 rest 可以,但我已经搜索了解决方案,但没有一个对我有帮助。 Here the code:这里的代码:

forms.py forms.py

class ProfileForm(forms.ModelForm):

    birth_date = forms.DateField(label='Fecha de nacimiento')
    photo = forms.ImageField(label="Foto de perfil")

    class Meta:
        model = Profile
        fields = ['birth_date', 'photo']

views,py视图,py

def editProfile(request):
    if request.method == 'POST':
        form = UEditF(request.POST, instance=request.user)
        extended_profile_form = ProfileForm(request.POST, request.FILES, instance=request.user.profile)        
        if form.is_valid() and extended_profile_form.is_valid():
            form.save()
            extended_profile_form.save()
            return redirect('/')
    else:
        form = UEditF(instance=request.user)
        extended_profile_form = ProfileForm(instance=request.user.profile)

    context = {
            'form': form,
            'extended_profile_form':extended_profile_form
    }

    return render(request, 'registration/edit_profile.html', context)

edit_profile.html edit_profile.html

{% extends "base_generic.html" %}
{% block content %}
{% load crispy_forms_tags %}
<div class="container-fluid">
    <div class="row justify-content-center">
        <h1
            style="padding-top: 0.5em; font-size: 4em; font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;">
            Editar el perfil del usuario {{ user.username }}</h1>
        <div class="container" >
            <div class="row justify-content-center">
                <div class="col-6" style="background-color: #ffffff; padding: 2em; border-radius: 5%;">
                    <form method="POST" action="" novalidate>
                        {% csrf_token %}
                        {{ form|crispy}}
                        {{ extended_profile_form|crispy}}
                        
                        <div class="text-center">
                            <button class="btn btn-lg btn-primary text-center" type="submit">Actualizar Perfil</button>
                            <p class="mt-5 mb-3 text-muted">&copy; Read Praxis Project</p>
                        </div>
                    </form>
                </div>
            </div>
        </div>

    </div>
</div>

{% endblock %}

At the code level it does not show an error, but by console this comes out:在代码级别它不会显示错误,但通过控制台会出现:

Not Found: /polls/edit/media/IMG_20200123_192312.jpg
[06/Jul/2020 02:41:17] "GET /polls/edit/media/IMG_20200123_192312.jpg HTTP/1.1" 404 2846

I really don't understand the problem, I'm new to using Django, if someone can help me it would be great and I would really appreciate it.我真的不明白这个问题,我是使用 Django 的新手,如果有人可以帮助我,那就太好了,我将不胜感激。

and add enctype="multipart/form-data" in form tag:并在表单标签中添加enctype="multipart/form-data"

<form method="POST" enctype="multipart/form-data" action="" novalidate>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM