MWh*_*man 2 python django model templatetags user-profile
我试图导入 {{ 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)
Run Code Online (Sandbox Code Playgroud)
视图.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
Run Code Online (Sandbox Code Playgroud)
模板
<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>
Run Code Online (Sandbox Code Playgroud)
您已经related_name='user'在UserProfile模型上设置了外键,这意味着您必须使用user.user.
related_name定义您如何访问反向关系,而不是正向关系,因此将其设置user为有点令人困惑,并且可能不是您希望它如何工作。
您可能会考虑将 更改为related_name,"profile"以便您可以将其访问为user.profile。
| 归档时间: |
|
| 查看次数: |
590 次 |
| 最近记录: |