Profile Update
In this view, we are going to implement the feature of profile update. Using this feature, the user can update their profile information our the database.
Resources:
Forms
At first, we need to create a form for profile update. So, import the modelform.
from django.forms import ModelForm
Now, create a form named "ProfileUpdateForm".
class ProfileUpdateForm(ModelForm):
# After that, we need to add form-control class to every field of this form.
def __init__(self, *args, **kwargs):
super(ProfileUpdateForm, self).__init__(*args, **kwargs)
for visible in self.visible_fields():
visible.field.widget.attrs["class"] = "form-control"
# Now create a meta class to map this form to a particular model.
class Meta:
model = User
# Specify the fields that is needed to be included in the form.
fields = [
"first_name",
"middle_name",
"last_name",
"gender",
"photo",
"date_of_birth",
"identity_document_type",
"identity_document_number",
]
In this way, we have create a form that updates some information of the user.
Views
In the view, lets import the form.
from user.forms import ProfileUpdateForm
After that, create a profile_update view.
[Explain the get method first, then post method]
def profile_update(request):
# When the user sends the post request,
if request.method == "POST":
# At first, we have to instantiate the form.
form = ProfileUpdateForm(request.POST, request.FILES, instance=request.user)
# We need to write request.FILES because we are accepting the profile image from this form.
# Now, when the form is valid
if form.is_valid():
# We will save the form
form.save()
# Then we have to provide the success message to the user.
messages.success(request, "Profile Information Updated Successfully")
# Then redirect the user to the profile-view page.
return HttpResponseRedirect(reverse("accounts:profile-view"))
# But if the form is invalid, we will show error message
messages.error(request, "Invalid Data! Please enter correct data")
# And, we will render the same profile-update page with the form.
return render(request, "user/profile-update.html", {"form": form})
# If the user sends the get request, we have to show the profile update form.
context = {"form": ProfileUpdateForm(instance=request.user)}
# We need to provide the instance of model which we want to update.
# To get the instance of currently logged in user, we can use request.user
# Now render the profile-update template with this context.
return render(request, "user/profile-update.html", context)
In this way, we have wrote the profile update view that renders a form to update the user information.
Templates
Now, lets create a template for this view "profiler-update.html".
I have attached profile-update.html file in the resources section. Downlad it, copy its content and paste it in the html file.
{% extends 'mysite/base.html' %}
{% block title %}
<title>Profile Update</title>
{% endblock title %}
{% block content %}
<div class="m-2 p-2">
<h4>Fill the following details to update the profile</h4>
{% include "components/auth-form.html" with form_name="Update Profile" %}
</div>
{% endblock content %}
URLS
Now, lets add the url path for this view.
path("profile-update/", views.profile_update, name="profile-update"),
[Run the development server and see the changes.]