英文:
ImportError: cannot import name 'urlpatterns' from 'django'
问题
以下是您要翻译的内容:
我试图运行我的第一个Django应用程序,当我运行服务器时,我收到以下错误:
ImportError: 无法从'django'导入名称'urlpatterns'(C:\Users\Chris\AppData\Local\Programs\Python\Python311\Lib\site-packages\django_init_.py)
为什么我在views.py
中使用的以下命令会导致错误:
from django import urlpatterns
据我所知,它应该可以工作?我尝试在网上搜索答案,但找不到任何信息。
英文:
I am trying to run my first Django application and when I run server I get the following error:
ImportError: cannot import name 'urlpatterns' from 'django' (C:\Users\Chris\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\__init__.py)
Why is the following command that I use in views.py
giving me errors:
from django import urlpatterns
As far as I know it should work? I have tried searching the web for an answer but I couldn't find anything.
答案1
得分: 1
urlpatterns
不需要从任何地方导入。urlpatterns
是一个变量,应该在urls.py
文件中定义,所以:
from django.contrib import admin
from django.urls import include, path
# 不需要导入from django import urlpatterns
urlpatterns = [
path('tasks/', include('tasks.urls')),
]
在闭合的方括号(]
)之后也不应该有尾随逗号。
英文:
There is no need to import urlpatterns
from anywhere. urlpatterns
is a variable that should be defined in the urls.py
file, so:
<pre><code>from django.contrib import admin
from django.urls import include, path
<s>from django import urlpatterns</s>
urlpatterns = [
path('tasks/', include('tasks.urls')),
]</code></pre>
There also should not be a trailing comma after the closing square bracket (]
).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论