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

Django新手访问根路径http://127.0.0.1:8000/报404错误如何解决?

报错原因

你访问的根路径http://127.0.0.1:8000/没有在URL配置中定义匹配规则,当前仅配置了form.html和hl7两个子路径的路由,因此触发404错误。

解决方案

可根据需求选择任意一种方案:

方案1:新增根路径路由

如果需要访问根路径时直接加载内容,在hl7rest应用的urls.py中新增根路径匹配规则即可:

from . import views
from django.urls import path
urlpatterns = [
    # 根路径绑定对应视图,示例为绑定表单页视图
    path('', views.render_form_View, name='index'),
    path('form.html', views.render_form_View, name='form1'),
    path('hl7', views.hl7_web_view ,name='hl7'),
]

如果需要根路径自动跳转到已有的页面,可使用RedirectView实现:

from . import views
from django.urls import path
from django.views.generic import RedirectView

urlpatterns = [
    # 根路径自动重定向到form.html页面
    path('', RedirectView.as_view(pattern_name='form1')),
    path('form.html', views.render_form_View, name='form1'),
    path('hl7', views.hl7_web_view ,name='hl7'),
]

方案2:直接访问已配置路径

无需修改配置,直接访问已经配置好的两个路径即可:

  • 表单页面:http://127.0.0.1:8000/form.html
  • hl7服务页面:http://127.0.0.1:8000/hl7

内容的提问来源于stack exchange,提问作者Manuel Sebastian Blanco

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 13:42:02