gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 22:18:57 24 4
gpt4 key购买 nike

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

{{ 用户。 }} 工作正常,所以我认为这是我的模型的问题,但我检查了 Django 文档“模型”、“数据库查询”和“模型实例引用”以及相关的 S/O 帖子,但如果它是我的模型就是这个问题,我不知道还要搜索什么。

谢谢

模型.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)

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

最佳答案

您已设置 related_name='user'关于你的外键 UserProfile模型,这意味着您必须使用 user.user 访问它.

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

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

关于python - 无法访问 Django 模板中的 UserProfile 模型字段。试过 {{ user.userprofile }},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50156579/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com