Django URLconf中的回调函数对象未调用 [英] The callback function object in Django URLconf is not called
问题描述
我正在 Django乍看之下租用Django | Django文档| Django
在引入URLconf时显示为:
When introducing URLconf It reads:
要设计对于应用程序的URL,您将创建一个称为URLconf的Python模块。应用程序的目录,其中包含URL模式和Python回调函数之间的简单映射。
To design URLs for an app, you create a Python module called a URLconf. A table of contents for your app, it contains a simple mapping between URL patterns and Python callback functions.
不过,回调函数是
mysite/news/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^articles/([0-9]{4})/$', views.year_archive),
]
print('callback funtion:',views.year_archive)
输出:
callback function: <function views.year_archive at 0x1039611e0>
因此它是一个未调用的函数对象。
So it a funtion object which is not called.
我想应该是 views.year_archive()
才能使其调用。
I suppose it should be views.year_archive()
to enable it to call.
如果没有叫,它如何工作?我假设在父类中有一个装饰器来处理它。
If not called,how does it work? I assume there a decorator to process it in parent class.
我检查 url
django.conf.urls | Django文档| Django
关键声明是:
elif callable(view):
return RegexURLPattern(regex, view, kwargs, name)
I继续探索 LocaleRegexProvider
, RegexURLPattern
django.urls.resolvers | Django文档| Django 。
没有找到合适的代码来调用回调函数 views.year_archive
I dig on to explore LocaleRegexProvider
, RegexURLPattern
django.urls.resolvers | Django documentation | Django.
No appopriate codes were found to call the callback function views.year_archive
其回调机制是什么
推荐答案
只要对给定的url模式发出请求,Django就会自动调用回调视图函数。
Django automatically calls the callback view function whenever a request is made to the given url pattern.
要了解其工作原理,请看以下示例:
To understand how it works, look at this example:
>>> def a():
print("Called function a")
>>> def b():
print("Called function b")
>>> def c(callback):
# call the callback function like this
callback()
>>> c(a)
Called function a
>>> c(b)
Called function b
基本上,这就是Django的 url
函数起作用。
Basically, this is how Django's url
function works.
这篇关于Django URLconf中的回调函数对象未调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!