nietianyue 2021-12-12 19:18 采纳率: 100%
浏览 1417
已结题

cannot import name 'url' from 'django.urls'

问题遇到的现象和发生背景

我希望使用

 url(r'^logging/', TemplateView.as_view(template_name="logging"),name='logging'),

创建一个TemplateView

问题相关代码,请勿粘贴截图
import os

import django
import pymssql
from django.contrib import admin
from django.shortcuts import HttpResponse
from django.shortcuts import render
from django.urls import path, url
from django.views.generic import TemplateView

urlpatterns = [
    url(r'^logging/', TemplateView.as_view(template_name="logging"),name='logging'),
    path('logging/',logging),
]
运行结果及报错内容

ImportError: cannot import name 'url' from 'django.urls' (C:\Users\RyuZU\AppData\Local\Programs\Python\Python310\lib\site-packages\django\urls_init_.py)

python manage.py runserver 0.0.0.0:8000
Watching for file changes with StatReloader
Performing system checks...

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "C:\Users\RyuZU\AppData\Local\Programs\Python\Python310\lib\threading.py", line 1009, in _bootstrap_inner
    self.run()
  File "C:\Users\RyuZU\AppData\Local\Programs\Python\Python310\lib\threading.py", line 946, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\RyuZU\AppData\Local\Programs\Python\Python310\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\RyuZU\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\commands\runserver.py", line 124, in inner_run
    self.check(display_num_errors=True)
  File "C:\Users\RyuZU\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\base.py", line 438, in check
    all_issues = checks.run_checks(
  File "C:\Users\RyuZU\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\checks\registry.py", line 77, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
  File "C:\Users\RyuZU\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "C:\Users\RyuZU\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
    return check_method()
  File "C:\Users\RyuZU\AppData\Local\Programs\Python\Python310\lib\site-packages\django\urls\resolvers.py", line 446, in check
    for pattern in self.url_patterns:
  File "C:\Users\RyuZU\AppData\Local\Programs\Python\Python310\lib\site-packages\django\utils\functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\RyuZU\AppData\Local\Programs\Python\Python310\lib\site-packages\django\urls\resolvers.py", line 632, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "C:\Users\RyuZU\AppData\Local\Programs\Python\Python310\lib\site-packages\django\utils\functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\RyuZU\AppData\Local\Programs\Python\Python310\lib\site-packages\django\urls\resolvers.py", line 625, in urlconf_module
    return import_module(self.urlconf_name)
  File "C:\Users\RyuZU\AppData\Local\Programs\Python\Python310\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "D:\Code\sqlproject\SQLproject\urls.py", line 23, in <module>
    from django.urls import path, url
ImportError: cannot import name 'url' from 'django.urls' (C:\Users\RyuZU\AppData\Local\Programs\Python\Python310\lib\site-packages\django\urls\__init__.py)
我的解答思路和尝试过的方法

我试着把

from django.urls import path, url

改成

from django.conf.urls import url

无效
直接删除

from django.urls import  url

的话
会显示NameError: name 'url' is not defined

我想要达到的结果
  • 写回答

1条回答 默认 最新

  • CSDN专家-黄老师 2021-12-13 10:04
    关注

    直接改用path就行了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 12月25日
  • 已采纳回答 12月17日
  • 修改了问题 12月12日
  • 创建了问题 12月12日

悬赏问题

  • ¥15 应用简单的Python代码完成一个学生成绩管理系统
  • ¥15 用matlab求微分方程初值问题
  • ¥15 vscode下编写第三方库opencv与pcl代码时没有代码提示
  • ¥15 能够跑通不报错,如何解决?(标签-matlab)
  • ¥15 MOS在RDS较大,频率高时开关波形异常
  • ¥15 SCENIC分析报错求解答
  • ¥15 ceph初始化mon不成功 下图不报错 这个是什么元
  • ¥15 数学建模数学建模少ai
  • ¥35 这个等效电路图是什么意思?(答疑)(有没有不是chat的,我就是在chat搜不到才来问的,chat还开了VIP)(可以追加酬金)
  • ¥200 基于同花顺supermind的量化策略脚本编辑