问题遇到的现象和发生背景
我希望使用
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