my views.py
file中的一个函数转换标记文件并将HTML返回给另一个函数,该函数用于显示当前条目(entry()
).在entry()
中,我有一个字典,它允许HTML模板访问转换的内容.然而,当<h1>
和<p>
等标签显示在页面上而不是隐藏时.
所以
<h1>CSS</h1> <p>CSS is a language that can be used to add style to an <a href="/wiki/HTML">HTML</a> page.</p>
显示,而不是
CSS
CSS是一种可用于为HTML页添加样式的语言.
--
如何go 掉页面上的标记,并将它们实际用于HTML文件中?
进入html:
{% block body %}
<div class="entry-container">
<div class="left">
{{ entry }}
</div>
<div class="right">
<a href="{% url 'edit' %}" class="edit-btn">
<button class="edit">EDIT</button>
</a>
</div>
</div>
{% endblock %}
意见.py:
import markdown
from . import util
def entry(request, name):
entry = util.get_entry(name)
converted = convert(entry)
if util.get_entry(name) is not None:
context = {
'entry': converted,
'name': name
}
global current_entry
current_entry = name
return render(request, 'encyclopedia/entry.html', context)
else:
return render(request, "encyclopedia/404.html")
def convert(entry):
return markdown.markdown(entry)
URL.py:
path('<str:name>', views.entry, name='entry'),
有用的py:
def get_entry(title):
"""
Retrieves an encyclopedia entry by its title. If no such
entry exists, the function returns None.
"""
try:
f = default_storage.open(f"entries/{title}.md")
return f.read().decode("utf-8")
except FileNotFoundError:
return None