Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

196
Visualizações
How to display the edited profile picture in django

I am able to upload the profile picture but i am not able edit it

I have the form populating with the image browse option, but when I click save, it doesn't actually update in template. but it is working fine in admin

views.py

def create_form(request):
        form = AlbumForm(request.POST or None, request.FILES or None)
        if form.is_valid():
            album = form.save(commit=False)
            album.user = request.user
            album.image= request.FILES['image']
            album.save()
            return render(request, 'about.html', {'album': album})
        context = {
            "form": form,
        }
        return render(request, 'create_form.html', context)

def profile_edit(request):
    if request.method == 'POST':
        form = AlbumForm(request.POST, instance=request.user.album)
        if form.is_valid():
            album = form.save(commit=False)
            album.user = request.user
            form.save()
            return redirect(reverse('about'))
    else:
        form = AlbumForm(instance=request.user.album)
        args = {'form': form}
        return render(request, 'profile_edit.html', args)

models.py

class Album(models.Model):
    user = models.OneToOneField(User)
    image = models.FileField()

forms.py

class AlbumForm(forms.ModelForm):
    class Meta:
        model = Album
        fields = ['image']

profile_edit.html

{%  extends 'base.html' %}
{%  block   content %}

<form action="{% url 'profile_edit' %}" method="post" enctype="multipart/form-data">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit" class="save btn btn-default">save</button>
</form>
<div>
      <img src="{{ user.album.image.url }}" class="img-responsive">
</div>

{%  endblock    %}
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Try this

AlbumForm(request.POST, request.FILES, instance=request.user.album)
over 4 years ago · Santiago Trujillo Relatório

0

You need to add request.FILES to your AlbumForm in the profile edit view.

over 4 years ago · Santiago Trujillo Relatório

0

If you upload file you must use request.Files read for more info this link

https://docs.djangoproject.com/en/1.10/topics/http/file-uploads/#basic-file-uploads

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda