如何在Django 1.6应用中实现markdown? [英] How do I implement markdown in Django 1.6 app?
问题描述
我在models.py
中有一个文本字段,可以在其中使用管理员输入博客的文本内容.
I have a text field in models.py
where I can input text content for a blog using the admin.
我希望能够以markdown格式写入此文本字段的内容,但是我使用的是Django 1.6,并且不再支持django.contrib.markup
.
I want to be able to write the content for this text field in markdown format, but I'm using Django 1.6 and django.contrib.markup
is not supported anymore.
我找不到任何有教程的地方,并且会在Django 1.6的文本字段中添加markdown来运行.有人可以查看我的.py
文件并帮助我对我的应用程序实施降价促销吗.
I can't find anywhere that has a tutorial and runs through adding markdown to a text field in Django 1.6. Can someone look at my .py
files and help me implement markdown to my app.
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>
推荐答案
Thank you for your answers and suggestions, but I've decided to use markdown-deux.
这是我的做法:
pip install django-markdown-deux
然后我做了pip freeze > requirements.txt
以确保我的需求文件已更新.
Then I did pip freeze > requirements.txt
to make sure that my requirements file was updated.
然后我将'markdown_deux'添加到INSTALLED_APPS列表:
Then I added 'markdown_deux' to the list of 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>
这篇关于如何在Django 1.6应用中实现markdown?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!