Skip to content

Ignore lines in templates #70

Closed

Description

@mschoettle

Is it possible to exclude certain lines in templates?

For example:

{% comment "foo" %}
some commented out stuff
{% endcomment %}

This will show up in the report as missing lines.

I tried adding {# pragma: no cover #} but it did not work.

Activity

  1. nedbat commented on May 6, 2020

    @nedbat
    Member

    The plugin is written to understand comment blocks and ignore them. Can you provide us with a reproducible test case?

  2. mschoettle commented on May 7, 2020

    @mschoettle
    Author

    I have a template file containing the above block:

    {% comment "foo" %}
    some commented out stuff
    {% endcomment %}
    

    And a test function like this:

    def test_foo(client):
        response = client.get(reverse('app:foo'))
        ...
    

    The relevant part of setup.cfg is as follows:

    [coverage:run]
    source = projectname
    omit =
        .*,
        *tests*,
        *migrations*,
    branch = True
    plugins =
        django_coverage_plugin
    

    Another reason why I asked is a {% block content %} definition in a base template which spans more than one line (i.e., the endblock is in a subsequent line. It's not going to be executed so I was wondering if there is a way to ignore them.

  3. marcgibbons commented on Apr 4, 2026

    @marcgibbons
    Contributor

    @nedbat I'm taking a stab at this one: getting a no pragma / exclude_lines implementation that mirrors the one for Python modules

  4. nedbat commented on Apr 7, 2026

    @nedbat
    Member

    Thanks, looking forward to it.

  5. PetrDlouhy commented on Jul 22, 2026

    @PetrDlouhy

    I hit this too and found the actual cause — at least for {% comment %} blocks it is not a missing "ignore" feature, it is a bug in the line mapping.

    In plugin.py, the token loop handles endcomment before the extends-skip, but the comment opener after it:

    if extends and not inblock:
        # In an inheriting template, ignore all tags outside of
        # blocks.
        continue
    
    if token.contents == "comment":
        comment = True

    So in a template that {% extends %} something, a {% comment %} opener placed outside a {% block %} is skipped by the extends and not inblock continue and comment = True is never set. The interior lines of the comment are then processed as regular content and reported as missing — they can never execute.

    Minimal repro:

    {% extends "base.html" %}
    {% comment %}
    this line is reported as a missing line
    {% endcomment %}
    {% block content %}covered{% endblock %}

    The fix is to check for the comment opener before the extends-skip, the same way endcomment already is. I can send a PR if you want.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions