Reputation: 941
After researching my problem ("HTML_REMOVED" when putting inline html in markdown), I came across this. When I try this solution, however, it gives me a syntax error:
Exception Type: TemplateSyntaxError
Exception Value:
custom_markdown requires 0 arguments, 1 provided
I am using Django 1.6.5.
{% extends "blogengine/includes/post.html" %}
{% load custom_markdown %}
{% block content %}
<div class="post">
<h1 id="post-title">{{ object.title }}</h1>
<p id="post-date">{{ object.pub_date }}</p>
<div id="post-body">
{{ object.text|custom_markdown:"safe" }}
</div>
</div>
{% endblock %}
import markdown
from django import template
from django.template.defaultfilters import stringfilter
from django.utils.encoding import force_unicode
from django.utils.safestring import mark_safe
register = template.Library()
@register.filter(is_safe=True)
@stringfilter
def custom_markdown(value):
extensions = ["nl2br", ]
return mark_safe(markdown.markdown(force_unicode(value),
extensions,
safe_mode=True,
enable_attributes=False))
Clarification: My question is: How to get the markdown to accept inline HTML? I am aware the safe is causing the syntax error.
Addition:
When adding a simple tag <a href="www.google.com">test</a>
, this is the result:
Upvotes: 2
Views: 718
Reputation: 941
Ok, I have actually figured out the solution is the opposite of what we've been discussing. I discovered here that enabling safe mode disables html. So after changing my custom_markdown.py to the following, it worked.
import markdown
from django import template
from django.template.defaultfilters import stringfilter
from django.utils.encoding import force_unicode
from django.utils.safestring import mark_safe
register = template.Library()
@register.filter(is_safe=False)
@stringfilter
def custom_markdown(value):
extensions = ["nl2br", ]
return mark_safe(markdown.markdown(force_unicode(value),
extensions,
safe_mode=False,
enable_attributes=False))
Upvotes: 0
Reputation: 36171
You are passing an extra argument to a filter which does not need any. The correct call should be:
{{ object.text|custom_markdown }}
The filter custom_markdown
already manages itself the process to render allows HTML in the content (with the @register.filter(is_safe=True)
decorator and mark_safe
function)
According to this answer, there is already a filter for allowing HTML, with django.contrib.markup.templatetags
, so you should not need to create your own filter (and I think you mixed the two ideas in your head):
{{ object.text|markdown:"safe" }}
Upvotes: 4