无法打开包含特殊字符的链接
我有 Django 应用程序,我在本地 PC 上测试它,然后当我添加新内容时,我会将更改上传到我公司的虚拟服务器。我的文件链接有问题。下面是附加文件链接的代码(我的本地和服务器都有相同的代码):
<p class="article-content mt-2 mb-1"><strong>Attachment: </strong><a href="{{post.file_close.url}}">{{post.file_close.name}}</a></p>
如果我上传文件 cars.png
,则两个版本中的一切都正常。无论扩展名、pdf、excel、图像等如何,这都有效。 问题是,如果我上传文件 carsčč.png
它仅在服务器端失败,在我的电脑上它工作得很好。 我在 Django/debugg 上收到以下错误:
Page not found (404)
“C:\inetpub\files\PyWeb\media\PN_files\2022\03\06\cars” does not exist
错误页面的 Url 是:
http://hidden_part/media/PN_files/2022/03/06/carsčč.png
但是当我单击复制并粘贴时,它是这样的:
http://hidden_part/media/PN_files/2022/03/06/cars%C4%8D%C4%8D.png
就像链接不完整一样,一旦遇到特殊字符,它就会停止。 但是,显示的链接仍然包含所有字母,它是 PN_files/2022/03/06/carsčč.png
尝试过: 我查看了区域设置,两台电脑上都是一样的。还有什么我可以检查或更改的吗?也许在链接中包含一些内容? 另外,当我手动搜索该文件时,名称没有被破坏,它在本地保存为 carsčč.png
。所以我猜这只是链接,尝试获取文件。 我发现这是因为变音字母。在我的“base.html”上,我有 meta charset="utf-8"
。 Django 设置 LANGUAGE_CODE = 'hr-BA'
。
我使用 Bootstrap 4.0
服务器托管在 Windows、IIS、.NET 上,全球化设置设置为 utf-8。
I have Django app which I test on my local PC and then when I add something new, I upload changes to my company virtual server. I have a problem with links to files. Below is the code for links to attached files (both my local and server have the same code):
<p class="article-content mt-2 mb-1"><strong>Attachment: </strong><a href="{{post.file_close.url}}">{{post.file_close.name}}</a></p>
If I upload file cars.png
everything works fine in both versions. This works no matter the extension, pdf, excel, image, etc.
Problem is, if I upload file carsčč.png
it fails only on server side, on my PC it works great.
I get the following error on my Django/debugg:
Page not found (404)
“C:\inetpub\files\PyWeb\media\PN_files\2022\03\06\cars” does not exist
Url of the error page is:
http://hidden_part/media/PN_files/2022/03/06/carsčč.png
But when I click copy and paste it's like this:
http://hidden_part/media/PN_files/2022/03/06/cars%C4%8D%C4%8D.png
Like the link is not complete, it stopped as soon as it runs into a special character.
But, shown link still containes all the letters, it's PN_files/2022/03/06/carsčč.png
Tried:
I looked at regional settings, it's the same on both PCs. Is there something else I could check or change? Maybe include something in the link?
Also, when I manually search for the file, the name is not currupted, it's saved localy as carsčč.png
. So I guess it's only the link, tring to get the file.
I figured out it's because of diacritic letters. On my 'base.html' I have meta charset="utf-8"
. Django setting LANGUAGE_CODE = 'hr-BA'
.
I use Bootstrap 4.0 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
Server is hosted on Windows, IIS, .NET Globalization settings are set to utf-8.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
行中,您在内部调用底层 Storage 类的 url() 方法,该方法返回错误的 url
“C:\inetpub\files\PyWeb\media\PN_files\2022\03\06\cars”
。主要问题出在您的存储类别中。您可能正在使用 django 中的
FileSystemStorage
。每当您保存文件时,存储类都会调用storage.get_valid_name
,它返回一个适合在目标系统上使用的名称(ref)。在此步骤中,特殊字符将被替换。存储上提供的代码仅保留原始文件名中的字母数字字符、句点和下划线,删除其他所有内容(ref)Storage 上提供的代码仅保留字母数字字符、句点并从原始文件名中添加下划线,删除其他所有内容。)。如果您想更改此行为,您可以编写自己的自定义存储类并覆盖
get_valid_name
方法 (ref)。In line
<a href="{{post.file_close.url}}">
you are internally calling the url() method of the underlying Storage class, which returns the wrong url“C:\inetpub\files\PyWeb\media\PN_files\2022\03\06\cars”
.The main problem is within your storage class. You are propably using
FileSystemStorage
from django. Whenever you are saving a file, the storage class is callingstorage.get_valid_name
, which is returning a name which is suitable to use on the target system (ref). In this step the special characters are replaced. The code provided on Storage retains only alpha-numeric characters, periods and underscores from the original filename, removing everything else (ref)The code provided on Storage retains only alpha-numeric characters, periods and underscores from the original filename, removing everything else.).If you want to change this behaviour, you could write your own custom storage class and overwrite the
get_valid_name
method (ref).