__init__()接受1个位置参数,但在向markdown添加Latex支持时给出了2个



我遵循本教程将LaTeX支持添加到markdown中,并收到以下错误:

环境:

请求方法:GET请求URL:http://localhost/posts/ttttt/

Django版本:2.2.10 Python版本:3.7.3已安装的应用程序:['home','search','info','list','streams','post','faq',"registration"、"snippet"、"wagtailmd"、"wagtailcodeblock",'wagtail.contrib.forms','wagtail.contrib.redirects',"wagtail.embeds"、"wagtain.sites"、"Wagtain.users","wagtail.sippets","wagtail.docents","wagtail.images",'wagtail.search','wagtail.admin','wagtail.core','wagtail.contrib.modeladmin','wagtail.contrib.styleguide','modelcluster','taggit','django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','django.contrib.sites',"django.contrib.humanize","allauth","allauth.account","allauth.socialaccount"、"crispy_forms"、"wagtailmenus"one_answers"captcha",'wagtailcaptcha']已安装的中间件:['django.contrib.sessions.middleware.SessionMiddleware','django.middleware.common.CommonMiddleware','django.middleware.csrf.CsrfViewMiddleware','django.contrib.auth.middleware.AuthenticationMiddleware','django.contrib.messages.middleware','django.middleware.cackjacketing.XFrameOptionsMiddleware','django.middleware.security.SecurityMiddleware','wagtail.core.middleware.SiteMiddleware','wagtail.contrib.redirects.middleware']

模板错误:在模板中C: \用户\腹部\ OneDrive \桌面\项目\网站\模板\base.html,第0行出现错误init()占用了1个位置参数,但有2个给定1:{%load static wagtailuserbar%}2:{%load menu_tags%}3:4:5:6:
7:8:9:{%块标题%}10:{%ifself.seo_title%}endif%}

追溯:

文件"C:\Users\abbi\OneDrive \Desktop\project.venv\lib\site packages\django\core\handlers\exception.py"在内部34.response=get_response(request)

文件"C:\Users\abbi\OneDrive \Desktop\project.venv\lib\site-packages\wagtail\core\models.py"在_get_response中1254.response=response.render()

文件"C:\Users\abbi\OneDrive \Desktop\project.venv\lib\site packages\django\template\response.py"渲染中106.self.content=self.rendered_content

文件"C:\Users\abbi\OneDrive \Desktop\project.venv\lib\site packages\django\template\response.py"在rendered_content中83.content=template.render(context,self.request)

文件"C:\Users\abbi\OneDrive \Desktop\project.venv\lib\site packages\django\template\backends\django.py"渲染中61.返回self.template.render(上下文)

文件"C:\Users\abbi\OneDrive \Desktop\project.venv\lib\site packages\django\template\base.py"渲染中171.回归自我_呈现(上下文)

文件"C:\Users\abbi\OneDrive \Desktop\project.venv\lib\site packages\django\template\base.py"在应答器中(_R)163.return-self.nodelist.render(上下文)

文件"C:\Users\abbi\OneDrive \Desktop\project.venv\lib\site packages\django\template\base.py"渲染中937.bit=node.render_anotated(上下文)

文件"C:\Users\abbi\OneDrive \Desktop\project.venv\lib\site packages\django\template\base.py"在render_anotated中904.返回self.render(上下文)

文件"C:\Users\abbi\OneDrive \Desktop\project.venv\lib\site packages\django\template\loader_tags.py"渲染中150.返回compiled_parent_呈现(上下文)

文件"C:\Users\abbi\OneDrive \Desktop\project.venv\lib\site packages\django\template\base.py"在应答器中(_R)163.return-self.nodelist.render(上下文)

文件"C:\Users\abbi\OneDrive \Desktop\project.venv\lib\site packages\django\template\base.py"渲染中937.bit=node.render_anotated(上下文)

文件"C:\Users\abbi\OneDrive \Desktop\project.venv\lib\site packages\django\template\base.py"在render_anotated中904.返回self.render(上下文)

文件"C:\Users\abbi\OneDrive \Desktop\project.venv\lib\site packages\django\template\loader_tags.py"渲染中62.result=block.nodelist.render(上下文)

文件"C:\Users\abbi\OneDrive \Desktop\project.venv\lib\site packages\django\template\base.py"渲染中937.bit=node.render_anotated(上下文)

文件"C:\Users\abbi\OneDrive \Desktop\project.venv\lib\site packages\django\template\base.py"在render_anotated中904.返回self.render(上下文)

文件"C:\Users\abbi\OneDrive \Desktop\project.venv\lib\site packages\django\template\base.py"渲染中987.output=self.filter_expression.resolve(上下文)

文件"C:\Users\abbi\OneDrive \Desktop\project.venv\lib\site packages\django\template\base.py"下决心698.new_obj=函数(obj,*arg_vals)

文件"C:\Users\腹部\OneDrive \Desktop\project\wagtailmd\templatetags\wagtailmd.py"在markdown_filter中14.output_format='html5'

文件"C:\Users\abbi\OneDrive \Desktop\project.venv\lib\site packages\markdown\core.py"降价387.md=降价(**kwargs)

文件"C:\Users\abbi\OneDrive \Desktop\project.venv\lib\site packages\markdown\core.py"在init97中。configs=kwargs.get('extension_configs',{})

文件"C:\Users\abbi\OneDrive \Desktop\project.venv\lib\site packages\markdown\core.py"在寄存器扩展中123.ext=self.build_extension(ext,configs.get(ext,{}))

文件"C:\Users\abbi\OneDrive \Desktop\project.venv\lib\site packages\markdown\core.py"内部构建扩展178.返回模块.makeExtension(**configs)

文件"C:\Users\abbi\OneDrive \Desktop\project\wagtailmd\mdx\mdx_mathjax.py"in makeExtension23.返回MathJaxExtension(配置)

异常类型:/posts/ttttt/异常值:init()处的TypeError取1个位置参数,但2个已给定

mdx_mathjax.py:

import markdown
import cgi
class MathJaxPattern(markdown.inlinepatterns.Pattern):
def __init__(self, md):
markdown.inlinepatterns.Pattern.__init__(
self, r'(?<!\)($$?)(.+?)2', md)
def handleMatch(self, m):
# Pass the math code through, unmodified except for basic entity 
# substitutions. 
# Stored in htmlStash so it doesn't get further processed by Markdown.
text = cgi.escape(m.group(2) + m.group(3) + m.group(2))
return self.markdown.htmlStash.store(text)
class MathJaxExtension(markdown.Extension):
def extendMarkdown(self, md, md_globals):
# Needs to come before escape matching because  is pretty important
# in LaTeX
md.inlinePatterns.add('mathjax', MathJaxPattern(md), '<escape')
def makeExtension(configs=[]):
return MathJaxExtension(configs)

wagtailmd.py:

import markdown
from django import template
register = template.Library()
@register.filter(name='markdown')
def markdown_filter(value):
return markdown.markdown(
value,
extensions=[
'wagtailmd.mdx.mdx_mathjax',
],
output_format='html5'
)

我的post_page.html

{% extends 'base.html' %}
{% load static %}
{% load wagtailcore_tags wagtailimages_tags wagtailmd %}

{% block content %}
<!-- main -->
<main>
<div class="uk-container uk-container-small" style="margin: 0 auto;">
<article>
<div class="math-editor">
{{ self.math_editor|markdown|safe }}
</div>
</article>
</div>
</main>
<!-- /main -->
{% endblock content %}
{% block extra_js %}
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
extensions: ["tex2jax.js"],
jax: ["input/TeX", "output/HTML-CSS"],
tex2jax: {
inlineMath: [['$','$']],
displayMath: [['$$','$$']] ,
processEscapes: true
},
"HTML-CSS": { availableFonts: ["TeX"] }
});
</script>
<script type="text/javascript"
src="//cdn.mathjax.org/mathjax/latest/MathJax.js">
</script>
{% endblock %}

谢谢。

您的代码中存在实现错误。根据markdown的文档,您应该定义要为自定义Extension提供的参数。

如果你还没有任何配置,你应该使用下面的东西:

def makeExtension(configs={}):
return MathJaxExtension(**configs)

完整的实现应该是这样的:

import markdown
import cgi
class MathJaxPattern(markdown.inlinepatterns.Pattern):
def __init__(self, md):
markdown.inlinepatterns.Pattern.__init__(
self, r'(?<!\)($$?)(.+?)2', md)
def handleMatch(self, m):
# Pass the math code through, unmodified except for basic entity 
# substitutions. 
# Stored in htmlStash so it doesn't get further processed by Markdown.
text = cgi.escape(m.group(2) + m.group(3) + m.group(2))
return self.markdown.htmlStash.store(text)
class MathJaxExtension(markdown.Extension):
def __init__(self, **kwargs):
self.config = {'option1' : ['value1', 'description1'],
'option2' : ['value2', 'description2'] }
super(MathJaxExtension, self).__init__(**kwargs)
def extendMarkdown(self, md, md_globals):
# Needs to come before escape matching because  is pretty important
# in LaTeX
md.inlinePatterns.add('mathjax', MathJaxPattern(md), '<escape')
def makeExtension(configs={}):
return MathJaxExtension(**configs)

最新更新