Je reçois un ValueError
lors de l'exécution de python manage.py test
. Mon projet s'appelle fellow_go
, et je travaille actuellement sur une application appelée pickup
.
Veuillez noter que cette erreur est ajoutée dans un commit relativement récent de Django: Correction # 24452 - Correction de la correction de HashedFilesMixin avec des chemins imbriqués. .
======================================================================
ERROR: test_view_url_exists_at_desired_location (pickup.tests.test_view.HomePageViewTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/sunqingyao/PycharmProjects/fellow_go/pickup/tests/test_view.py", line 10, in test_view_url_exists_at_desired_location
resp = self.client.get('/', follow=True)
File "/Users/sunqingyao/Envs/Django_tutorial/lib/python3.6/site-packages/Django/test/client.py", line 536, in get
**extra)
File "/Users/sunqingyao/Envs/Django_tutorial/lib/python3.6/site-packages/Django/test/client.py", line 340, in get
return self.generic('GET', path, secure=secure, **r)
File "/Users/sunqingyao/Envs/Django_tutorial/lib/python3.6/site-packages/Django/test/client.py", line 416, in generic
return self.request(**r)
File "/Users/sunqingyao/Envs/Django_tutorial/lib/python3.6/site-packages/Django/test/client.py", line 501, in request
six.reraise(*exc_info)
File "/Users/sunqingyao/Envs/Django_tutorial/lib/python3.6/site-packages/Django/utils/six.py", line 686, in reraise
raise value
File "/Users/sunqingyao/Envs/Django_tutorial/lib/python3.6/site-packages/Django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/Users/sunqingyao/Envs/Django_tutorial/lib/python3.6/site-packages/Django/core/handlers/base.py", line 217, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/sunqingyao/Envs/Django_tutorial/lib/python3.6/site-packages/Django/core/handlers/base.py", line 215, in _get_response
response = response.render()
File "/Users/sunqingyao/Envs/Django_tutorial/lib/python3.6/site-packages/Django/template/response.py", line 107, in render
self.content = self.rendered_content
File "/Users/sunqingyao/Envs/Django_tutorial/lib/python3.6/site-packages/Django/template/response.py", line 84, in rendered_content
content = template.render(context, self._request)
File "/Users/sunqingyao/Envs/Django_tutorial/lib/python3.6/site-packages/Django/template/backends/Django.py", line 66, in render
return self.template.render(context)
File "/Users/sunqingyao/Envs/Django_tutorial/lib/python3.6/site-packages/Django/template/base.py", line 207, in render
return self._render(context)
File "/Users/sunqingyao/Envs/Django_tutorial/lib/python3.6/site-packages/Django/test/utils.py", line 107, in instrumented_test_render
return self.nodelist.render(context)
File "/Users/sunqingyao/Envs/Django_tutorial/lib/python3.6/site-packages/Django/template/base.py", line 990, in render
bit = node.render_annotated(context)
File "/Users/sunqingyao/Envs/Django_tutorial/lib/python3.6/site-packages/Django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/Users/sunqingyao/Envs/Django_tutorial/lib/python3.6/site-packages/Django/template/loader_tags.py", line 177, in render
return compiled_parent._render(context)
File "/Users/sunqingyao/Envs/Django_tutorial/lib/python3.6/site-packages/Django/test/utils.py", line 107, in instrumented_test_render
return self.nodelist.render(context)
File "/Users/sunqingyao/Envs/Django_tutorial/lib/python3.6/site-packages/Django/template/base.py", line 990, in render
bit = node.render_annotated(context)
File "/Users/sunqingyao/Envs/Django_tutorial/lib/python3.6/site-packages/Django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/Users/sunqingyao/Envs/Django_tutorial/lib/python3.6/site-packages/Django/templatetags/static.py", line 105, in render
url = self.url(context)
File "/Users/sunqingyao/Envs/Django_tutorial/lib/python3.6/site-packages/Django/templatetags/static.py", line 102, in url
return self.handle_simple(path)
File "/Users/sunqingyao/Envs/Django_tutorial/lib/python3.6/site-packages/Django/templatetags/static.py", line 117, in handle_simple
return staticfiles_storage.url(path)
File "/Users/sunqingyao/Envs/Django_tutorial/lib/python3.6/site-packages/Django/contrib/staticfiles/storage.py", line 162, in url
return self._url(self.stored_name, name, force)
File "/Users/sunqingyao/Envs/Django_tutorial/lib/python3.6/site-packages/Django/contrib/staticfiles/storage.py", line 141, in _url
hashed_name = hashed_name_func(*args)
File "/Users/sunqingyao/Envs/Django_tutorial/lib/python3.6/site-packages/Django/contrib/staticfiles/storage.py", line 432, in stored_name
raise ValueError("Missing staticfiles manifest entry for '%s'" % clean_name)
ValueError: Missing staticfiles manifest entry for 'favicon.ico'
----------------------------------------------------------------------
fellow_go/settings.py
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
# ......
# Simplified static file serving.
# https://warehouse.python.org/project/whitenoise/
STATICFILES_STORAGE = 'whitenoise.Django.GzipManifestStaticFilesStorage'
fellow_go/urls.py
urlpatterns = i18n_patterns(
url(r'^$', HomePageView.as_view(), name='index'),
url(r'^pickup/', include('pickup.urls')),
url(r'^accounts/', include('Django.contrib.auth.urls')),
url(r'^admin/', admin.site.urls),
prefix_default_language=False
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
fellow_go/pickup/views.py
class HomePageView(TemplateView):
template_name = 'index.html'
fellow_go/templates/index.html
<link rel="icon" href="{% static "favicon.ico" %}">
fellow_go/pickup/tests/test_view.py
class HomePageViewTest(TestCase):
def test_view_url_exists_at_desired_location(self):
resp = self.client.get('/', follow=True)
self.assertEqual(resp.status_code, 200)
Tout ce que j'ai un favicon.ico
fichier:
Étrangement, aucune erreur ne se produit avec python manage.py runserver
:
/Users/sunqingyao/Envs/Django_tutorial/bin/python3.6 /Users/sunqingyao/PycharmProjects/fellow_go/manage.py runserver 8000
Performing system checks...
System check identified no issues (0 silenced).
May 24, 2017 - 22:09:25
Django version 1.11.1, using settings 'fellow_go.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[24/May/2017 22:09:28] "GET / HTTP/1.1" 200 6276
[24/May/2017 22:09:28] "GET /static/css/style.min.css HTTP/1.1" 200 2474
[24/May/2017 22:09:28] "GET /static/css/ie10-viewport-bug-workaround.css HTTP/1.1" 200 430
[24/May/2017 22:09:28] "GET /static/js/ie10-viewport-bug-workaround.js HTTP/1.1" 200 685
[24/May/2017 22:09:28] "GET /static/js/opt-in.js HTTP/1.1" 200 511
[24/May/2017 22:09:28] "GET /static/css/datetimepicker.css HTTP/1.1" 200 12351
[24/May/2017 22:09:28] "GET /static/js/bootstrap-datetimepicker.js HTTP/1.1" 200 55741
[24/May/2017 22:09:35] "GET /static/favicon.ico HTTP/1.1" 200 766
Not Found: /Apple-touch-icon-precomposed.png
[24/May/2017 22:09:35] "GET /Apple-touch-icon-precomposed.png HTTP/1.1" 404 2678
Not Found: /Apple-touch-icon.png
[24/May/2017 22:09:35] "GET /Apple-touch-icon.png HTTP/1.1" 404 2642
S'il vous plaît dites-moi ce qui ne va pas avec mon code.
Essayez de courir:
python manage.py collectstatic
Le test fonctionne-t-il maintenant? Si tel est le cas, cela pourrait être la configuration posant problème:
STATICFILES_STORAGE = 'whitenoise.Django.GzipManifestStaticFilesStorage'
En relation:
https://stackoverflow.com/a/32347324/2596187
Consultez la documentation Django: https://docs.djangoproject.com/fr/1.11/ref/contrib/staticfiles/#Django.contrib.staticfiles.storage.ManifestStaticFilesStorage. manifeste_strict
Si vous souhaitez continuer à utiliser le module WhiteNoise dans votre projet Django 1.11 (ou plus récent) tout en évitant cette erreur "Entrée de fichier de données statique manquantes", vous devez le désactiver. le manifest_strict
attribut par héritage, comme indiqué dans documentation Django .
Comment y arriver?
Tout d'abord, créez un storage.py
fichier dans votre répertoire de projet:
from whitenoise.storage import CompressedManifestStaticFilesStorage
class WhiteNoiseStaticFilesStorage(CompressedManifestStaticFilesStorage):
manifest_strict = False
Deuxièmement, éditez le STATICFILES_STORAGE
constante dans votre settings.py
fichier pour désigner cette nouvelle classe, tels que:
STATICFILES_STORAGE = 'my_project.storage.WhiteNoiseStaticFilesStorage'
Cela ne se produit pas nécessairement avec le package whitenoise
. En changeant STATIC_STORAGE
à Django.contrib.staticfiles.storage.ManifestStaticFilesStorage
générera la même erreur lors de l'exécution des tests commençant par Django 1.11 .
Cela se produit parce que ManifestStaticFilesStorage
s'attend à ce que staticfiles.json
d'exister et contenir le fichier demandé. Vous pouvez le confirmer en lançant ./manage.py collectstatic
et essayer à nouveau.
Vous ne voyez généralement pas cette erreur dans le développement parce que quand DEBUG == True
, ManifestStaticFilesStorage
bascule vers non-haché urls.
Pour surmonter cela, vous devez vous assurer que:
STATICFILES_STORAGE = 'Django.contrib.staticfiles.storage.StaticFilesStorage'
Quel est le défaut.
Une solution serait de remplacer les paramètres de la classe de test:
from Django.test import TestCase, override_settings
@override_settings(STATICFILES_STORAGE='Django.contrib.staticfiles.storage.StaticFilesStorage')
class MyTest(TestCase):
pass
ou la méthode:
from Django.test import TestCase, override_settings
class MyTest(TestCase):
@override_settings(STATICFILES_STORAGE='Django.contrib.staticfiles.storage.StaticFilesStorage')
def test_something(self):
pass