0
問題的簡單性在於,「用戶」對象可以在開發服務器上的Django模板中訪問,但不能在Apache服務器上訪問。我在TEMPLATE_CONTEXT_PROCESSORS中啓用了'django.core.context_processors.request'。Django - 無法訪問Apache服務器中的「用戶」,在開發服務器上工作
下面是從行爲像它應該開發服務器上的模板的示例一段代碼,而不是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,
}
嘗試使用django-debug-toolbar在調試模式下暫時**運行您的站點,以檢查哪些變量可用於模板。 – bouke
謝謝你今晚會和我一起玩,我甚至沒有意識到有這樣的工具。另外,我可以首先在我的本地Apache服務器上測試它。 – MikkoP