URL path parameters vs query parameters in Django

前端 未结 1 1324
盖世英雄少女心
盖世英雄少女心 2021-01-19 08:21

I\'ve looked around for a little while now and can\'t seem to find anything that even touches on the differences. As the title states, I\'m trying to find out what differenc

相关标签:
1条回答
  • 2021-01-19 08:48

    This would depend on what architectural pattern you would like to adhere to. For example, according to the REST architectural pattern (which we can argue is the most common), you want do design URLs such that without query params, they point to "resources" which roughly correspond to nouns in your application and then HTTP verbs correspond to actions you can perform on that resource.

    If, for instance, your application has users, you would want to design URLs like this:

    GET /users/ # gets all users
    POST /users/ # creates a new user
    GET /users/<id>/ # gets a user with that id. Notice this url still points to a user resource
    PUT /users/<id> # updates an existing user's information
    DELETE /users/<id> # deletes a user
    

    You could then use query params to filter a set of users at a resource. For example, to get users that are active, your URL would look something like

    /users?active=true
    

    So to summarize, query params vs. path params depends on your architectural preference.

    A more detailed explanation of REST: http://www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api

    Roy Fielding's version if you want to get really academic: http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm

    0 讨论(0)
提交回复
热议问题