erwinmatijsen / django-markdownify

Markdown template filter for Django
MIT License
120 stars 24 forks source link

fixes AttributeError when trying to render None #21

Closed DataGreed closed 3 years ago

DataGreed commented 4 years ago

Fixes an issue when the variable you are trying to markdownify in the template is set to None.

Steps to reproduce original problem fixed by this pull request:

  1. Create a view
  2. Pass variable foo=None to the template context
  3. Render this variable in the template with markdownify filter: {{ foo|markdownify}}

Expected result: foo is rendered as an empty string in the template.

Actual result: app crashes with an exception:

AttributeError: 'NoneType' object has no attribute 'strip'
  File "django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "django/core/handlers/base.py", line 145, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "django/core/handlers/base.py", line 143, in _get_response
    response = response.render()
  File "django/template/response.py", line 106, in render
    self.content = self.rendered_content
  File "django/template/response.py", line 83, in rendered_content
    content = template.render(context, self._request)
  File "django/template/backends/django.py", line 61, in render
    return self.template.render(context)
  File "django/template/base.py", line 171, in render
    return self._render(context)
  File "newrelic/api/function_trace.py", line 131, in dynamic_wrapper
    return wrapped(*args, **kwargs)
  File "django/template/base.py", line 163, in _render
    return self.nodelist.render(context)
  File "django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "django/template/loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "newrelic/api/function_trace.py", line 131, in dynamic_wrapper
    return wrapped(*args, **kwargs)
  File "django/template/base.py", line 163, in _render
    return self.nodelist.render(context)
  File "django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "newrelic/hooks/framework_django.py", line 760, in wrapper
    return wrapped(*args, **kwargs)
  File "django/template/loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "django/template/base.py", line 987, in render
    output = self.filter_expression.resolve(context)
  File "django/template/base.py", line 698, in resolve
    new_obj = func(obj, *arg_vals)
  File "markdownify/templatetags/markdownify.py", line 46, in markdownify
    html = markdown.markdown(text, extensions=extensions)
  File "markdown/core.py", line 387, in markdown
    return md.convert(text)
  File "markdown/core.py", line 248, in convert
    if not source.strip():