How to capture text from URLs in Python's Django framework

Source: Internet
Author: User
Each captured parameter is sent as a pure Python string, regardless of the format in the regular expression. For example, in this line of urlconf:

(R ' ^articles/(? P
 
  
   
  \d{4})/$ ', views.year_archive),
 
  

Although \d{4} will only match the string of integers, the parameter year is passed as a string to views.year_archive () instead of an integral type.

It is important to remember this when you write the view code, and many of the python built-in methods are very particular about the types of objects that are accepted. Many built-in Python functions are fastidious (and rightly so) to accept only certain types of objects. A typical mistake is to create a Datetime.date object with a string value instead of an integer value:

>>> Import datetime>>> datetime.date (' 1993 ', ' 7 ', ' 9 ') Traceback (most recent call last):  ... Typeerror:an integer is required>>> datetime.date (1993, 7, 9) datetime.date (1993, 7, 9)

Back at the urlconf and view, the error seems likely to be this:

# urls.pyfrom django.conf.urls.defaults Import *from mysite Import viewsurlpatterns = Patterns (",  (R ' ^articles/(\d {4}) /(\d{2})/(\d{2})/$ ', views.day_archive),) # Views.pyimport Datetimedef day_archive (request, year, month, day):  # The following statement raises a typeerror!  Date = Datetime.date (year, month, day)

Therefore, day_archive () should be correct in this way:

def day_archive (request, year, month, day):  date = datetime.date (int.), Int (month), Int (day)

Note that when you pass a string that does not completely contain a number, int () throws an ValueError exception, but we have avoided this error because in the regular expression of urlconf it is ensured that only strings containing numbers are passed into the view function.
Decide what to urlconf search for

When a request comes in, Django tries to urlconf the requested URL as a normal Python string (rather than as a Unicode string). This does not include GET or POST parameters or domain names. It also does not include the first slash, because each URL must have a slash.

For example, in a request to http://www.example.com/myapp/, Django will try to match myapp/. In the request to Http://www.example.com/myapp/?page=3, Django also goes to match myapp/.

The request method (for example, POST, GET, HEAD) is not considered when parsing urlconf. In other words, all request methods for the same URL will be directed to the same function. Therefore, it is the responsibility of the view function to handle the branch according to the request method.

  • Related Article

    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.