You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django访问根路径自动跳转到/sales问题求助

问题描述

当前Django项目的关键配置及现象如下:

urls.py 代码

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include("dashboardapp.urls")),
]

模板代码

<!DOCTYPE html>
<html>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<body>
<style>
h2 {text-align: center;}

.center {
  margin-left: auto;
  margin-right: auto;
}

table, th, td {
  border: 1px solid black;
  border-collapse: collapse;
}
th, td {
  padding-top: 10px;
  padding-bottom: 20px;
  padding-left: 30px;
  padding-right: 40px;
}
tr:nth-child(even) {background-color: #f2f2f2;}
</style>

  <h2>Summary Details</h2>
  <table class="center">
    <thead>
    <tr>
      <th>scrapper_id</th>
      <th>scrapper_jobs_log_id</th>
      <th>external_job_source_id</th>
    </tr>
    </thead>
    <tbody>
    {% for stud in scrappers %}
    {% csrf_token %}
      <tr>
        <td>{{stud.scrapper_id}}</td>
        <td>{{stud.scrapper_jobs_log_id}}</td>
        <td>{{stud.external_job_source_id}}</td>
      </tr>
    {% endfor %}
    </tbody>
  </table>
</body>
</html>

启动日志与异常现象

Starting development server at http://127.0.0.1:8000/.

访问上述地址时,页面自动跳转到http://127.0.0.1:8000/sales,但当前项目未配置/sales路径,仅本地另一项目存在该路径,需修复此跳转异常。

解决步骤
  1. 清除浏览器缓存与Cookie
    直接清空当前域名下的缓存、Cookie,或使用无痕模式访问测试,排除浏览器缓存旧跳转记录的可能。

  2. 排查Django内部重定向逻辑

  • 检查dashboardapp/urls.py中的根路径配置,确认视图函数是否返回了指向/sales的重定向响应(如redirect('sales')或HttpResponseRedirect)。
  • 查看项目的中间件配置,确认是否有全局中间件在处理请求时触发了跳转(比如认证中间件强制跳转至特定路径)。
  1. 确认端口与进程唯一性
  • 用netstat -ano | findstr :8000(Windows)或lsof -i :8000(Linux/Mac)检查8000端口的占用情况,杀死其他占用该端口的进程后,重启当前Django项目,避免端口冲突导致的请求混淆。
  1. 验证前端跳转逻辑
    检查模板及引入的外部脚本,确认是否存在页面加载时触发的JavaScript跳转(如window.location.href = '/sales')。

  2. 核对URL路由配置
    安装django-extensions后,运行python manage.py show_urls,查看项目所有已注册的URL,确认是否有意外引入的/sales路径。

内容的提问来源于stack exchange,提问作者Ajay

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 00:40:22