简体   繁体   English

为什么views.py中的function在Django的这种情况下需要请求参数?

[英]Why does a function in views.py require a request parameter in this case in Django?

In url.py I have set up a new path within the main urlpatterns list:在 url.py 中,我在主 urlpatterns 列表中设置了一个新路径:

path('ko/', views.ko),

I learned that I need to write this function in views.py to get the webpage going:我了解到我需要在 views.py 中编写此 function 以使网页正常运行:

 def ko(request):
    return HttpResponse("It's a page")

My question is why doesn't the function work when I leave the parameter blank instead of request?:我的问题是,当我将参数留空而不是请求时,为什么 function 不工作?:

def ko():
     return HttpResponse("It's a page")

Running the page when I delete the request parameter outputs a TypeError:ko() takes 0 positional arguments but 1 was given.当我删除请求参数时运行页面输出 TypeError:ko() 需要 0 位置 arguments 但给出了 1。

If I don't have a request input on the function call of views.ko then why is the request parameter necessary when writing the initial function, what is the request parameter doing, and where is this request parameter going into?如果我在views.ko的function调用中没有请求输入,那么为什么在编写初始function时需要请求参数,请求参数在做什么,这个请求参数在哪里? What are its attributes?它的属性是什么? I would really appreciate a thorough response on its qualities.我真的很感激对其品质的彻底回应。

A view function, or view for short, is a Python function that takes a Web request and returns a Web response. A view function, or view for short, is a Python function that takes a Web request and returns a Web response. So every view must accept an request parameter.所以每个视图都必须接受一个request参数。

The request object contains metadata about the request, for example what HTTP request method used, The IP address of the client etc. You find the list of HttpRequest here request object 包含有关请求的元数据,例如使用的 HTTP 请求方法,客户端的 IP 地址等。您可以在此处找到HttpRequest的列表

Also from the documentation .也来自文档

Once one of the URL patterns matches, Django imports and calls the given view, which is a Python function (or a class-based view). Once one of the URL patterns matches, Django imports and calls the given view, which is a Python function (or a class-based view). The view gets passed the following arguments :该视图通过以下 arguments

An instance of HttpRequest. HttpRequest 的一个实例。

If the matched URL pattern contained no named groups, then the matches from the regular expression are provided as positional arguments.如果匹配的 URL 模式不包含命名组,则正则表达式中的匹配项将作为位置 arguments 提供。

The keyword arguments are made up of any named parts matched by the path expression that are provided, overridden by any arguments specified in the optional kwargs argument to django.urls.path() or django.urls.re_path(). The keyword arguments are made up of any named parts matched by the path expression that are provided, overridden by any arguments specified in the optional kwargs argument to django.urls.path() or django.urls.re_path().

Each view function takes an HttpRequest object as its first parameter, which is typically named request每个视图 function 都将 HttpRequest object 作为其第一个参数,通常命名为 request

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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