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

Django项目集成django-smart-selects的URL配置问题

解决django-smart-selects的URL配置问题

你遇到的NameError: name 'url' is not defined是因为Django 2.0及以上版本已经移除了url()函数,现在统一用path()或re_path()(后者对应旧版url(),支持正则表达式)来配置路由。

针对你的情况,有两种可行的修改方式:

方法一:用path()直接添加(推荐)

因为chaining/是固定路径,不需要正则匹配,直接用path()即可,修改后的urlpatterns如下:

urlpatterns = [
    path('', include('TimeTracking.urls')),
    path('admin/', admin.site.urls),
    path('data-browser/', include('data_browser.urls')),
    # 新增django-smart-selects的路由
    path('chaining/', include('smart_selects.urls')),
]

方法二:用re_path()兼容正则写法

如果你想严格对应文档里的正则配置,需要先导入re_path,再添加路由:

  1. 在urls.py顶部导入re_path:
from django.urls import path, include, re_path
  1. 然后在urlpatterns里添加:
re_path(r'^chaining/', include('smart_selects.urls')),

另外别忘了确认:

  • 已经通过pip install django-smart-selects安装了工具
  • 在项目的settings.py的INSTALLED_APPS里添加了'smart_selects'

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 11:04:58