0

I want to iterate through User model and the UserProfile model that is attached to User to display all the information about the User in a profile page such as

UserName:
FirstName:
LastName:
Date of Birth:
.....

Is there a generic templateView, that I can use to pass the model and gets its fields displayed on template. Reading many SO articles, I tried this:

 ##display user fields
    {% for field in User._meta.get_all_field_names() %}
       <p> {{ field }} : {{ getatttr(field,user) }} </p>
     {% endfor %}
     ## display Userprofile fields
     {for field in User.profile._meta.get_all_field_names() %}
          <p> {{ field }} : {{ getatttr(field,user.profile) }} </p>
     {% endfor %}

This does not work: in fact, I tried only to display all the fields and not its values first:

{% for field in User._meta.get_all_field_names() %}
        <p> {{ field }} </p>
     {% endfor %}

but I get this error: Variables and attributes may not begin with underscores: 'User._meta.get_all_field_names'

Any idea how to fix this?

eagertoLearn
  • 9,772
  • 23
  • 80
  • 122

1 Answers1

3

You can push this code to your view, and use the handy django.forms.models.model_to_dict function to get a dictionary of fields to values for user and user.profile

views.py:

from django.forms.models import model_to_dict

class UserDetailView(DetailView):

    model = User

    def get_context_data(self, **kwargs):
        context = super(UserDetailView, self).get_context_data(**kwargs)
        context['user_attr_map'] = model_to_dict(self.object)
        context['userprofile_attr_map'] = model_to_dict(self.object.profile)
        return context

template.html:

{% for k, v in user_attr_map.items %}
    <p> {{ k }} : {{ v }} </p>
{% endfor %}
{% for k, v in userprofile_attr_map.items %}
    <p> {{ k }} : {{ v }} </p>
{% endfor %}
Sohan Jain
  • 2,318
  • 1
  • 16
  • 17
  • great, this helped a lot.is it possible to remove fields such as `is_superuser`, `is_staff` etc – eagertoLearn Mar 27 '14 at 19:03
  • yep, you can pass a list of fields to the the keyword argument `exclude` to model_to_dict. `context['user_attr_map'] = model_to_dict(self.object, exclude=['is_superuser', 'is_staff']) the method signature is here: https://github.com/django/django/blob/master/django/forms/models.py#L112 – Sohan Jain Mar 27 '14 at 19:07
  • Thanks that worked. Is there a way to display all the User details for a user to edit after they are logged in such as email, password, first name, last name etc but not UserName.. – eagertoLearn Mar 27 '14 at 19:15
  • I tried this `{% for field, value in userprofile_attr_map.items %}

    {{ field }} : {{ value|yesno:"-----" }}

    {% endfor %}` but it is not displaying yes/no. why is that?
    – eagertoLearn Mar 27 '14 at 19:35
  • Hm, is that the correct way to use the "yesno" filter? I think it takes in string with 3 comma separated values, like here: https://docs.djangoproject.com/en/dev/ref/templates/builtins/#yesno – Sohan Jain Mar 27 '14 at 19:40
  • Thanks again. that helped. do you have any thought above my previous comment? – eagertoLearn Mar 27 '14 at 19:43