ImportError: 无法从 ‘django’ 导入 ‘urlpatterns’。

huangapple go评论54阅读模式
英文:

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 (]).

huangapple
  • 本文由 发表于 2023年5月14日 23:59:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76248417.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定