I can not understand the use of django-markdownx

This is awkward. But I can not understand the django-markdownx documentation on how to use the application. I completed the Getting Started guide, installed the application and the dependencies, added jquery and works in the admin backend. But the template does not display text = MarkdownxField() as a properly formatted markdown, but as plain text.

I don’t understand this part ... and then include in the template the form required by the template using {{ form.media }} :

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

I tried to add this code immediately 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 this does not correct.

What am I missing? I know this is trivial. But I have no experience in 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 
+6
source share
2 answers

Ok, now I see your problem (thanks for asking my questions :)). django-markdownx provides you with the ability to have a Markdown editor inside your forms. However, it does not format this markdown when displayed in the template - it is just text.

In accordance with this problem in the GitHub project, you need to display the markdown in your views and transfer it to your template. Another way to do this, which I would prefer if I used 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 %} 
+15
source

Another way, depending on your needs, is to use a template tag:

 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 as

 {{ article.text|formatted_markdown }} 
0
source

Source: https://habr.com/ru/post/1015213/


All Articles