A handy feature is that you can assign a default parameter to a view. This allows the default value to be used when the parameter is not assigned.
Example:
# urls.pyfrom django.conf.urls.defaults Import *from mysite Import viewsurlpatterns = Patterns (', (R ' ^blog/$ ', Views.page), (R ' ^blog/page (? P
\d+)/$ ', Views.page),) # Views.pydef page (Request, num= ' 1 '): # Output the appropriate page of blog Entr IES, according to Num. # ...
Here, two URL expressions point to the same view Views.page, but the first expression does not pass any arguments. If the first style is matched, the page () function will use the default value "1" for parameter num, and if the second expression succeeds, the page () function will use the value of NUM passed by the regular expression.
(Note: We have noticed that setting the default parameter value is the string ' 1 ', not an integer ' 1 '.) In order to remain consistent, the value that is captured to ' num ' is always a string.
As explained earlier, this technique is common with configuration options. The following example has a slight improvement over the example provided in the View configuration Options section.
def my_view (Request, template_name= ' mysite/my_view.html '): var = do_something () return Render_to_response ( Template_name, {' var ': var})