How to capture text from a url in the Django framework of Python

Source: Internet
Author: User
This article mainly introduces how to capture text from URLs in the Python Django framework, as well as some URLconf searches, you can refer to each captured parameter that will be 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 an integer string, the year parameter is passed as a string to views. year_archive () instead of an integer.

It is important to remember this when writing View code. many built-in Python methods are very considerate about the types of accepted objects. Many built-in Python functions are picky (this is of course) and only accept specific types of objects. A typical error is to use a string value instead of an integer to create a datetime. date object:

>>> 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)

Return to URLconf and view. the error may look like 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 correctly written as follows:

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

Note: When you pass a string that does not fully contain digits, int () will throw a ValueError exception, but we have already avoided this error, this is because the regular expression in URLconf ensures that only strings containing numbers are uploaded to this view function.
Determine what to search for in URLconf

When a request comes in, Django tries to match the request URL as a normal Python string in the URLconf mode (instead of a Unicode string ). This does not include GET or POST parameters or domain names. It does not include the first slash, because each URL must have a slash.

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

When parsing URLconf, request methods (such as POST, GET, and HEAD) are not considered. In other words, all request methods for the same URL will be directed to the same function. Therefore, it is the responsibility of View functions to process branches based on request methods.

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.