如何在Django 1.6应用中实现Markdown?

17

我在models.py中有一个文本字段,可以在管理员中输入博客的文本内容。

我想要以markdown格式编写此文本字段的内容,但我正在使用Django 1.6,django.contrib.markup已不再支持。

我找不到任何教程并运行添加markdown到Django 1.6的文本字段。有人能够查看我的.py文件,并帮助我将markdown实现到我的应用程序中吗?

models.py

from django.db import models

# Create your models here.
class Post(models.Model):
    title = models.CharField(max_length=200)
    pub_date = models.DateTimeField()
    text = models.TextField()
    tags = models.CharField(max_length=80, blank=True)
    published = models.BooleanField(default=True)

admin.py

from django.contrib import admin
from blogengine.models import Post

class PostAdmin(admin.ModelAdmin):
    # fields display on change list
    list_display = ['title', 'text']
    # fields to filter the change list with
    save_on_top = True
    # fields to search in change list
    search_fields = ['title', 'text']
    # enable the date drill down on change list
    date_hierarchy = 'pub_date'

admin.site.register(Post, PostAdmin)

index.html

<html>
    <head>
        <title>My Django Blog</title>
    </head>
    <body>
        {% for post in post %}
        <h1>{{ post.title }}</h1>
        <h3>{{ post.pub_date }}</h3>
        {{ post.text }}
        {{ post.tags }}
        {% endfor %}
    </body>
</html>

你有什么建议可以在Django 1.7中使用吗? - Ajoy
3个回答

26

感谢您的回答和建议,但我决定使用markdown-deux

以下是我的操作步骤:

pip install django-markdown-deux

然后执行pip freeze > requirements.txt以确保我的需求文件已更新。

接下来将'markdown_deux'添加到INSTALLED_APPS列表中:

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

然后我将我的模板 index.html 更改为:

{% 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>

我很高兴你解决了这个问题,但是你应该真的尝试一下GitHub API,至少它比标记语言好得多。我还没有尝试过markdown_deux,也许它也是一个不错的选择。 - laike9m
1
@laike9m 如果你只是在保存到数据库时渲染一次,那么使用API应该没问题。但如果你想用它来替代模板标签(这是我认为许多人使用Markdown的方式),那就会变得复杂。 - Jordan Reiter

7

啊,我几个月前也遇到相同的问题,后来发现最简单且最稳健的解决方案是使用Github Markdown API

以下是我在我的博客中使用的代码,我相信对你有些帮助。顺便说一下,我使用的是Python 3,所以编码部分可能与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()

我的代码托管在Github上,你可以在这里找到它。
而我的博客在http://laike9m.com


嗨 laike9m,我喜欢你的答案。 "self.body" 来自哪里?我看了你在 GitHub 上的博客,但还不是很清楚 'md_file' 是从哪里来的。谢谢! - Scott Skiles
1
@ScottSkiles md文件在这里定义:https://github.com/laike9m/My_Blog/blob/master/css3two_blog/models.py#L53,我提交了一个表单,在表单中填写原始的markdown文本,然后将文本保存到文件中,该文件就是md_file。但是我认为这不是本答案的重点,我的整个观点是使用GitHub Markdown API,仅此而已。 - laike9m

1

django-markupfield在Django 1.7上不受支持,特别是在迁移方面。 - bbengfort
现在它似乎已更新为 Django 1.8+。 - meshy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接