Reputation: 97
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. 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. Here the code:
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
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
{% 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">© 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.
Upvotes: 1
Views: 39
Reputation: 2018
and add enctype="multipart/form-data"
in form tag:
<form method="POST" enctype="multipart/form-data" action="" novalidate>
Upvotes: 1