Methods for using default view parameters in Django urlconf

Source: Internet
Author: User
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})
  • 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.