开发者

How can reduce the number of URL rewriting configs in Django

开发者 https://www.devze.com 2023-03-15 20:25 出处:网络
I have this listing in django. These looks identical. Is there any way to make one line from these four lines

I have this listing in django. These looks identical. Is there any way to make one line from these four lines

(r'^book/list/$', list_detail.object_list, book_info),
(r'^author/list/$', list_detail.object_list, author_info),
(r'^publisher/list/$', list_detail.object_list, publisher_info),
(开发者_JAVA百科r'^shop/list/$', list_detail.object_list, shop_info),


I would prefer to leave it like it is so you can clearly see the urls of your site (and each list can have a name for use with {% url %}, but perhaps you can do something like:

(r'^(?P<model>\w+)/list/$', list_detail.object_list)

Then in the views.py:

def object_list(request, model):
    if model == 'books':
        return books_list(request)

def books_list(request):
    # whatever

I think what you have is best though.


You can combine your terms in the regular expression, using an OR character which is designed by the pipe symbol. Something like this: (r'^(book|author|publisher|shop)/list/$', list_detail.object_list, shop_info),

I'm presuming that you don't want to use a wildcard character.

0

精彩评论

暂无评论...
验证码 换一张
取 消