2018-10-01 TIL(UserChangeForm 사용해서 프로필 수정하기)

forms.py

class UpdateProfile(UserChangeForm):
    class Meta:
        model = User
        fields = ('img_profile',
                  'username',
                  'password',
                  'email',
                  'phone_number',
                  'gender',
                  'main_position',
                  'introduce'
                  )

views.py

def change_profile(request):
    if request.method == 'POST':
        form = UpdateProfile(request.POST, instance=request.user)
        if form.is_valid():
            form.save()
            return redirect('profile')
    else:
        form = UpdateProfile(instance=request.user)
        context = {
            'form':form,
        }
        return render(request, 'members/change_profile.html', context)

change_profile

 <div class="container mt-5">
	<form action="" method="POST" enctype="multipart/form-data">
		{% csrf_token %}
			{% for field in form %}
				<div class="form-group">
					<p class="form-control-sm">{{ field.label_tag }} {{ field }}</p>
						{{ field.errors }}
             </div>
          {% endfor %}
		<button type="submit">수정하기</button>
	</form>
</div>

Comments