简体   繁体   English

为什么 Django 模板加载器在生产中区分大小写,但在开发中不区分大小写

[英]Why Django template loader is case-sensitive in production but not in development

I'm using manage.py runserver on a MacOs Catalina OS as development.我在 MacOs Catalina OS 上使用manage.py runserver作为开发。 I have some templates that fit my built-in class based views.我有一些适合我内置的基于类的视图的模板。 For example:例如:

CuadroDeControl_detail.html                     LoteDeMedio_list.html                           TipoDeMedio_detail_tablaCuadros.html
CuadroDeControl_detail_resumen.html             LoteDeMedio_list_tabla.html                     TipoDeMedio_list.html
CuadroDeControl_detail_tablaMetodos.html        MetodoDeControl_detail.html                     TipoDeMedio_list_tabla.html
LoteDeMedio_confirm_delete.html                 MetodoDeControl_detail_resumen.html             dropdown_CuadroDeControl.html
LoteDeMedio_create.html                         TipoDeMedio_confirm_delete.html                 dropwdown_CuadroDeControl.html
LoteDeMedio_detail.html                         TipoDeMedio_detail.html
LoteDeMedio_detail_resumen.html                 TipoDeMedio_detail_resumen.html

Here is an example of a working view:这是工作视图的示例:

class TipoDeMedioDetailView(AreaCalidadMixin, DashboardMixin,  DetailView):
         model = TipoDeMedio

Note that my views do not explicitly set template_name .请注意,我的视图没有明确设置template_name In my production environment, all my views load just fine.在我的生产环境中,我所有的视图都加载得很好。 Django's template loader knows that the corresponding template to the view is TipoDeMedio_detail.html Django 的模板加载器知道视图对应的模板是TipoDeMedio_detail.html

However, in my production environment, which is set up with apache2 and mod_wsgi on a Ubuntu 20.04.2 LTS x86_64 Linode VM, the template loader fails to load the template of the same view, because it searches for it in all lowercase.但是,在我的生产环境中,在 Ubuntu 20.04.2 LTS x86_64 Linode VM 上设置了apache2mod_wsgi ,模板加载器无法加载相同视图的模板,因为它以全部小写形式搜索它。 Here is an example:下面是一个例子:

Request Method: GET
Request URL:    http://45.79.4.38/calidad/TipoDeMedio/lista
Django Version: 3.1.6
Exception Type: TemplateDoesNotExist
Exception Value:    
calidad/tipodemedio_list.html
Exception Location: /home/jonatan/django-app/venv/lib/python3.8/site-packages/django/template/loader.py, line 47, in select_template
Python Executable:  /home/jonatan/django-app/venv/bin/python
Python Version: 3.8.5
Python Path:    
['/home/jonatan/django-app/mysite',
 '/usr/lib/python38.zip',
 '/usr/lib/python3.8',
 '/usr/lib/python3.8/lib-dynload',
 '/home/jonatan/django-app/venv/lib/python3.8/site-packages']
Server time:    Mon, 21 Jun 2021 18:24:19 -0500
Template-loader postmortem

Django tried loading these templates, in this order:

Using engine django:
django.template.loaders.filesystem.Loader: /home/jonatan/django-app/mysite/templates/calidad/tipodemedio_list.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/jonatan/django-app/mysite/login/templates/calidad/tipodemedio_list.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/jonatan/django-app/venv/lib/python3.8/site-packages/django/contrib/admin/templates/calidad/tipodemedio_list.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/jonatan/django-app/venv/lib/python3.8/site-packages/django/contrib/auth/templates/calidad/tipodemedio_list.html (Source does not exist)

An easy fix is to manually specify my template_name attribute on each of my CBV and point to the correct case-sensitive template name (for instance TipoDeMedio_detail.html . However, I would really like to avoid that.一个简单的解决方法是在我的每个CBV上手动指定我的template_name属性并指向正确的区分大小写的模板名称(例如TipoDeMedio_detail.html 。但是,我真的很想避免这种情况。

I'm just trying to understand what is the root cause of the change in behavior between the environments.我只是想了解环境之间行为变化的根本原因是什么。 It just leads me to believe I will encounter similar problems in other aspects of Django's behavior.它只是让我相信我会在 Django 行为的其他方面遇到类似的问题。

The default Apple File System (APFS) is not case sensitive.默认的 Apple 文件系统 (APFS) 不区分大小写。 This means that during development on your Mac, Django was able to find templates even if the filenames used incorrect case.这意味着在 Mac 上的开发过程中,即使文件名使用了不正确的大小写,Django 也能够找到模板。

Now that you have moved to Ubuntu, it uses a case-sensitive file system by default.现在您已经迁移到 Ubuntu,它默认使用区分大小写的文件系统。 Django is not able to find the templates if the case is wrong.如果大小写错误,Django 将无法找到模板。

Note that the Django docs for class-based generic views state:请注意, 基于类的通用视图Django 文档状态:

... in the absence of an explicit template Django will infer one from the object's name ... is the lowercased version of the model's name. ...在没有显式模板的情况下,Django 会从对象的名称中推断出一个 ... 是模型名称的小写版本。

The preferable solution is to rename your templates using lowercase.更可取的解决方案是使用小写字母重命名模板。

MattRowbum pointed out to me APFS isn't case sensitive. MattRowbum 向我指出 APFS 不区分大小写。 I consider this sufficient explanation for the change in behavior.我认为这是行为改变的充分解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM