Django : 'current_tags'는 유효한 태그 라이브러리가 아닙니다.
친구로부터받은 작은 Django 프로젝트가 있습니다. 코드는 그의 시스템에서 완벽하게 작동합니다. 그러나 내 시스템에서 서버를 실행할 때 다음 오류 메시지가 나타납니다.
/의 TemplateSyntaxError
'current_tags'는 유효한 태그 라이브러리가 아닙니다. 템플릿 라이브러리 current_tags를 찾을 수 없습니다. django.templatetags.current_tags를 시도했습니다.
문제는 html 파일의 한 줄에 있습니다.
{% load current_tags %}
이 똑같은 코드는 그의 시스템에서 오류없이 작동합니다. 그게 뭔데?
다음을 제안합니다.
(대부분) 태그 라이브러리의 종속성 중 하나를 설치하지 않았습니다.
current_tags.py
모듈 내부의 가져 오기를 확인하십시오 .확인에 등록 된 태그 라이브러리가 포함 된 응용 프로그램 확인
settings.py
아래를INSTALLED_APPS
.태그 라이브러리를 성공적으로 가져올 수 있는지 확인하십시오.
python manage.py shell >>> from app.templatetags import current_tags
이것은 다음 링크에서 권장하는 내용을 요약합니다. 즉, 오류 자체가 템플릿을 찾는 위치에 대해 오해하는 경향이 있다는 것입니다. 가져 오기시 오류를 자동으로 무시
current_tags.py
하므로 구문 오류가 있거나 ImportError를 발생시키는 또 다른 이유가있을 수 있습니다.
다른 모든 방법이 실패하면 다음 링크를 확인하십시오. http://www.b-list.org/weblog/2007/dec/04/magic-tags/
이 문제가 있었고 __init__.py
내 appname / templatetags / 디렉토리에 빈 파일 을 추가하여 수정했습니다 .
가능성은 많습니다.
- 개발자 서버를 재설정하지 않았습니다 .
- 당신은이 종속 루프 templatetag 파일을.
- 당신은 맞춤법이 틀린 ( '로드'등의 디렉토리, 폴더, 템플릿 이름) 무언가를.
- INSTALLED_APPS에 앱을 추가하는 것을 잊었습니다 .
서버 를 다시 시작하면 문제가 해결되었습니다. 그들은 문서에서 그것을 언급했을 것입니다.
나는 같은 오류가 발생했지만 다른 이유로 인해 (다른 사람이 같은 문제가 발생하는 경우) 알려 드리겠습니다.
나는 모든 것이 맞았지만 폴더 안에 사용자 정의 태그가 있었고 template_tags
오랜 검색 끝에이어야한다는 것을 알았고 templatetags
이제 작동합니다. 따라서 폴더 이름이 정확히templatetags
.
다음 구조가 있다고 가정합니다.
-애플리케이션 _ 이름
------- 템플릿 태그
-------------- 초기화 .py
--------------templates_extras.py
------- 초기화 .py
-------settings.py
-manage.py
다음 사항을 확인해야합니다.
"templatetags"가있는 응용 프로그램 자체가 실제로 settings.py의 INSTALLED_APPS에 설치됩니다 (예 : "Application_Name").
"templatetags"내에 존재하는 태그 모듈 자체는 settings.py의 INSTALLED_APP에 이미 설치되어 있습니다 (예 : "ApplicationName.templatetags.tempaltes_extras").
templatetags 디렉토리 아래에 " init .py" 가 있는지 확인하십시오.
서버를 다시 시작해야합니다
어떤 경우에는 생성 된 모든 * .pyc가 작동하지 않으면 제거하고 다시 시도해야합니다.
"사용자 지정 태그"는 유효한 태그 라이브러리 오류가 아닙니다. 사용자 지정 태그가 앱에로드되지 않았기 때문에 더 자주 발생합니다.
"custom template tag"가있는 폴더 안에 빈 init .py를 넣고 터미널에서 아래 코드를 실행하여 사용자 정의 템플릿 태그를로드합니다.
touch __init__.py
templatetags 폴더가 python으로 초기화되었는지 확인하십시오. 확실하지 않은 경우 모든 파일을 백업하십시오.
모든 파일을 제거하고 templatetags 폴더 안에 init .py 파일 만 생성 한 다음 서버를 다시 시작합니다.
이제 폴더가 Python 아래에 있으므로 작업을 수행하십시오.
이것은 나를 위해 작동합니다 ...
저에게는 load
태그 에서 라이브러리 이름을 따옴표로 묶는 실수였습니다 .
잘못된: {% load 'library_name' %}
옳은: {% load library_name %}
다른 답변도 참조하십시오. 나는 여기에 착륙하기 전에 그 몇 가지 문제도 해결했습니다.
이것에 직면 한 다른 사람들을 위해. 앱 이름이 MyApp
있고 태그 폴더 이름이 templatetags
다음 과 같다고 가정합니다 settings.py
.
INSTALLED_APPS = [
'MyApp',
'MyApp.templatetags'
]
django 앱과 앱 패키지 아래에있는 태그 폴더가 모두 필요합니다.
-> MyApp
---> models.py
---> views.py
---> templatetags
-----> __init__.py
-----> app_filters.py
그리고 템플릿 파일에서 :
{% load app_filters %}
또한 app_filters.py
다음과 같습니다.
# coding=utf-8
from django import template
register = template.Library()
@register.filter(name='get_item')
def get_item(dictionary, key):
return dictionary.get(key)
위의 모든 단계를 확인하면 문제를 찾을 수 있습니다.
load
진술이 정확한지 확인하십시오 . 앱 이름이 아니라 파일 이름이어야합니다. 예를 들어이 앱이있는 경우 :
appname
├── __init__.py
├── templatetags
│ ├── __init__.py
│ └── foobarfilename.py
그런 다음이를 템플릿에 넣어야합니다.
{% load foobarfilename %}
물론 다른 답변도 확인해야합니다.
템플릿 태그를 생성하고 settings.INSTALLED_APPS에 설치된 앱 내 'templatetags'패키지 내에 있어야합니다. dev-server를 다시 시작해야합니다.
Maybe someone will find this useful: somehow I had named the directory 'templatetags '
instead of 'templatetags'
, that is -- with a space at the end. Took hours to finally realize.
All of the advice listed here didn't help me. So in my specific case the problem was that the templatetag had to be loaded from a third-party app, and I manually copied source folder with that app into src
folder in my virtualenv. Then I ran python setup.py install
inside that folder. After that django could not load this module.
Then I removed the source and installed folder of this app and installed it using pip install -r requirements.txt
after adding a relevant line into requirements.txt
file. It was downloaded into the src
folder, installed and everything began working properly. Hope this helps someone.
In my case I have created library instance using tag variable instead of register variable
tag = template.Library()
But it should be
register = template.Library()
To be a valid tag library, the module must contain a module-level variable named register that is a template.Library instance, in which all the tags and filters are registered
In my case the problem was, I was using {% load filter_method_name %}
I had to change to {% filename %}
In my case it was - I am using
@register.inclusion_tag('template-to-use.html')
I forgot to create that template and right away it started working. I know above answers are more related to most of the issues - but hope maybe someone find it useful. It should have gotten to me:
Template does not exist
but it did not and this worked.
참고URL : https://stackoverflow.com/questions/5493776/django-current-tags-is-not-a-valid-tag-library
'developer tip' 카테고리의 다른 글
Android에서 HTML 태그를 제거하거나 이스케이프하는 방법 (0) | 2020.10.20 |
---|---|
UIImagePickerController에서 여러 이미지를 선택하는 방법 (0) | 2020.10.20 |
DateTimeField가 관리 시스템에 표시되지 않습니다. (0) | 2020.10.20 |
포인터에 대한 포인터 대 일반 포인터 (0) | 2020.10.20 |
유효한 연도를 테스트하기위한 정규식 일치 (0) | 2020.10.20 |