无法访问 Django 模板中的 UserProfile 模型字段。试过 {{ user.userprofile }}

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

我尝试导入 {{ user.userprofile。 }} 和 {{ user.userprofile }} 没有成功。在 django shell 中,我可以使用 UserProfile.objects.all().

访问用户配置文件

{{ 用户。 }} 工作正常,所以我认为这是我的模型的问题,但我检查了 Django 文档 'Models'、'database query' 和 'model instance reference' 以及相关的 S/O 帖子,但如果问题出在我的模型上,我不知道还能搜索什么。

谢谢

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

模板

<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>

您已经在 UserProfile 模型的外键上设置了 related_name='user',这意味着您必须使用 user.user.

访问它

related_name defines how you access the reverse relationship,不是前向的,所以将其设置为 user 有点令人困惑,而且可能不是您希望它工作的方式。

您可以考虑将 related_name 更改为 "profile",这样您就可以将其作为 user.profile 访问。

将 related_name 从 user 更改为 userprofile

user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='userprofile')

并且不要忘记将用户传递到上下文中。