Skip to content

Fix public profile 404 when username differs from slug - #2768

Merged
Didayolo merged 3 commits into
developfrom
fix/public-profile-username-lookup
Nov 23, 2026
Merged

Didayolo merged 3 commits into
developfrom
fix/public-profile-username-lookup

Conversation

@Didayolo

@Didayolo Didayolo commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Original PR: #2762

This PR fixes the link to profile when username and slug differ. In a second step, let's improve the signup to avoid these cases in the future.

Description

The public profile page at /user/<username>/ returns 404 for some users, such as siddhant_chaudhary__ in #2761.

Root cause: PublicProfileView sets slug_url_kwarg = 'username' but keeps the default slug_field = 'slug', so it looks up User.slug == <url value>. User.save() sets slug = slugify(username), and slugify lowercases the value and strips leading and trailing _ and -. So siddhant_chaudhary__ gets the slug siddhant_chaudhary. Most links (competition header, participant manager, task management, forums, server status, and the /profiles/user/<username>/ redirect) point to /user/<username>/, so they 404 for any user whose username starts or ends with _/-, or has uppercase letters.

Fix: PublicProfileView.get_object() now looks the user up by username first and falls back to the slug. Links built from either value keep working. The deleted, inactive and banned filters still apply, because the lookup uses the view's queryset.

Test: added test_username_that_differs_from_slug_opens_the_profile. It creates a user trailing_user__, checks that the slug differs from the username, and checks that both /user/<username>/ and /user/<slug>/ return 200. Without the fix it fails with 404 == 200.

Issues this PR resolves

A checklist for hand testing

  • Sign up (or edit a user in admin) with a username ending in __, e.g. test_user__
  • Open /user/test_user__/ while logged in: the profile loads
  • Open /profiles/user/test_user__/: redirects to the profile and it loads
  • Open /user/test_user/ (the slug): the same profile loads
  • Deleted / inactive / banned users still return 404

Any relevant files for testing

src/apps/profiles/tests/test_public_profile_page.py

Checklist

  • Code review by me
  • Hand tested by me
  • I'm proud of my work
  • Code review by reviewer
  • Hand tested by reviewer
  • CircleCi tests are passing
  • Ready to merge

@Didayolo
Didayolo merged commit 4f790e3 into develop Nov 23, 2026
0 of 3 checks passed
@Didayolo
Didayolo deleted the fix/public-profile-username-lookup branch November 23, 2026 12:10
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.

Profile Page 404

2 participants