如何在个人资料页面上显示扩展的用户数据django

时间:2016-11-11 20:52:25

标签: django django-models django-forms django-templates django-views

所以我正致力于扩展django用户模型following this tutorial

经过一些试验和错误后,我完成了它,但我似乎无法找到一种方法来显示配置文件上的扩展数据

profile.html     

    <h2> {{user.first_name}} </h2>
    <h2>  {{user.last_name}} </h2>
    <h2>  {{user.email} </h2>
    <h2>  {{user.website} </h2>
    <h2>  {{user.bio} </h2>
    <h2>  {{user.phone} </h2>
    <h2>  {{user.city} </h2>
    <h2>  {{user.country} </h2>


</div>

view.py

@login_required() # only logged in users should access this
def edit_user(request, pk):
    # querying the User object with pk from url
    user = User.objects.get(pk=pk)

    # prepopulate UserProfileForm with retrieved user values from above.
    user_form = UserForm(instance=user)

    # The sorcery begins from here, see explanation below
    ProfileInlineFormset = inlineformset_factory(User, UserProfile, fields=('website', 'bio', 'phone', 'city', 'country', 'organization'))
    formset = ProfileInlineFormset(instance=user)

    if request.user.is_authenticated() and request.user.id == user.id:
        if request.method == "POST":
            user_form = UserForm(request.POST, request.FILES, instance=user)
            formset = ProfileInlineFormset(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 HttpResponseRedirect('/accounts/profile/')

        return render(request, "account/account_update.html", {
            "noodle": pk,
            "noodle_form": user_form,
            "formset": formset,
        })
    else:

        raise PermissionDenied

网址

url(r'^admin/', admin.site.urls),
    url(r'^$', main_views.home, name='home'),
    url(r'^accounts/profile/$', main_views.uprofile, name='uprofile'),
    url(r'^accounts/update/(?P<pk>\d+)/', User_Profile_views.edit_user, name='edit_user'),
    url(r'^accounts/', include('allauth.urls')),

模型

class UserProfile(models.Model):
    user = models.OneToOneField(User, related_name='user')
    website = models.URLField(default='', blank=True)
    bio = models.TextField(default='', blank=True)
    phone = models.CharField(max_length=20, blank=True, default='')
    city = models.CharField(max_length=100, default='', blank=True)
    country = models.CharField(max_length=100, default='', blank=True)
    organization = models.CharField(max_length=100, default='', blank=True)




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)

一切正常但我找不到显示在UserProfile模型中创建的扩展数据的方法

2 个答案:

答案 0 :(得分:0)

您的扩展数据模板变量中似乎缺少第二个“}”。

尝试:

{{1}}

答案 1 :(得分:0)

你必须跨越这种关系,如下:

    <h2>  {{user.user.bio}} </h2>
    <h2>  {{user.user.phone}} </h2>
    <h2>  {{user.user.city}} </h2>
    <h2>  {{user.user.country}} </h2>

请注意,如果您没有related_name='user'

,则会如下所示
        <h2>  {{user.userprofile.bio}} </h2>
        <h2>  {{user.userprofile.phone}} </h2>
        <h2>  {{user.userprofile.city}} </h2>
        <h2>  {{user.userprofile.country}} </h2>