简体   繁体   中英

How to implement markdown2 in the template tags? Getting TemplateSyntaxError 'md2' is not a registered tag library

I followed instructions at this source for getting markdown2 to work in the template tags. The code is below.

settings.py :

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'wikiencyc',
    'markdown2',
]

Note: I tried using the originally suggested 'django_markdown2' above but the Django server would not start and give a chain of exceptions originating with the following error:

'<frozen importlib._bootstrap>'
from line 200 in Python38-32/lib/pathlib.py

With 'markdown2' instead of 'django_markdown2' , the server started.

entry_detail.html :

{% load md2 %}
<h4>{{ entry.subject }} </h4>
<br><br>
<p>
    {{ entry.content|markdown:"safe" }}
</p>

So, I have tried to do everything according to book. I also made sure that markdown2.py is in the path.

I am not sure what they mean by putting django_markdown2 in the PYTHONPATH. Such an environment variable does not exist on my system. I tried creating said env variable and putting the value of django_markdown2 , but still no joy.

I am using Windows 7 as OS, Python version 3.8, and Django version 3.0.8. Below is the output of pip freeze :

asgiref==3.2.10
cachelib==0.1.1
click==7.1.2
Django==3.0.8
Flask==1.1.2
Flask-Session==0.3.2
itsdangerous==1.1.0
Jinja2==2.11.2
markdown2==2.3.9
MarkupSafe==1.1.1
Pillow==7.2.0
psycopg2-binary==2.8.5
pytz==2020.1
SQLAlchemy==1.3.18
sqlparse==0.3.1
Werkzeug==1.0.1

I get the following error.

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

What am I doing wrong? Please provide the specifics: which file to edit, what libs to use or import, and exactly what to type, the exact syntax, and where. The devil is in the details.

I think it is supposed to be

'django_markdown2',

Instead of

'markdown2',

In settings.py

This is the same for Adding it to the path

I don't think you've actually installed django-markdown2 : it doesn't appear in your pip freeze output. It sounds like you've tried to manually download the file and modify your PYTHONPATH , but there should be no need to do that.

I hope you are explicitly defining your dependencies, in which case you could add django-markdown2 to your requirements.txt and run pip install -r requirements.txt . If not, please consider starting this practice.

If you just want to install django-markdown2 in your current environment, run

pip install django-markdown2

and forget about manually downloading anything or modifying PYTHONPATH .

Hopefully this is with in a virtual environment.

Then, once you've installed the library, change your settings.py to include django_markdown2 in your INSTALLED_APPS instead of markdown2 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM