How to access the user profile in a Django template?

前端 未结 6 1711
滥情空心
滥情空心 2020-11-30 21:05

I\'m storing some additional per-user information using the AUTH_PROFILE_MODULE.

We can access the user in a Django template using {{ request.user }} bu

6条回答
  •  悲哀的现实
    2020-11-30 21:33

    Yes it is possible to access profile from template using request.user.get_profile

    However there is a small caveat: not all users will have profiles, which was in my case with admin users. So calling directly {{ request.user.get_profile.whatever }} from the template will cause an error in such cases.

    If you are sure all your users always have profiles it is safe to call from template, otherwise call get_profile() from within try-except block in your view and pass it to the template.

提交回复
热议问题