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

Django项目访问accounts/register页面提示404路径不匹配如何解决

问题排查及修复方案

你出现404的核心原因是访问的URL地址和Django配置的路由规则不匹配,除此之外还有几个代码细节错误,按以下步骤修复即可:

1. 修正访问地址

你当前访问的是 http://127.0.0.1:8000/accounts/register.html,但你在accounts应用的urls.py里配置的路由路径是register,拼接前缀后正确的访问地址应该是:
http://127.0.0.1:8000/accounts/register
直接访问上述地址即可先解决404问题。

2. 优化路由配置(可选但推荐)

Django默认推荐路由末尾加斜杠,修改accounts应用的urls.py代码:

from django.urls import path
from . import views
urlpatterns = [
    # 末尾加斜杠后,后续访问时带不带斜杠Django都会自动重定向匹配
    path('register/', views.register, name="register")
 ]

3. 修复register.html的多处错误

  • 原代码重复了<head>和<body>标签,需要删掉冗余标签修正HTML结构
  • form的action属性硬写相对路径容易出现匹配错误,推荐用Django模板的url标签反向解析路由
  • 密码输入框应该用type="password"避免输入内容明文显示
    修正后的完整代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Registeration</title>
</head>
<body>
    <form action="{% url 'register' %}" method = "post">
        {% csrf_token %}
        <input type= "text" name="first_name" placeholder="First Name"><br>
        <input type= "text" name="last_name" placeholder="Last Name"><br>
        <input type= "text" name="username" placeholder="UserName"><br>
        <input type= "password" name="password1" placeholder="Password"><br>
        <input type= "password" name="password2" placeholder="Verify Password"><br>
        <input type= "submit">
    </form>
</body>
</html>

4. 确认模板路径配置正确

确保你的register.html文件放在Django能识别的模板目录下:如果是放在accounts应用内,需要先在项目settings.py的INSTALLED_APPS列表中添加accounts应用,再在accounts应用根目录下新建templates文件夹,把register.html放入该文件夹即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.07 04:54:02