简体   繁体   English

如何将 md 文件转换为 HTML 并在 Django 中呈现它们

[英]How do I convert md files to HTML and render them in Django

I have some md files in an 'entries' folder in my django app folder and i wanna get them, covert them to HTML then render them.我的 django 应用程序文件夹的“条目”文件夹中有一些 md 文件,我想获取它们,将它们转换为 HTML 然后渲染它们。 This is my util.py这是我的 util.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


def convert_md(filename):
    """
    Converts given md file to html
    """
    #file= f"{filename}.md"

    file= default_storage.open(f"{filename}.md")
    md= file.read()
    html= md.markdown(md)
    return html
    

This is my views.py这是我的意见.py

def wiki(request, topic):
    if util.get_entry(topic) != None:
        html= util.convert_md(topic)
        return render(request, "encyclopedia/pages.html", {
            "title": topic, "body": html 
        })
    else:
        return render(request, "encyclopedia/pages.html", {
            "title": "ERROR", "body": "THIS PAGE IS NOT AVALIABLE."
        })

I also have...我也有...

path("wiki/<str:topic>", views.wiki)

in my urls.py but I'm still getting a FileNotFoundError at /wiki/Python error Note: I have pip installed markdown在我的 urls.py 但我仍然在 /wiki/Python 错误处收到 FileNotFoundError注意:我安装了 pip markdown

pip install markdown on your console/terminal. pip install markdown

Then try the following:然后尝试以下操作:

import markdown
f = open("MDFile.md","r")
f = f.read()
html = markdown.markdown(f)

where MDFile.md are your markdown files.其中 MDFile.md 是您的 markdown 文件。 html will have the HTML version of your markdown file. html将具有 markdown 文件的 HTML 版本。

I was facing same filenotfound exception, but i have found the answer finally.我面临同样的 filenotfound 异常,但我终于找到了答案。 Md files are located in entries folder, so you have to specify that in convert_Md function in utils.py just like in get_entry function. Md 文件位于条目文件夹中,因此您必须在 utils.py 中的 convert_Md function 中指定它,就像在 get_entry function 中一样。

file = default_storage.open(f"entries/{filename}.md", "r") I hope this will fix your issue as it did mine. file = default_storage.open(f"entries/{filename}.md", "r") 我希望这能像解决我的问题一样解决您的问题。 :) :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将文件从 Django 管理员直接上传到 Google Cloud Storage,以及如何在模板中呈现它们? - How do I upload files from Django admin directly to Google Cloud Storage, and how do I render them in the templates? 如何呈现此Django表单? - How do I render this Django form? 如何编写代码以使用 tifffile 库读取 TIFF 文件并将它们转换为 JPEG? - How do I write code to use the tifffile library to read TIFF files and convert them to JPEGs? 如何编写可读取doc / docx文件并将其转换为txt的python脚本? - How do I write a python script that can read doc/docx files and convert them to txt? 如何在python上创建带有字符串的html代码以在django模板上呈现 - How do I create an html code with a string on python to render on django template Django静态文件不显示在生产服务器中。 如何正确映射它们,以便可以看到管理GUI - Django static files do not display in production server. How can I map them correctly, so I can see the admin GUI 如何动态提供文件,然后使它们可在 Django 中下载? - How can I dynamically serve files and then make them downloadable in Django? 如何在 Django 中渲染 html 文件 - How to render html file in Django 如何将此SQL转换为Django QuerySet? - How do I convert this SQL to Django QuerySet? 如何将html文件中存储的许多片段收集到Django中的一个模板中? - How do I collect many pieces stored in html files into one template in Django?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM