I can't understand the django-markdownx's usage

人盡茶涼 提交于 2019-12-06 01:20:11

问题


It is kind of embarrassing. But I can't seems to understand django-markdownx's documentation on how to use the app. I've followed the Getting Started guide, installed the app and dependencies, added jquery and it works in the Admin backend. But the template doesn't render text = MarkdownxField() as correctly formatted markdown, but as plain text.

I don't understand the part ...and then, include a form's required media in the template using {{ form.media }}:

<form method="POST" action="">{% csrf_token %}
    {{ form }}
</form>
{{ form.media }}

I tried to add that code just before the article tag in my template.

{% extends "base.html" %}
{% block content %}
    <form method="POST" action="">{% csrf_token %}
        {{ form }}
    </form>
    {{ form.media }}
    <article>
        <h1>{{ article.title }}</h1>
        <p>{{ article.text }}</p>
        <div>{{ article.pub_date }} {{ article.category }} {{ article.tag }}</div>
    </article>
{% endblock %}

But it doesn't fix it.

What I'm I missing? I know it is trivial. But I have no experience with forms.


app/models.py

from django.db import models
from django.urls import reverse
from markdownx.models import MarkdownxField

class Article(models.Model):
    title = models.CharField(max_length=250, verbose_name='title')
    text = MarkdownxField()
    pub_date = models.DateField(verbose_name='udgivelsesdato')
    category = models.ForeignKey(Category, verbose_name='kategori', null=True)
    tag = models.ForeignKey(Tag, verbose_name='mærke', null=True)

    def get_absolute_url(self):
        return reverse('article-detail', kwargs={'pk': self.pk})

    def __str__(self):
        return self.title

    class Meta():
        verbose_name = 'artikel'
        verbose_name_plural = 'artikler'

settings.py

INSTALLED_APPS = [
    ***
    'markdownx',
    'articles',
]

# Markdown extensions
MARKDOWNX_MARKDOWN_EXTENSIONS = [
    'markdown.extensions.sane_lists',
    'markdown.extensions.nl2br',
    'markdown.extensions.extra',
]

urls.conf

from django.conf.urls import url, include
from django.contrib import admin

urlpatterns = [
    url(r'^', include('articles.urls')),
    url(r'^markdownx/', include('markdownx.urls')),
    url(r'^admin/', admin.site.urls),
]

app/urls.py

from django.conf.urls import url
from django.views.generic.dates import ArchiveIndexView

from articles.models import Article
from articles.views import ArticleDetailView, ArticleListView

urlpatterns = [
    url(r'^arkiv/$',
        ArchiveIndexView.as_view(model=Article, date_field="pub_date"),
        name="article_archive"),
    url(r'^$', ArticleListView.as_view(), name='article_list'),
    url(r'(?P<pk>\d+)/$', ArticleDetailView.as_view(), name='article_detail'),
]

app/templates/app/article_list.html

{% extends "base.html" %}
{% block content %}
    <article>
    <h1>{{ article.title }}</h1>
    <p>{{ article.text }}</p>
    <div>{{ article.pub_date }}</div>
    </article>
{% endblock %}

app/views.py

from django.views.generic import ListView, DetailView

from articles.models import Article


class ArticleListView(ListView):
    model = Article


class ArticleDetailView(DetailView):
    model = Article

回答1:


Ok, I see your issue now (thank you for handling my questions:) ). django-markdownx provides you with the ability to have a Markdown editor inside of your forms. It does not, however, format that markdown when shown in a template - it's just plain text.

According to this issue on the project's GitHub you need to render the markdown in your views and then pass that to your template. Another way of doing this, which I would prefer if I were using this in a project:

from markdownx.utils import markdownify

class Article(models.Model):
    title = models.CharField(max_length=250, verbose_name='title')
    text = MarkdownxField()
    pub_date = models.DateField(verbose_name='udgivelsesdato')
    category = models.ForeignKey(Category, verbose_name='kategori', null=True)
    tag = models.ForeignKey(Tag, verbose_name='mærke', null=True)

    # Create a property that returns the markdown instead
    @property
    def formatted_markdown(self):
        return markdownify(self.text)

    def get_absolute_url(self):
        return reverse('article-detail', kwargs={'pk': self.pk})

    def __str__(self):
        return self.title

    class Meta():
        verbose_name = 'artikel'
        verbose_name_plural = 'artikler'

then in your template:

{% extends "base.html" %}
{% block content %}
    <article>
    <h1>{{ article.title }}</h1>
    <p>{{ article.formatted_markdown|safe }}</p>
    <div>{{ article.pub_date }}</div>
    </article>
{% endblock %}



回答2:


Another way, depending on needs is using a templatetag:

from django import template
from django.utils.safestring import mark_safe
from markdownx.utils import markdownify

register = template.Library()

@register.filter
def formatted_markdown(text):
    return mark_safe(markdownify(text))

and use it like

{{ article.text|formatted_markdown }}


来源:https://stackoverflow.com/questions/42416123/i-cant-understand-the-django-markdownxs-usage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!