简体   繁体   中英

Django "render" is not accessed Pylance

After creating an app, in the file "views.py" the import "from django.shortcuts import render" says django "render" is not accessed Pylance 带有消息的代码

hello>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',
]

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"))
]

网页消息错误

It simply says that you import something that is not used later in the program. You thus have imported render , but no view is making a call to render , or passing a reference to the render function for example.

The same with the request parameter, it is not used when you construct a HttpResponse('Hello') . Usually if you import something, or you define a parameter for something, you use that something in the rest of the code. The message thus warns you that you might have forgotten something. You can here thus remove the from django.shortcuts import render line.

If you have not figured it out, it is not loading because you did not change the URL to /hello/.

During that lecture, as he was saying to run the program, he tells you to change the URL to IP:PORT/hello/.

I had the same problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM