Django中的URL路径参数与查询参数 [英] URL path parameters vs query parameters in Django

查看:478
本文介绍了Django中的URL路径参数与查询参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经环顾了一阵子了,似乎找不到任何涉及差异的东西。如标题所示,我试图找出通过URL路径参数(例如 / content / 7 )获取数据并在urls.py中使用正则表达式有什么区别。它们来自使用 request.GET.get()这样的查询参数,例如 / content?num = 7 。 p>

每种方法都有优点和缺点,并且在任何情况下,一个方案显然比另一个方案更好吗?



另外,据我所知,(Django的)首选方法似乎是使用带有正则表达式的url路径参数。除了可能更干净的URL之外,还有其他任何原因吗?欢迎提供与该主题有关的任何其他信息。

解决方案

这取决于您要遵循的架构模式。例如,根据REST体系结构模式(我们可以说这是最常见的),您想要设计URL,使得没有查询参数的情况下,它们指向资源,该资源大致对应于应用程序中的名词,然后HTTP动词对应



例如,如果您的应用程序有用户,则需要设计如下URL:

  GET / users /#获取所有用户
POST / users /#创建一个新用户
GET / users /< id> /#获取具有该ID的用户。注意,该URL仍指向用户资源
PUT / users /< id>。 #更新现有用户的信息
DELETE / users /< id> #删除用户

然后可以使用查询参数来过滤资源中的一组用户。例如,要使用户处于活动状态,您的网址应类似于

  / users?active = true 

总而言之,查询参数与路径参数取决于您的体系结构偏好。



有关REST的更详细说明: http ://www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api



Roy Fielding的版本,如果您想真正获得学术机构: http://www.ics.uci.edu/~ fielding / pubs / dissertation / rest_arch_style.htm


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 difference getting your data via url path parameters like /content/7 then using regex in your urls.py, and getting them from query params like /content?num=7 using request.GET.get() actually makes.

What are the pros and cons of each, and are there any scenarios where one would clearly be a better choice than the other?

Also, from what I can tell, the (Django's) preferred method seems to be using url path params with regex. Is there any reason for this, other than potentially cleaner URLs? Any additional information pertinent to the topic is welcome.

解决方案

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

这篇关于Django中的URL路径参数与查询参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆