Cipher
Cipher

Reputation: 2122

How to display object of two forms in template

I want to display username using {{ object.first_name }}. I am not able to display it. I am also using User in-built module where UserProfile is linked with OneToOneField. if i am using {{ p_form.user_image }}, it is display image input field profile.html

{% extends "base.html" %}
    {% load crispy_forms_tags %}
    {% block content %}
    <div class="col-md-6 grid-margin stretch-card">
        <div class="card">
          <div class="card-body">
                <h4 class="card-title">Profile Information - {{ object.first_name }}</h4>
            <form class="forms-sample" action="" method="POST" enctype="multipart/form-data">
                {% csrf_token %}
                {{ u_form|crispy }}
                {{ p_form|crispy }}

                <button class="btn btn-success mr-2" type="submit">Update</button>
            </form>
          </div>
        </div>
    </div>
    <ul>
    {% for key, val in object %}
      <li>{{ key }}: {{ val }}</li>
    {% endfor %}
    </ul>
    {% endblock content %}

Views.py

def userProfileUpdate(request, pk):
    if request.method == 'POST':
        u_form = UserUpdateForm(request.POST, instance=User.objects.get(id=pk))
        p_form = UserProfileForm(request.POST, 
                                 request.FILES,
                                 instance=UserProfile.objects.get(user_id=pk))
        if u_form.is_valid() and p_form.is_valid():
            u_form.save()
            p_form.save()
            messages.success(request, 'Profile Updated!!!')
            return redirect('users')
    else:
        u_form = UserUpdateForm(instance=User.objects.get(id=pk))
        p_form = UserProfileForm(instance=UserProfile.objects.get(user_id=pk))
    context ={
        'u_form': u_form,
        'p_form': p_form
    }

    return render(request, 'users/profile.html', context)

UserProfile Model

class UserProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)

Upvotes: 0

Views: 87

Answers (1)

ruddra
ruddra

Reputation: 51988

You need to pass object as context. You can try like this:

def userProfileUpdate(request, pk):
    if request.method == 'POST':
        u_form = UserUpdateForm(request.POST, instance=User.objects.get(id=pk))
        p_form = UserProfileForm(request.POST, 
                                 request.FILES,
                                 instance=UserProfile.objects.get(user_id=pk))
        if u_form.is_valid() and p_form.is_valid():
            u_form.save()
            p_form.save()
            messages.success(request, 'Profile Updated!!!')
            return redirect('users')
    else:
        instance = User.objects.get(id=pk)
        u_form = UserUpdateForm(instance=instance)
        p_form = UserProfileForm(instance=UserProfile.objects.get(user_id=pk))
    context ={
        'object': instance, <-- here
        'u_form': u_form,
        'p_form': p_form
    }

    return render(request, 'users/profile.html', context)

Or if the user is logged in then use {{ user.first_name }} to show first name.

Upvotes: 1

Related Questions