How do I implement markdown in Django 1.6 app?

前端 未结 3 1579
滥情空心
滥情空心 2020-12-25 14:58

I have a text field in models.py where I can input text content for a blog using the admin.

I want to be able to write the content for this text field i

相关标签:
3条回答
  • 2020-12-25 15:09

    Ah, I met with the same problem several months ago, and I found the easiest and most robust solution is to use Github Markdown API.

    Here is the code I use for my blog, which I believe will help you more or less. btw I use Python 3 so the encoding part may be different from Python 2.

    # generate rendered html file with same name as md
    headers = {'Content-Type': 'text/plain'}
    if type(self.body) == bytes:  # sometimes body is str sometimes bytes...
        data = self.body
    elif type(self.body) == str:
        data = self.body.encode('utf-8')
    else:
        print("somthing is wrong")
    
    r = requests.post('https://api.github.com/markdown/raw', headers=headers, data=data)
    # avoid recursive invoke
    self.html_file.save(self.title+'.html', ContentFile(r.text.encode('utf-8')), save=False)
    self.html_file.close()
    

    My code is hosted on github, you can find it here
    And My blog is http://laike9m.com.

    0 讨论(0)
  • 2020-12-25 15:23

    Thank you for your answers and suggestions, but I've decided to use markdown-deux.

    Here's how I did it:

    pip install django-markdown-deux

    Then I did pip freeze > requirements.txt to make sure that my requirements file was updated.

    Then I added 'markdown_deux' to the list of INSTALLED_APPS:

    INSTALLED_APPS = (
        ...
        'markdown_deux',
        ...
    )
    

    Then I changed my template index.html to:

    {% load markdown_deux_tags %}
    
    <html>
        <head>
            <title>My Django Blog</title>
        </head>
        <body>
            {% for post in post %}
            <h1>{{ post.title }}</h1>
            <h3>{{ post.pub_date }}</h3>
            {{ post.text|markdown }}
            {{ post.tags }}
            {% endfor %}
        </body>
    </html>
    
    0 讨论(0)
  • 2020-12-25 15:24

    You may use substitution of old markup implemented here - https://github.com/jamesturk/django-markupfield

    0 讨论(0)
提交回复
热议问题