Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SOLVED] Render markdown to HTML in template #83

Closed
alzearafat opened this issue Sep 2, 2017 · 11 comments
Closed

[SOLVED] Render markdown to HTML in template #83

alzearafat opened this issue Sep 2, 2017 · 11 comments

Comments

@alzearafat
Copy link

Hi there

I'm new here, and I'm having an issue rendering markdownx to HTML. I use this tag :

{{ post.content|safe }}

But it's not working. And I cannot find in the docs (Maybe I missed it?)

Thank you!

@adi-
Copy link
Member

adi- commented Sep 2, 2017

Most of the info how to properly setup markdownx is written in the docs. Please read it carefully.

To be able to give you some tips, more info with your issue is needed.

@alzearafat
Copy link
Author

alzearafat commented Sep 2, 2017

Thankyou @adi-

I already read the setup, and it's already working in admin/model. Now I want to display it in template, but not working. It not rendered as HTML, just plain text.

I usually just using this {{ post.content|safe }} tag in another WYSIWG editor and just workin fine :/

@alzearafat
Copy link
Author

alzearafat commented Sep 3, 2017

I just solved the issue, using template tag filter and 'markdownx.utils.markdownify' function. This is the steps (I dont know if this is the best way or not, but it works as I expected) :

  1. I create custom template tag filter :
from django import template
from markdownx.utils import markdownify

register = template.Library()

@register.filter
def show_markdown(text):
    return markdownify(text)
  1. And then just call it in template :
{{ post.content|show_markdown|safe }}
  1. Now it rendered as HTML

@alzearafat alzearafat changed the title Render markdown to HTML in template [SOLVED] Render markdown to HTML in template Sep 3, 2017
@xenatisch
Copy link
Collaborator

xenatisch commented Sep 3, 2017

There are plenty of options to achieve this; most of which take the same approach as you've manually implemented yourself.

We exemplify this in the documentations for the purpose of preview alongside the editor, but not for final stand-alone displays.

Thanks for sharing anyway!

@denizdogan
Copy link

Shouldn't there be a built-in way to render Markdown to HTML in Django templates?

Since the Markdown is previewed live in the admin, I assumed that there was a way to render Markdown in any Django template using a filter. I feel like that should definitely be in the scope of this project. The documentation could definitely be improved to clarify that we have to implement this on our own.

@tonykyriakidis
Copy link

I completely agree with @denizdogan , this should be in the scope of the project and the documentation could be improved. There should be a section on how to render markdown in templates with a proper/complete example.

@adi-
Copy link
Member

adi- commented Nov 7, 2018

Same as #77

@spatocode
Copy link

I solved the problem in this way. It's probably the easiest way.

model.py
from markdownx.models import MarkdownxField
from markdownx.utils import markdownify

class Article(models.Model):
author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
title = models.CharField(max_length=100, unique=True)
content = MarkdownxField()

def formatted_markdown(self):
     return markdownify(self.content)

template.html

{{ article.formatted_markdown|safe }}

@adi- adi- added the duplicate label Dec 26, 2019
@adi- adi- closed this as completed Dec 26, 2019
@JulioCoder
Copy link

I solved the problem in this way. It's probably the easiest way.

model.py
from markdownx.models import MarkdownxField
from markdownx.utils import markdownify

class Article(models.Model):
author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
title = models.CharField(max_length=100, unique=True)
content = MarkdownxField()

def formatted_markdown(self):
     return markdownify(self.content)

template.html

{{ article.formatted_markdown|safe }}

I worked for me, now is rendering ok, but the quotes are not working.

@jonathan-daniel
Copy link

For anyone coming here for a XSS safe solution: #163

@bitFez
Copy link

bitFez commented Dec 23, 2021

I tried the above solutions without much luck. After much fiddling, I managed to pull it off and decided to write this up since I can see other users above also had the same issues as myself.

@alzearafat
tutorial here: https://blog.devgenius.io/django-markdown-part-1-rendering-it-b61cbcbc833e#6d86-8337ea5bff80

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants