Django Model: ValueError: Missing staticfiles manifest entry for “file_name.ext”

前端 未结 1 2044
渐次进展
渐次进展 2020-12-18 15:44

Before you mark it as duplicate, I have read ValueError: Missing staticfiles manifest entry for \'favicon.ico\' , and it does not solve my problem.

I have the follow

相关标签:
1条回答
  • 2020-12-18 16:28

    Solution:

    You can circumvent this issue and improve the code by moving the static() call out of the model field and changing the default value to the string "pledges/images/no-profile-photo.png". It should look like this:

    avatar_url = models.URLField(default='pledges/images/no-profile-photo.png')

    When you access avatar_url, use either

    1. (frontend / Django Templates option) {% static profile_instance.avatar_url %}, where profile_instance is a context variable referring to a Profile object.

    2. (backend / Python option) use static(profile_instance.avatar_url).

    Explanation:

    By using the result of static() for a default value, the app is putting a URL in the database that includes the STATIC_URL prefix -- which is like hard-coding it because data won't change when settings.py does. More generally, you shouldn't store the results of static() in the database at all.

    If you ensure that you're using the {% static %} tag or static() function each time you're accessing avatar_url for display on the frontend, STATIC_URL will still be added based on your environment config at runtime.

    This SO thread has a lot of good content on staticfiles

    Why the error is happening:

    It looks like you have a circular dependency:

    1. collectstatic needs to run in order to create manifest.json

    2. your application needs to load in order to run manage.py commands, which calls static()

    3. static() relies on an entry in manifest.json to resolve.

    0 讨论(0)
提交回复
热议问题