I want an exception if the django `static` template tag displays a broken url

I want to see an exception if settings.DEBUG is true, and a line like this is handled by the template engine:

<link rel="stylesheet" type="text/css" href="{% static "foo/css/file-name-with-typo.css" %}"> 

Docs for static template tag: https://docs.djangoproject.com/en/1.9/ref/contrib/staticfiles/#static

Background: An exception will crash our continuous integration system, and a broken one may not go live. I do not want to run code in production that creates broken links.

+5
source share
1 answer

Override the url() method to cause an error if the file does not exist:

 from django.core.files.storage import FileSystemStorage class CustomFileSystemStorage(FileSystemStorage): def url(self, name): if not self.exists(name): raise ValueError('"%s" does not exist.' % name) return super(CustomFileSystemStorage, self).url(name) 

In your settings.py file:

 DEFAULT_FILE_STORAGE = 'mymodule.CustomFileSystemStorage' 
+2
source

Source: https://habr.com/ru/post/1241842/


All Articles