I can't understand the django-markdownx's usage
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 %}
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 }}