【问题标题】:Django request not recognizedDjango 请求无法识别
【发布时间】:2021-11-16 18:06:18
【问题描述】:

我目前正在通过视频教程学习 Django,但遇到了一个无法摆脱的问题。 我在我的一个应用程序的视图文件中,当我尝试使用“请求”时,它告诉我“未定义“请求”PylancereportUndefinedVariable'

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


# Create your views here.
def home_view(*args, **kwargs):

    return render(request, "home.html", {})

def contact_view(*args, **kwargs):
    return HttpResponse("<h1>contact page</h1>")

view code

我尝试导入 HttpRequests 并修改代码以适应,但它告诉我在运行服务器时它没有 META 属性 runserver error

我想使用“请求”而不是为它导入新库,因为它应该随 django 一起提供,我观看的教程中的那个人只是输入请求并且它可以工作。我在Django 文档,使用 request 似乎不应该成为问题。

【问题讨论】:

    标签: python django django-templates pylance


    【解决方案1】:

    request 是视图函数的第一个参数,因此需要明确指定:

    #                &downarrow; request parameter
    def home_view(request, *args, **kwargs):
        return render(request, 'home.html', {})

    【讨论】:

    • 谢谢,我会给自己打印一个 python/django 备忘单,不明白我怎么没看到现在看起来很明显
    猜你喜欢
    • 2013-02-19
    • 1970-01-01
    • 2018-12-10
    • 1970-01-01
    • 2014-06-08
    • 2019-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多