【问题标题】:Django "render" is not accessed Pylance未访问 Django“渲染” Pylance
【发布时间】:2023-04-04 19:35:01
【问题描述】:

创建应用程序后,在文件“views.py”中,导入“from django.shortcuts import render”表示未访问 django“render” Pylance

你好>views.py

from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.
def index(request):
    return HttpResponse("hello")

lecture3>settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'hello',
]

你好>urls.py

from django.urls import path
from . import views
urlpatterns = [
    path("",views.index, name="index")
]

lecture3>urls.py

from django.contrib import admin
from django.urls import path
from django.urls.conf import include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('helllo/', include("hello.urls"))
]

【问题讨论】:

  • 它只是说你导入了一些稍后在程序中没有使用的东西。

标签: python django visual-studio


【解决方案1】:

它只是说您导入了稍后在程序中不使用的东西。因此,您已导入 render,但没有视图调用 render,或传递对 render 函数的引用。

与请求参数相同,构造HttpResponse('Hello')时不使用。通常,如果您导入某些内容,或者为某些内容定义参数,您会在其余代码中使用该内容。因此,该消息警告您您可能忘记了某些内容。您可以在此处删除from django.shortcuts import render 行。

【讨论】:

  • 我已经编辑了我的帖子,显示了我所做的一切,基于 CS50W 课程逐行进行,它应该可以工作,谢谢
  • @JulianGomez:通常它起作用,这些只是警告,而不是错误
【解决方案2】:

如果您还没有弄清楚,它没有加载,因为您没有将 URL 更改为 /hello/。

在那次演讲中,正如他所说的运行程序,他告诉您将 URL 更改为 IP:PORT/hello/。

我遇到了同样的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-11
    • 2021-11-23
    • 2017-11-04
    • 2014-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多