Я пытался импортировать {{user.userprofile. }} и {{user.userprofile}} безуспешно. В оболочке django я могу получить доступ к профилю пользователя с помощью UserProfile.objects.all ().
{{пользователь. }} работает нормально, поэтому я думаю, что это проблема с моей моделью, но я проверил документы Django «Модели», «запрос к базе данных» и «Ссылка на экземпляр модели» и связанные сообщения 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>