fix(login): render login page when the Site row for SITE_ID is missing - #15843
Open
Maffooch wants to merge 1 commit into
Open
fix(login): render login page when the Site row for SITE_ID is missing#15843Maffooch wants to merge 1 commit into
Maffooch wants to merge 1 commit into
Conversation
DojoLoginView inherited Django's LoginView.get_context_data(), which resolves the current Site via get_current_site() and raises Site.DoesNotExist -- an HTTP 500 -- when the django_site row referenced by SITE_ID is absent on an instance. Because this happens on the anonymous sign-in page itself, it locks users out entirely. Override get_context_data() to resolve the current site defensively, falling back to RequestSite(request) when the row is missing, mirroring Django's behaviour when the sites framework is not installed. The Site is only used for the site/site_name template context, so a missing row must never take the page down. No schema change. Add unittests/test_login_view_missing_site.py covering the site-present (control) and site-missing (regression) cases. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014MhRZa88XXg3GHhpvz3pEk
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
GET /loginreturns an HTTP 500 on instances where thedjango_siterow referenced bySITE_IDis absent from the database:Django's
LoginView.get_context_data()unconditionally resolves the currentSitewhile building the login page context.Site.objects.get_current()looks the row up bySITE_IDand raisesSite.DoesNotExistwhen that row is missing. Because this happens on the anonymous login page itself, the failure locks users out entirely — they cannot even reach the sign-in form.Root cause
DojoLoginView(dojo/user/ui/views.py) inherits Django'sLoginView.get_context_data, which hard-depends on aSiterow existing forSITE_ID. TheSiteobject is only used to populatesite/site_namein the template context, so a missing row should never take the whole page down.Fix
Override
get_context_dataonDojoLoginViewto resolve the current site defensively, falling back toRequestSite(request)whenSite.DoesNotExistis raised. This mirrors what Django itself does inget_current_site()when the sites framework is not installed, so the login page always renders. No schema change.Why no migration
Seeding the
Siterow via a data migration was considered and rejected:SITE_IDpoints at a different (missing) row — the page would 500 again.The defensive fallback resolves all cases with zero migrations.
Tests
New
unittests/test_login_view_missing_site.py:Siterow present →GET /loginreturns 200.Siterows deleted →GET /loginreturns 200 (previously 500 withSite.DoesNotExist).Verified locally: the regression test reproduces the exact
Site.DoesNotExisttraceback against the current code and passes with the fix; the control case and the existing login-touching test (test_permission_policy_headers) stay green.Downstream (commercial plugin) impact
The commercial plugin overrides the
/loginroute with aProLoginViewthat subclassesDojoLoginViewand does not overrideget_context_data, so it inherits this fix directly. Its login template does not referencesite/site_name, so no template change is required there. This change therefore resolves the reported errors on those instances without any additional downstream code.Generated by Claude Code