Django and query string parameters

后端 未结 3 1071
终归单人心
终归单人心 2020-12-08 04:15

Assuming I have a \'get_item\' view, how do I write the URL pattern for the following php style of URL?

http://example.com/get_item/?id=2&type=foo&co         


        
3条回答
  •  我在风中等你
    2020-12-08 04:45

    Make your pattern like this:

    (r'^get_item/$', get_item)
    

    And in your view:

    def get_item(request):
        id = int(request.GET.get('id'))
        type = request.GET.get('type', 'default')
    

    Though for normal detail views etc. you should put the id/slug in the url and not in the query string! Use the get parameters eg. for filtering a list view, for determining the current page etc...

提交回复
热议问题