trytocode
trytocode

Reputation: 403

Change profile picture in django

I'm trying to make a edit profile module , but it needs to change profile picture too.. I already try some code , but the picture isnt change when I press the submit button

Here's the views.py for update_profile

def update_profile(request):
    if request.method == 'POST':
        user_form = UserInfoForm(request.POST, instance=request.user )
        profile_form = UserProfileInfoForm(request.POST, instance=request.user.profile)
        if user_form.is_valid() and profile_form.is_valid():            
            user = user_form.save()            
            user.save()
            profile = profile_form.save(commit=False)
            profile.user = user 
            if 'profile_pic' in request.FILES:                
                profile.profile_pic = request.FILES['profile_pic']           
            profile.save()         
            return redirect('/profile/')
        else:
            messages.error(request, ('Please correct the error below.'))
    else:
        user_form = UserInfoForm(instance=request.user)
        profile_form = UserProfileInfoForm(instance=request.user.profile)
    return render(request, 'profile.html', {
        'user_form': user_form,
        'profile_form': profile_form
    })

here's the form

<form role="form" class="form-horizontal" method="post">                          

                          {% load staticfiles %}
                          {% block body_block %}

                                {% if registered %}
                                  <h1>Update Profile Success!</h1>
                                {% else %}    
                                  <form class="cmxform form-horizontal style-form" id="commentForm" enctype="multipart/form-data" method="POST" action="">
                                    {% csrf_token %}        
                                    {{ user_form.as_p }}
                                    {{ profile_form.as_p }}

                                    <input type="submit" name="" value="Update">                          
                                  </form>

                                {% endif %}

                          {% endblock %}                             

                        </form>

urls.py (already add +static[]) from someone suggestion and still not working

app_name = 'polls'
urlpatterns = [
    path('profile/', views.update_profile, name='profile'),        
]+static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Hope someone can solve the problem, thank you.

Upvotes: 0

Views: 330

Answers (2)

Chris
Chris

Reputation: 2212

You are not passing the file to your form, so add request.FILES as the second parameter to form containing the picture in your view, e.g.

profile_form = UserProfileInfoForm(request.POST, request.FILES, instance=request.user.profile

Upvotes: 1

Nalin Dobhal
Nalin Dobhal

Reputation: 2342

Add enctype in your HTML form.

<form role="form" class="form-horizontal" method="post" enctype="multipart/form-data">
    <!-- Your html elements -->
</form>

also provide request.FILES to your form.

profile_form = UserProfileInfoForm(request.POST, request.FILES, instance=request.user.profile)

remove these lines:

if 'profile_pic' in request.FILES:                
    profile.profile_pic = request.FILES['profile_pic']

If you are passing the request.FILES to UserProfileInfoForm then you don't have to directly assign it to the profile.

Upvotes: 1

Related Questions