其所有简单性的问题在于“用户”对象可以在开发服务器上的 Django 模板中访问,但不能在 Apache 服务器上访问。我在 TEMPLATE_CONTEXT_PROCESSORS 中启用了 'django.core.context_processors.request'。
这是来自模板的示例代码,其行为类似于在开发服务器上的行为,而不是在 apache 服务器上(我将 user 和 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,
}