Django登录时静态资源404且未跳转至个人页问题排查求助
Let's break down your problem and walk through targeted troubleshooting steps, plus clarify how Django's login redirect should work normally.
Troubleshooting Directions
- Check your login view's post-success logic
The log shows yourPOST /accounts/login/returns a 200 status code—this is unusual for a successful login, which should return a 302 redirect. This means your view is probably rendering a template directly instead of redirecting to the user's profile page. Double-check:- Did you accidentally replace a
return redirect(...)with areturn render(...)in the login view? - If using Django's built-in
LoginView, did you override theget_success_url()method incorrectly, or modify theLOGIN_REDIRECT_URLsetting to point to a broken page?
- Did you accidentally replace a
- Verify the broken page's static resource paths
The 404s formain.cssand images happen because the page being loaded is looking for/static/core/bundles/main.css, but your actual file is at/static/core/bundles/css/main.css. Since other pages load this CSS correctly, the issue is specific to the template being rendered after login:- Check the "load failed" template for incorrect
{% static %}tags. For example, it might have{% static 'core/bundles/main.css' %}instead of{% static 'core/bundles/css/main.css' %}. - Those unexpected images confirm the wrong template is being loaded entirely—this page shouldn't appear after login at all.
- Check the "load failed" template for incorrect
- Confirm user authentication status
Even though the POST returns 200, make sure the user is actually being authenticated. Add debug prints in your login view (likeprint(request.user.is_authenticated)after callingauthenticate()) or check the session in your browser dev tools. If authentication is failing silently, the view might be falling back to rendering the wrong error page instead of the login form with an error message. - Check for middleware interference
If you have custom middleware that runs after login, it might be intercepting the redirect and forcing a different page to load. Disable any recently added middleware temporarily to see if the redirect starts working again.
Normal Django Login Redirect Logic
Whether you're using Django's built-in auth views or a custom one, the successful login flow should follow this pattern:
- User submits login credentials via
POSTto/accounts/login/. - The view uses
authenticate()to verify the credentials. - If valid,
login(request, user)is called to set the user's session. - The view returns a 302 redirect (not a 200) to the configured success URL:
- For built-in
LoginView, this uses theLOGIN_REDIRECT_URLsetting (default is/accounts/profile/), or thenextparameter if provided in the login form. - For a custom view, you'd explicitly return
return redirect('profile-url-name')or the direct URL of the user's profile page.
- For built-in
- If authentication fails, the view renders the login template again (200 status) with an error message, not an unrelated "load failed" page.
The core issue here is that your login flow isn't hitting that redirect step—instead, it's rendering an incorrect template with broken static paths and unwanted images. Start by fixing the view's post-authentication return value, then clean up any template path issues if needed.
内容的提问来源于stack exchange,提问作者emma_lynn_rose
相关产品推荐
相关产品推荐

