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.
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 calling storage.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).