Skip to content

Fixed #36519 -- Made centre filter consistent for odd padding. #19669

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

Merged
merged 2 commits into from
Jul 28, 2025

Conversation

mriduldhall
Copy link
Contributor

Refactored template centre filter to match f-string behaviour for filter.
Thanks Lily Acorn for the report.

Trac ticket number

ticket-36519

Branch description

Provide a concise overview of the issue or rationale behind the proposed changes.

Checklist

  • This PR targets the main branch.
  • The commit message is written in past tense, mentions the ticket number, and ends with a period.
  • I have checked the "Has patch" ticket flag in the Trac system.
  • I have added or updated relevant tests.
  • I have added or updated relevant docs, including release notes if applicable.
  • I have attached screenshots in both light and dark modes for any UI changes.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello! Thank you for your contribution 💪

As it's your first contribution be sure to check out the patch review checklist.

If you're fixing a ticket from Trac make sure to set the "Has patch" flag and include a link to this PR in the ticket!

If you have any design or process questions then you can ask in the Django forum.

Welcome aboard ⛵️!

Copy link
Contributor

@nessita nessita left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @mriduldhall for this branch! 🌟

The core of the solution is correct. I was thinking that we could use some extra testing for the center template filter as an initial commit, to ensure that behavior is not changed (other than for the fix itself).

For example, I think that the current center in main accepts negative values while this version does not. How about adding this new test which passes in main?

    def test_widths(self):
        value = "something"
        for i in (-1, *range(len(value) + 1)):
            with self.subTest(i=i):
                self.assertEqual(center(value, i), value)

In summary, ideally this PR would include two commits, the first one with the test above and the second one with the actual ticket fix (and something will have to be done for negative widths).

@mriduldhall
Copy link
Contributor Author

mriduldhall commented Jul 23, 2025

Hi @nessita,
Thanks a lot for the review and the feedback. I'll work on fixing the patch.

As per your suggested changes what would the ideal next steps be for me? Should I add commits to the current branch and PR or would it be a better idea to close this PR and create a new PR?

@LilyAcorn
Copy link
Contributor

You can make the changes on this branch.

@LilyAcorn
Copy link
Contributor

@mriduldhall For nessita's suggestion to have the test she suggested in its own commit, you might find it useful to look into how git rebase --interactive works.

@mriduldhall
Copy link
Contributor Author

Thank you for the guidance! Would the commits now be in a suitable form?

Copy link
Contributor

@nessita nessita left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @mriduldhall for addressing my review comments.

I think there’s still a small misunderstanding. The first commit, taken on its own, doesn't have a passing test suite (test_even_input fails). We need to make sure that the tests pass both after the first and second commits.

My original suggestion was to add only the test_widths test in the first commit (which should pass with the current logic), and then include the full set of changes that fix ticket-36519 in the second commit (this would be the logic change and test_odd_input and test_even_input).

Lastly, the first commit could be something like this:

Added test for various widths in tests/template_tests/filter_tests/test_center.py.

I'm happy to help rearrange the commits if needed, but feel free to give it a shot, just let me know!

@mriduldhall
Copy link
Contributor Author

I believe the commits should now be in the required form.
Thanks a lot for guiding me through it!

@nessita
Copy link
Contributor

nessita commented Jul 25, 2025

Amazing, thank you @mriduldhall and @LilyAcorn, this looks good, I will do the final review and merge next week. Well done! 🌟

mriduldhall and others added 2 commits July 28, 2025 09:26
…adding.

Refactored `center` template filter to match f-string behaviour,
producing consistent padding for both odd and even fillings.

Thanks Lily Acorn for the report and Natalia Bidart for the review.

Co-authored-by: Lily Acorn <[email protected]>
Copy link
Contributor

@nessita nessita left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thank you! Will merge when CI is green 🌟

(I pushed a small fix of the commit messages since the filter is called center and not centre.)

@nessita nessita merged commit d4dd3e5 into django:main Jul 28, 2025
29 of 32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants