Python

[Django] 'staticfiles' is not a registered tag library

Share this for your friends.

[Django] 'staticfiles' is not a registered tag library

'staticfiles' is not a registered tag library

Djangoでこのようなエラーが発生した。
今回はこの 'staticfiles' エラーの原因と対処法を紹介する。


エラー現象

PythonのWEBフレームワークDjangoで以下のコマンドでWEBサービスを起動する。

python manage.py runserver

するとデフォルトでは http://localhost:8000/ にWEBサービスが立ち上がる。

そしてWEBブラウザでアクセスすると以下のエラーメッセージが表示される。

TemplateSyntaxError at /xxx/
'staticfiles' is not a registered tag library. Must be one of:
admin_list
admin_modify
admin_urls
cache
i18n
l10n
log
static
tz


エラー原因

エラーメッセージからある程度推測するに

SyntaxError なので構文のエラー

そして 'staticfiles' is not a registered tag library から 'staticfiles' が tag library に無いが無理に呼ぼうとしてエラーになっていると推測できる。


そしてDjangoの公式ドキュメントを見ると、'staticfiles' がDjango3.0で削除になっているのがわかる。

django.contrib.staticfiles.templatetags.staticfiles.static() is removed.


対処法

対処法としては 'staticfiles' の宣言を別の文言で置き換える。
'staticfiles'templates フォルダの中のhtmlファイルで宣言されていることが多いので、htmlファイルを開いて中の staticfilesstatic に置き換える。

before

{% load staticfiles %}


after

{% load static %}





まとめ

'staticfiles' is not a registered tag library
のエラー原因は Django3.0 で staticfiles が廃止されたこと。
代わりに static を宣言するとエラーを回避できる。





関連図書



Share this for your friends.

ITipsと同じようなブログを作る方法

ブログに興味がありますか?

もしブログに興味がある場合は↓このページ↓を参考にすれば、ITipsと同じ構成でブログを作ることができます

サーバー、ドメイン、ASPと【ブログに必要なものは全て】このページに書きました。
同じ構成でブログ作るのはいいけど、記事はマネしないでネ (TДT;)

ランキング参加中

にほんブログ村 IT技術ブログへ

他にもブログやSNSで紹介してくれると励みになります。

はてブのコメントで酷評されると泣きます(´;ω;`)

-Python
-, ,

© 2024 ITips