如何访问 Django 模板中的用户配置文件? [英] How to access the user profile in a Django template?
问题描述
我正在使用 AUTH_PROFILE_MODULE
.
I'm storing some additional per-user information using the AUTH_PROFILE_MODULE
.
我们可以使用 {{ request.user }}
在 Django 模板中访问用户,但是我们如何访问配置文件中的字段,因为配置文件只能通过函数 user 访问.get_profile()
?
We can access the user in a Django template using {{ request.user }}
but how do we access fields in the profile since the profile is only accessible via a function user.get_profile()
?
真的需要每次都显式地将配置文件传递到模板中吗?
Is it really required to explicitly pass the profile into the template every time?
推荐答案
使用 {{ request.user.get_profile.whatever }}
.Django 的模板语言会自动调用可调用的东西 - 在本例中为 .get_profile()
方法.
Use {{ request.user.get_profile.whatever }}
. Django's templating language automatically calls things that are callable - in this case, the .get_profile()
method.
这篇关于如何访问 Django 模板中的用户配置文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!