Using default view parameters in Django URLconf

Source: Internet
Author: User
This article describes how to use default view parameters in Django's URLconf. Django is the most famous Python web development framework, for more information, see the next convenient feature. You can specify the default parameters for a view. In this way, the default value is used when this 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 entries, according to num. #...

 

Here, both URL expressions point to the same view views. page, but the first expression does not pass any parameters. If the first style is matched, the page () function uses the default value "1" for the num parameter. If the second expression matches successfully, page () the function uses the num value passed by the regular expression.

(Note: we have noticed that the default parameter value is '''1', not an integer ''1 ''. To maintain consistency, the value captured to ''num'' is always a string.

As explained above, the combination of this technology and configuration options is common. The following example is somewhat improved than the example in the section "view configuration options.

Def my_view (request, template_name = 'mysite/my_view.html '): var = do_something () return render_to_response (template_name, {'var': var })

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.