如何在模板标签中实现markdown2?获取TemplateSyntaxError'md2'不是注册的标签库 [英] How to implement markdown2 in the template tags? Getting TemplateSyntaxError 'md2' is not a registered tag library

查看:95
本文介绍了如何在模板标签中实现markdown2?获取TemplateSyntaxError'md2'不是注册的标签库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照此来源上的说明进行操作,以使markdown2在模板标记中起作用。代码如下。

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',
]

注意:我尝试使用上面最初建议的'django_markdown2'但Django服务器将无法启动并给出由以下错误引起的异常链:

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

带有'markdown2'而不是'django_markdown2',服务器启动。

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>

因此,我已尝试根据本书进行所有操作。我还确保 markdown2.py 在路径中。

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

我不确定将<$ c $放在什么位置PYTHONPATH中的c> django_markdown2 。这样的环境变量在我的系统上不存在。我尝试创建所述的env变量,并将 django_markdown2 的值放进去,但是仍然没有任何乐趣。

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.

我使用Windows 7作为操作系统, Python版本3.8和Django版本3.0.8。以下是 pip冻结的输出:

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

我收到以下错误。

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.

推荐答案

我不认为您实际上已经安装了 django-markdown2 :它不会出现在 pip冻结输出中。听起来您好像已经尝试手动下载文件并修改 PYTHONPATH ,但是没有必要这样做。

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.

我希望您明确定义了依赖项,在这种情况下,您可以在 requirements.txt 中添加 django-markdown2 并运行 pip install -r requirements.txt 。如果没有,请考虑开始这种做法。

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.

如果您只想在当前环境中安装 django-markdown2 ,请运行

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

pip install django-markdown2

然后忘记关于手动下载任何内容或修改 PYTHONPATH

and forget about manually downloading anything or modifying PYTHONPATH.

希望这是在虚拟环境中使用的。

Hopefully this is with in a virtual environment.

然后,在安装库之后,更改您的 settings.py ,在您的<$中包含 django_markdown2 c $ c> INSTALLED_APPS 而不是 markdown2

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

这篇关于如何在模板标签中实现markdown2?获取TemplateSyntaxError'md2'不是注册的标签库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆