问题很简单,“user”对象可以在开发服务器上的Django模板中访问,但不能在Apache服务器上访问。我在TEMPLATE_CONTEXT_PROCESSORS中启用了“django.core.context_processors.request”。
以下是来自模板的一段示例代码,其行为类似于开发服务器上的内容,但不是在apache服务器上(我将用户和request.user都用于测试):
{{ user.get_profile.user_type }}
{{ request.user.get_profile.user_type }}
这是一个观点:
class NotificationsListView(LoginRequiredMixin, CorrectUserMixin, TemplateView):
error_message = 'Oops, something went wrong. \
The browser was trying to access someone else\'s notification list.'
def get_context_data(self, **kwargs):
my_id = self.request.user.id
user_type = self.request.user.get_profile().user_type
if user_type == 'Developer':
self.template_name = 'my_notifications_developer.html'
notifications = RequestNotification.objects.filter(receiver__id = my_id, seen=False).order_by('-time_created')
else:
self.template_name = 'my_notifications_charity.html'
notifications = RequestNotification.objects.filter(receiver__id = my_id, seen=False).order_by('-time_created')
# needed for correct user mixin
self.url_id = self.kwargs['pk']
return {
'params': kwargs,
'notifications': notifications,
}
答案 0 :(得分:0)
哦,天哪,这只是我的一个愚蠢的错误,因为我在管理界面中创建了一个配置文件,并没有手动将用户配置文件附加到用户对象...