Search code examples
pythondjangomodeluser-profiletemplatetags

Unable to access UserProfile model fields in templates in Django. Tried {{ user.userprofile }}


I've tried to import {{ user.userprofile. }} and {{ user.userprofile }} with no success. In the django shell I can access the user profile with UserProfile.objects.all().

{{ user. }} works fine, so I think it's an issue with my model, but I've checked the Django docs 'Models', 'database query', and 'model instance reference' and related S/O posts, but if it's my model that's the issue, I don't know what else to search for.

Thanks

models.py

from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver

class UserProfileManager(models.Manager):
    pass

class UserProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='user')
    can_inst_nat = models.BooleanField(verbose_name='I can explain grammar and word usage to people in my NATIVE language.', default=False)
    can_inst_tar = models.BooleanField(verbose_name='I can explain grammar and word usage to people in my TARGET language.', default=False)
    wants_nat_lang_inst = models.BooleanField(verbose_name='I would like grammar and vocabulary explained to me in my NATIVE language.', default=False)
    wants_tar_lang_inst = models.BooleanField(verbose_name='I would like grammar and vocabulary explained to me in my TARGET language.', default=False)
    wants_cont = models.BooleanField(verbose_name='I would like to continue working with the same partner for several consecutive exchanges.', help_text='(If you want continuity, check this; If you like speaking to a variety of people, leave it blank.)', default=False)
    image = models.ImageField(upload_to='profile_image', blank=True)


    def __str__(self):
        return self.user.username

def create_profile(sender, **kwargs):
    user = kwargs['instance']
    if kwargs['created']:
        user_profile = UserProfile(user=user)
        user_profile.save()
post_save.connect(create_profile, sender=User)

views.py

def edit_profile(request, pk):
    user = User.objects.get(pk=pk)
    user_form = EditProfileForm(instance=user)
    ProfileInlineFormset = inlineformset_factory(User, UserProfile, fields=[
        'image',
        'can_inst_nat',
        'can_inst_tar',
        'wants_nat_lang_inst',
        'wants_tar_lang_inst',
        'wants_cont',
    ])

    formset = ProfileInlineFormset(instance=user)

    if request.user.is_authenticated and request.user.id == user.id:
        if request.method == "POST":
            user_form = EditProfileForm(request.POST, request.FILES, instance=user)

            if user_form.is_valid():
                created_user = user_form.save(commit=False)
                formset = ProfileInlineFormset(request.POST, request.FILES, instance=created_user)

                if formset.is_valid():
                    created_user.save()
                    formset.save()
                    return redirect(reverse('accounts:view_profile'))

        return render(request, 'accounts/edit_profile.html', {
            "noodle": pk,
            "noodle_form": user_form,
            "formset": formset,
        })

    else:
        raise PermissionDenied

template

<h4>Preferences</h4>
    <p> {{ user.userprofile }}</p>
    <ul>
    {% if user.profile.can_inst_nat %}
      {{ user.userprofile.can_inst_nat }}
      <li class="profile-list-item">"I <strong>can explain grammar and word usage</strong> to people <strong>in my <em>native</em> language</strong>."</li><br>
    {% endif %}
    {% if user.userprofile.can_inst_tar %}
      <li class="profile-list-item">"I <strong>can explain grammar and word usage</strong> to people <strong>in my <em>target</em> language</strong>."</li><br>
    {% endif %}
    {% if user.userprofile.wants_nat_lang_inst %}
      <li class="profile-list-item">"I <strong>want grammar and vocab</strong> explained to me <strong>in my <em>native</em> language</strong>."</li><br>
    {% endif %}
    {% if user.userprofile.wants_tar_lang_inst %}
      <li class="profile-list-item">"I <strong>want grammar and vocab</strong> explained to me <strong>in my <em>target</em> language</strong>."</li><br>
    {% endif %}
    {% if user.userprofile.wants_cont %}
      <li class="profile-list-item">"I <strong>want</strong> to continue working with <strong>the same partner</strong> for several <strong>consecutive exchanges</strong>."</li><br>
    {% endif %}
    </ul>

Solution

  • You have set related_name='user' on the foreign key on you UserProfile model, which means you have to access it with user.user.

    related_name defines how you access the reverse relationship, not the forward one, so setting it to user is a bit confusing, and probably isn't how you intended for it to work.

    You might consider changing the related_name to "profile" so that you can instead access it as user.profile.