IE 11 - 基于 Django 的视图问题
IE 11 - Django based views issue
我有一个 Django 应用程序,我在其中的某些部分使用了基于 class 的视图。当我尝试在 IE 11 上访问这些视图时,它会下载 html 而不是渲染它。我在网上搜索了一下,发现其他帖子都在谈论同样的问题:
https://www.reddit.com/r/django/comments/74r2af/django_not_working_with_internet_explorer/ 和
IE 11 Downloads Page instead of opening it, works in Chrome and Firefox
据此,是由于上下文定义错误导致的渲染问题,但我的观点如下所示:
class BaseContentMixin(ActiveLoginRequiredMixin, TenantHasRolePermMixin, TenantActiveNotExpiredMixin, TenantHasPermMixin, SetDefaultTimezoneMixin):
"""
Attributes:
content (str): content name, i.e 'training'.
content_type (str): content type, 'shared' or 'custom'.
permission (str): permission, i.e 'TRAININGS'.
"""
content = ''
content_type = ''
role_permission = 'SEE'
permission = ''
def get_content_class(self):
return Util.get_content_class('{}-{}'.format(self.content, self.content_type))
def get_content_shared_class(self):
return Util.get_content_class('{}-shared'.format(self.content))
class ContentListView(BaseContentMixin, TemplateView):
def get_context_data(self, **kwargs):
context = super(ContentListView, self).get_context_data(**kwargs)
context['title'] = Util.get_content_title(self.content)
context['active'] = self.content
context['submenu_active'] = '{}_{}'.format(self.content, self.content_type)
context['objects'] = self.get_content_class().objects.get_for_user(self.request.user)
context['toggle_url'] = reverse('{}_{}_active_toggle'.format(self.content, self.content_type))
context['add_url'] = reverse('{}_{}_add'.format(self.content, self.content_type), kwargs={'lang': get_language()})
if self.content_type == 'custom':
contents_shared = self.get_content_shared_class().objects.get_topics(self.request.user)
context['form'] = Util.get_content_form_from_template(self.content)(contents_shared=contents_shared)
context['add_url_from_template'] = reverse('{}_custom_add_from_template'.format(self.content), kwargs={'lang': get_language()})
return context
当我打印上下文时,它给了我这个:
{
'add_url_from_template': u'/trainings/custom/add-from-template/es-ar/',
'form': <TrainingCustomFromTemplateFrombound=False,
valid=Unknown,
fields=(trainings_template)>,
'title': u'M\xf3dulos Interactivos',
'add_url': u'/trainings/custom/add/es-ar/',
'objects': <TranslationQueryset[
<TrainingCustom: 1a1a1a1a1aa1>,
<TrainingCustom: DoubleTest1>,
<TrainingCustom: DoubleTest2>,
<TrainingCustom: Nuevito>,
<TrainingCustom: TestCampaign4>,
<TrainingCustom: TestCampaignDOUBLETEST>,
<TrainingCustom: TestCampaignDoubleTest1>,
<TrainingCustom: TestCampaignTest>,
<TrainingCustom: TestCampaignTest2>,
<TrainingCustom: TestCampaignTest3>,
<TrainingCustom: aversifunk>,
<TrainingCustom: eeee>,
<TrainingCustom: qweqweqwew>,
<TrainingCustom: rrrr>,
<TrainingCustom: weqwe>,
<TrainingCustom: wqeqweqwe>
]>,
'toggle_url': u'/trainings/custom/active-toggle/',
'submenu_active': 'training_custom',
'active': 'training',
u'view': <trainingcustom.views.TrainingCustomListViewobjectat0x10af0b550>
}
好像没什么问题,我有点卡在这里了。我认为如果这是 TemplateView/IE11 兼容性问题,我应该找到一些相关信息。
BaseContentMixin
不应使用名为 content_type
的字段,因为这会与 TemplateView
中的类似命名字段发生冲突,后者最终用于设置 响应的Content-Type
header。当您将其覆盖为 'custom'
时,您最终会混淆浏览器,因为这不是有效的内容类型。
我有一个 Django 应用程序,我在其中的某些部分使用了基于 class 的视图。当我尝试在 IE 11 上访问这些视图时,它会下载 html 而不是渲染它。我在网上搜索了一下,发现其他帖子都在谈论同样的问题:
https://www.reddit.com/r/django/comments/74r2af/django_not_working_with_internet_explorer/ 和
据此,是由于上下文定义错误导致的渲染问题,但我的观点如下所示:
class BaseContentMixin(ActiveLoginRequiredMixin, TenantHasRolePermMixin, TenantActiveNotExpiredMixin, TenantHasPermMixin, SetDefaultTimezoneMixin):
"""
Attributes:
content (str): content name, i.e 'training'.
content_type (str): content type, 'shared' or 'custom'.
permission (str): permission, i.e 'TRAININGS'.
"""
content = ''
content_type = ''
role_permission = 'SEE'
permission = ''
def get_content_class(self):
return Util.get_content_class('{}-{}'.format(self.content, self.content_type))
def get_content_shared_class(self):
return Util.get_content_class('{}-shared'.format(self.content))
class ContentListView(BaseContentMixin, TemplateView):
def get_context_data(self, **kwargs):
context = super(ContentListView, self).get_context_data(**kwargs)
context['title'] = Util.get_content_title(self.content)
context['active'] = self.content
context['submenu_active'] = '{}_{}'.format(self.content, self.content_type)
context['objects'] = self.get_content_class().objects.get_for_user(self.request.user)
context['toggle_url'] = reverse('{}_{}_active_toggle'.format(self.content, self.content_type))
context['add_url'] = reverse('{}_{}_add'.format(self.content, self.content_type), kwargs={'lang': get_language()})
if self.content_type == 'custom':
contents_shared = self.get_content_shared_class().objects.get_topics(self.request.user)
context['form'] = Util.get_content_form_from_template(self.content)(contents_shared=contents_shared)
context['add_url_from_template'] = reverse('{}_custom_add_from_template'.format(self.content), kwargs={'lang': get_language()})
return context
当我打印上下文时,它给了我这个:
{
'add_url_from_template': u'/trainings/custom/add-from-template/es-ar/',
'form': <TrainingCustomFromTemplateFrombound=False,
valid=Unknown,
fields=(trainings_template)>,
'title': u'M\xf3dulos Interactivos',
'add_url': u'/trainings/custom/add/es-ar/',
'objects': <TranslationQueryset[
<TrainingCustom: 1a1a1a1a1aa1>,
<TrainingCustom: DoubleTest1>,
<TrainingCustom: DoubleTest2>,
<TrainingCustom: Nuevito>,
<TrainingCustom: TestCampaign4>,
<TrainingCustom: TestCampaignDOUBLETEST>,
<TrainingCustom: TestCampaignDoubleTest1>,
<TrainingCustom: TestCampaignTest>,
<TrainingCustom: TestCampaignTest2>,
<TrainingCustom: TestCampaignTest3>,
<TrainingCustom: aversifunk>,
<TrainingCustom: eeee>,
<TrainingCustom: qweqweqwew>,
<TrainingCustom: rrrr>,
<TrainingCustom: weqwe>,
<TrainingCustom: wqeqweqwe>
]>,
'toggle_url': u'/trainings/custom/active-toggle/',
'submenu_active': 'training_custom',
'active': 'training',
u'view': <trainingcustom.views.TrainingCustomListViewobjectat0x10af0b550>
}
好像没什么问题,我有点卡在这里了。我认为如果这是 TemplateView/IE11 兼容性问题,我应该找到一些相关信息。
BaseContentMixin
不应使用名为 content_type
的字段,因为这会与 TemplateView
中的类似命名字段发生冲突,后者最终用于设置 响应的Content-Type
header。当您将其覆盖为 'custom'
时,您最终会混淆浏览器,因为这不是有效的内容类型。