views.py
views.py
from django.http import Http404, HttpResponse import datetime
def hours_ahead(request, offset):
try:
offset = int(offset)
except ValueError:
raise Http404()
dt = datetime.datetime.now() + datetime.timedelta(hours=offset)
html = "<html><body>In %s hour(s), it will be %s.</body></html>" % (offset, dt)
return HttpResponse(html)
urls.py
urls . py
from django.conf.urls.defaults import *
from mysite.views import hello, current_datetime, hours_ahead
urlpatterns = patterns('',
(r'^hello/$', hello),
(r'^time/$', current_datetime),
(r'^time/plus/(\d{1,2})/$', hours_ahead),
)
it says the parameter offset which value extract from the matching url care nothing about its name,but position matters. It's its second parameter standing after request that decide what it can do. But why ? hours_ahead , is just a user-defined method, not a class or something. Who give its power that make the parameter's position can work that way. So be clear, my question is , "offset = int (offset)" does it make any sense, why can offset receive the value from url when user set the hours's.
它说从匹配url中提取的参数偏移量与它的名称无关,而与位置有关。它的第二个参数是一个接着一个的请求,决定它能做什么。但是为什么呢?hours_ahead,只是一个用户定义的方法,而不是类或其他东西。谁赋予它的力量使参数的位置可以这样工作。显然,我的问题是,"offset = int (offset)"是否有意义,为什么当用户设置小时数时,offset可以从url接收值。
2 个解决方案
#1
0
it says the parameter offset which value extract from the matching url care nothing about its name,but position matters. It's its second parameter standing after request that decide what it can do. But why ?
它说从匹配url中提取的参数偏移量与它的名称无关,而与位置有关。它的第二个参数是一个接着一个的请求,决定它能做什么。但是为什么呢?
In Python, there are two kinds of arguments you can pass to a method. Positional arguments and keyword arguments. Positional arguments are based on the position in the method's signature, and their order is important (that's why they are called positional, because their position in the method's signature is important).
在Python中,有两种类型的参数可以传递给方法。位置参数和关键字参数。位置参数基于方法签名中的位置,它们的顺序很重要(这就是为什么它们被称为位置参数,因为它们在方法签名中的位置很重要)。
Positional arguments must always have a value. They are not optional.
位置参数必须始终具有一个值。他们不是可选的。
Keyword arguments are those that can be passed in any order - as long as they are passed in after positional arguments. Keywords arguments may be optional.
关键字参数是那些可以以任何顺序传递的参数——只要它们在位置参数之后被传递。关键字参数可以是可选的。
Here is an example:
这是一个例子:
def foo(a, b, c='Hello', d='World'):
print(a,b,c,d)
a
and b
are positional arguments. They are required. You must pass in a value. c
and d
are optional keyword arguments, with their default values.
a和b是位置参数。他们是必需的。你必须传递一个值。c和d是可选的关键字参数,带有它们的默认值。
The first argument passed will be referred to by a
, the second b
. After that, you can pass either d, or c or none:
通过的第一个参数将被a、第二个b引用。在此之后,您可以通过d、c或none:
>>> foo(1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: foo() takes at least 2 arguments (1 given)
>>> foo(1,2)
(1, 2, 'Hello', 'World')
>>> foo(1,2,d='Yes')
(1, 2, 'Hello', 'Yes')
>>> foo(1,2,d='Yes',c='No')
(1, 2, 'No', 'Yes')
Now, in django's urls.py - there are two ways to capture elements of the URL:
现在,在django url。py -有两种方法可以捕获URL的元素:
^time/plus/(\d+{1,2})/$
- this is capturing the argument and passing it as a positional argument. The result of the regular expression captured will be passed to the second argument to the mapped request function, whatever that argument is called (the first argument is typically called request).
^时间/ + /(\ d + { 1,2 })/美元——这是获取参数,通过它作为位置参数。捕获的正则表达式的结果将被传递给映射请求函数的第二个参数,无论这个参数被调用什么(第一个参数通常被称为request)。
To map the above url, you must have a view method that takes exactly two positional arguments, but they can be called whatever you like. Remember, request is the first positional argument. Now with that in mind, consider this:
要映射上面的url,必须有一个视图方法,该方法只接受两个位置参数,但是可以任意调用它们。记住,请求是第一个位置参数。记住这一点,考虑一下
def foo(request, a) # You must have two positional arguments
def foo(request, b) # It does not matter what the second argument is called
def foo(request, a, b='world') # You can have any number of
# additional keyword arguments
# but they must be optional (have defaults)
def foo(request, a, b, c='world') # This will fail because you have two
# positional arguments, but only one pattern is
# captured in the URL.
^time/plus/(?P<offset>\d+{1,2})/$
- This syntax (called a named group) passes the result of the regular expression as a keyword argument to the mapped URL function. This means, that the value of the 2 digits will passed as the keyword argument offset to the view function. You can read more about named groups in URLs at the django documentation.
^时间/ + /(? P <抵消> \ d + { 1,2 })/美元——这语法(称为一个命名组)通过正则表达式的结果作为一个关键字参数的URL映射函数。这意味着,两个数字的值将作为关键字参数偏移到视图函数。您可以在django文档中阅读更多关于命名组的url。
If you have the above named group URL pattern, then your request method should have the following signature:
如果您有上面命名的组URL模式,那么您的请求方法应该具有以下签名:
def hours_ahead(request, offset)
As an example, consider this URL pattern:
例如,考虑这个URL模式:
^time/(\d+{1,2})/(\d+{1,2})/(?P<hello>\w+)/$
To match this pattern, your view function must have the following signature:
要匹配此模式,您的视图函数必须具有以下签名:
def foo(request, a, b, hello)
When you receive the following URL time/32/42/world/
, a
will be 32, b
will be 42, and hello
will have the value world.
当您收到以下URL时/32/42/world/, a将是32,b将是42,而hello将拥有价值世界。
If you change hello
in your view method to something else, like def foo(request, a, b, blah)
your URL will not get mapped because the pattern is specifically looking for the keyword hello in the function signature.
如果您将视图方法中的hello更改为其他内容,比如def foo(请求,a, b,等等),那么您的URL将不会被映射,因为该模式专门在函数签名中查找关键字hello。
#2
1
The way it works is that Django reads this url pattern r'^time/plus/(\d{1,2})/$'
, extracts the parameters that are enclosed in parenthesis (which is (\d{1,2})
in this case), then passes it as argument to the hours_ahead
function. If there are many parameters, the order at which they are written in the url pattern also dictates the order that they are passed into the corresponding view function. You can read further on the docs here: https://docs.djangoproject.com/en/dev/topics/http/urls/
它的工作方式是,Django读取这个url模式r ^时间/ + /(\ d { 1,2 })/美元”,提取参数括在括号((\ d { 1,2 })在这种情况下),然后将它作为参数传递给hours_ahead函数。如果有许多参数,那么在url模式中写入参数的顺序也会指定它们被传递到相应的视图函数的顺序。您可以在这里进一步阅读文档:https://docs.djangoproject.com/en/dev/topics/http/urls/
#1
0
it says the parameter offset which value extract from the matching url care nothing about its name,but position matters. It's its second parameter standing after request that decide what it can do. But why ?
它说从匹配url中提取的参数偏移量与它的名称无关,而与位置有关。它的第二个参数是一个接着一个的请求,决定它能做什么。但是为什么呢?
In Python, there are two kinds of arguments you can pass to a method. Positional arguments and keyword arguments. Positional arguments are based on the position in the method's signature, and their order is important (that's why they are called positional, because their position in the method's signature is important).
在Python中,有两种类型的参数可以传递给方法。位置参数和关键字参数。位置参数基于方法签名中的位置,它们的顺序很重要(这就是为什么它们被称为位置参数,因为它们在方法签名中的位置很重要)。
Positional arguments must always have a value. They are not optional.
位置参数必须始终具有一个值。他们不是可选的。
Keyword arguments are those that can be passed in any order - as long as they are passed in after positional arguments. Keywords arguments may be optional.
关键字参数是那些可以以任何顺序传递的参数——只要它们在位置参数之后被传递。关键字参数可以是可选的。
Here is an example:
这是一个例子:
def foo(a, b, c='Hello', d='World'):
print(a,b,c,d)
a
and b
are positional arguments. They are required. You must pass in a value. c
and d
are optional keyword arguments, with their default values.
a和b是位置参数。他们是必需的。你必须传递一个值。c和d是可选的关键字参数,带有它们的默认值。
The first argument passed will be referred to by a
, the second b
. After that, you can pass either d, or c or none:
通过的第一个参数将被a、第二个b引用。在此之后,您可以通过d、c或none:
>>> foo(1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: foo() takes at least 2 arguments (1 given)
>>> foo(1,2)
(1, 2, 'Hello', 'World')
>>> foo(1,2,d='Yes')
(1, 2, 'Hello', 'Yes')
>>> foo(1,2,d='Yes',c='No')
(1, 2, 'No', 'Yes')
Now, in django's urls.py - there are two ways to capture elements of the URL:
现在,在django url。py -有两种方法可以捕获URL的元素:
^time/plus/(\d+{1,2})/$
- this is capturing the argument and passing it as a positional argument. The result of the regular expression captured will be passed to the second argument to the mapped request function, whatever that argument is called (the first argument is typically called request).
^时间/ + /(\ d + { 1,2 })/美元——这是获取参数,通过它作为位置参数。捕获的正则表达式的结果将被传递给映射请求函数的第二个参数,无论这个参数被调用什么(第一个参数通常被称为request)。
To map the above url, you must have a view method that takes exactly two positional arguments, but they can be called whatever you like. Remember, request is the first positional argument. Now with that in mind, consider this:
要映射上面的url,必须有一个视图方法,该方法只接受两个位置参数,但是可以任意调用它们。记住,请求是第一个位置参数。记住这一点,考虑一下
def foo(request, a) # You must have two positional arguments
def foo(request, b) # It does not matter what the second argument is called
def foo(request, a, b='world') # You can have any number of
# additional keyword arguments
# but they must be optional (have defaults)
def foo(request, a, b, c='world') # This will fail because you have two
# positional arguments, but only one pattern is
# captured in the URL.
^time/plus/(?P<offset>\d+{1,2})/$
- This syntax (called a named group) passes the result of the regular expression as a keyword argument to the mapped URL function. This means, that the value of the 2 digits will passed as the keyword argument offset to the view function. You can read more about named groups in URLs at the django documentation.
^时间/ + /(? P <抵消> \ d + { 1,2 })/美元——这语法(称为一个命名组)通过正则表达式的结果作为一个关键字参数的URL映射函数。这意味着,两个数字的值将作为关键字参数偏移到视图函数。您可以在django文档中阅读更多关于命名组的url。
If you have the above named group URL pattern, then your request method should have the following signature:
如果您有上面命名的组URL模式,那么您的请求方法应该具有以下签名:
def hours_ahead(request, offset)
As an example, consider this URL pattern:
例如,考虑这个URL模式:
^time/(\d+{1,2})/(\d+{1,2})/(?P<hello>\w+)/$
To match this pattern, your view function must have the following signature:
要匹配此模式,您的视图函数必须具有以下签名:
def foo(request, a, b, hello)
When you receive the following URL time/32/42/world/
, a
will be 32, b
will be 42, and hello
will have the value world.
当您收到以下URL时/32/42/world/, a将是32,b将是42,而hello将拥有价值世界。
If you change hello
in your view method to something else, like def foo(request, a, b, blah)
your URL will not get mapped because the pattern is specifically looking for the keyword hello in the function signature.
如果您将视图方法中的hello更改为其他内容,比如def foo(请求,a, b,等等),那么您的URL将不会被映射,因为该模式专门在函数签名中查找关键字hello。
#2
1
The way it works is that Django reads this url pattern r'^time/plus/(\d{1,2})/$'
, extracts the parameters that are enclosed in parenthesis (which is (\d{1,2})
in this case), then passes it as argument to the hours_ahead
function. If there are many parameters, the order at which they are written in the url pattern also dictates the order that they are passed into the corresponding view function. You can read further on the docs here: https://docs.djangoproject.com/en/dev/topics/http/urls/
它的工作方式是,Django读取这个url模式r ^时间/ + /(\ d { 1,2 })/美元”,提取参数括在括号((\ d { 1,2 })在这种情况下),然后将它作为参数传递给hours_ahead函数。如果有许多参数,那么在url模式中写入参数的顺序也会指定它们被传递到相应的视图函数的顺序。您可以在这里进一步阅读文档:https://docs.djangoproject.com/en/dev/topics/http/urls/