How to Use url_helper to simplify the url configuration of Django framework in Python

Source: Internet
Author: User

How to Use url_helper to simplify the url configuration of Django framework in Python

This article describes how to use url_helper to simplify the url configuration of the Django framework in Python. For more information, see

Django URLs are configured using regular expressions, which are also widely criticized. Opponents think that django's url configuration is too cumbersome and does not support the default routing function.

I think it's okay, but if I feel uncomfortable, why isn't I just a little hack? It's just a few lines of code.

In this context, I have integrated the url_helper and use url_helper to simplify the configuration and implement the default url routing. The so-called url_helper actually only has a file named url_helper.py. You only need to import the file.

For the usage of url_helper, see the example below:

Url_helper download/Example

The following is a simple description of the usage method.

Default url route

?

1

2

3

4

5

6

From url_helper import execute, url _

Import views

 

Urlpatterns + = patterns ('',

Url (R' ^ (? P <urls>. *) ', execute, {'view': views }),

)

Add the following configuration in urls. py, where views is the View module for routing. The url rule is/action/param1/param2 /... /.

For example:

?

1

2

3

4

5

#/Edit/4/

 

Def edit (request, n = "id "):

Html = "edit object: % s" % n

Return HttpResponse (html)

When no action is specified, the default action is index.

Provide function url _ simplify url Configuration

Like ROR, the parameter is marked.

For example:

?

1

2

3

4

5

6

# Url _ (R'/space/: username/: tag/', views. url _),

#/Space/vicalloy/just/

 

Def url _ (request, username, tag ):

Html = "username: % s <br/> tag: % s" "% (username, tag)

Return HttpResponse (html)

Complete url_helper code

As mentioned above, there are very few codes. However, some extensions are needed for practical applications.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

#! /Usr/bin/env python

#-*-Coding: UTF-8 -*-

From django import http

From django. conf. urls. defaults import url

Import re

 

Def execute (request, urls, views ):

"""

Urls [methodName/] param1/param2 /.../

MethodName default index

"""

Def get_method (views, methodName ):

Try:

Return getattr (views, methodName)

Except t Exception, e:

Return None

Method = None

Params = [e for e in urls. split ("/") if e]

Params. reverse ()

If params:

Method = get_method (views, params. pop ())

If not method:

Method = get_method (views, 'index ')

If not method:

Raise http. Http404 ('the requested admin page does not exist .')

Return method (request, * params)

 

Def url _ (* args, ** dic ):

Regex = args [0]

If regex [0] = "/":

Regex = regex [1:]

Regex = '^' + regex

Regex = regex + '$'

Regex = re. sub (": [^/] + ",

Lambda matchobj :"(? P <% s> [^/] +) "% matchobj. group (0) [1:],

Regex)

Return url (regex, * args [1:], ** dic)

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.