Methods for function import in Django urlconf

Source: Internet
Author: User
Look at this URLconf:

From django.conf.urls.defaults import *from mysite.views import Hello, current_datetime, hours_aheadurlpatterns = Patterns ("," (R '  ^hello/$ ', hello), (R '  ^time/$ ', current_datetime),  (R ' ^time/plus/(\d{1,2})/$ ', Hours_ Ahead),)

Each entry in the URLconf includes a view function associated with it, passing directly to a function object. This means that the view function needs to be imported at the beginning of the module.

But as the Django application becomes more complex, its URLconf is growing, and maintaining these imports can make management cumbersome. (For each new view function, you have to remember to import it, and in this way the import statement will become quite long.) You can avoid this trouble by importing the views module itself. The following example urlconf is equivalent to the previous one:

From django.conf.urls.defaults import ***from mysite Import views**urlpatterns = Patterns (',  (R ' ^hello/$ ', * * views.hello**),  (R ' ^time/$ ', **views.current_datetime**),  (R ' ^time/plus/(d{1,2})/$ ', **views.hours_ahead ** ),)

Django also provides another way to specify a view function for a particular pattern in URLconf: You can pass in a string that contains the module name and function name, not the function object itself. To continue the example:

From django.conf.urls.defaults Import *urlpatterns = Patterns (",  (R ' ^hello/$ ', * * ' mysite.views.hello ' * *),  ( R ' ^time/$ ', * * ' mysite.views.current_datetime ' * *),  (R ' ^time/plus/(d{1,2})/$ ', * * ' mysite.views.hours_ahead ' * *) ,)

(note the quotation marks before and after the view name.) The quoted ' Mysite.views.current_datetime ' should be used instead of the mysite.views.current_datetime. )

With this technique, you do not have to import the view function; Django will import the appropriate view function based on the name and path of the view function described by the string the first time it is needed.

When using string technology, you can take a more streamlined approach: extracting a common view prefix. In our urlconf example, the beginning of each view string is "\", resulting in repeated input. We can extract the common prefix and pass it as the first parameter to the \ ' function:

System MESSAGE:WARNING/2 (
 
  
   
  , line); backlinkinline literal start-string without End-string.from django.c Onf.urls.defaults Import *urlpatterns = Patterns (* * ' mysite.views ' * *,  (R ' ^hello/$ ', * * ' hello ' * *),  (R ' ^time/$ ', * * ' current_datetime ' * *),  (R ' ^time/plus/(d{1,2})/$ ', * * ' hours_ahead ' *
 *),)
  

Note Do not follow the prefix with a dot ("."), or precede the view string with a dot number. Django will handle them automatically.

Which of these two methods is better to keep in mind? It depends on your personal coding habits and needs.

The advantages of the string method are as follows:

More compact because you do not need to import view functions.

If your view function exists in several different Python modules, it makes URLconf easier to read and manage.

The benefits of the function object method are as follows:

It is easier to wrap the view function (Wrap). See the "Packaging View Functions" section later in this chapter.

More pythonic, that is, more in line with Python's tradition, such as passing functions as objects.

The two methods are valid, and even you can mix them in the same URLconf. The decision is yours.

  • 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.