简体   繁体   English

Django / Python RegEx-'?id ='是做什么的?

[英]Django / Python RegEx - what does '?id=' do?

I'm reading a Django tutorial and in the tutorial, the urls.py is this: 我正在阅读Django教程,在该教程中,urls.py是这样的:

(r'^vote/$', bookmark_vote_page),

and there is a model called 'SharedBookmark': 并且有一个名为“ SharedBookmark”的模型:

class SharedBookmark(models.Model):
    bookmark = models.ForeignKey(Bookmark, unique=True)

but in the template, the link which leads to /vote/ is this: 但是在模板中,指向/ vote /的链接是这样的:

{% if shared_bookmarks %}
     <ul class="bookmarks">
     {% for shared_bookmark in shared_bookmarks %}
         <li>
             <a href="/vote/?id={{ shared_bookmark.id }}" class="vote">[+]</a>

As you can see in the line: 如您所见:

<a href="/vote/?id={{ shared_bookmark.id }}" class="vote">[+]</a>

the link does not just go to /vote/, it goes to /vote/ with a '?id=x' where x is a number. 该链接不仅会转到/ vote /,还会转到带有'?id = x'的/ vote /,其中x是数字。

The view which handles the link is this: 处理链接的视图是这样的:

def bookmark_vote_page(request):
     if request.GET.has_key('id'):
         try:
             id = request.GET['id']

My question is, what does '?id=x' exactly do? 我的问题是,'?id = x'到底能做什么? Because it does NOT change the URL (when I click the link, it still goes to /vote/). 因为它不会更改URL(当我单击链接时,它仍然转到/ vote /)。

I tried google'ing but all the explanations I saw explained what ?P= is but not what ?id= does. 我尝试了google'ing,但看到的所有解释都说明了?P =是什么,但没有说明?id =的作用。 I remember seeing a django documentation which explains it but I can't find the link to the documentation anymore for some reason. 我记得看到django文档对其进行了解释,但由于某种原因我再也找不到该文档的链接。

Sam explained how the URL is put together. Sam解释了如何将URL组合在一起。 Continuing from there... 从那里继续...

In Django, the query strings are turned into a dictionary-like object. 在Django中,查询字符串变成了一个类似字典的对象。 Since the request is a GET request (the kind that puts query parameters into the URL instead of the request body), this dictionary-like object is stored at request.GET . 由于请求是GET请求(将查询参数放入URL而不是请求正文的类型),因此该类字典对象存储在request.GET So when the view executes this line: 因此,当视图执行此行时:

 if request.GET.has_key('id'):

it's checking if the URL had a query string parameter with the name "id" at all. 它会检查网址中是否包含名称为“ id”的查询字符串参数。 Once it's sure, it continues by getting it with: 确定后,将继续进行以下操作:

 id = request.GET['id']

BTW, it's recommended (for readability, mostly) to instead check for query parameter presence like this: 顺便说一句,建议(主要是为了提高可读性)改为检查查询参数是否存在,如下所示:

 if 'id' in request.GET:

? in URLs start a query string . 在URL中开始查询字符串 Query strings are list of parameters in the form: 查询字符串是以下形式的参数列表:

?name1=value1&name2=value2&name3=value3

This is an easy way to pass data to modify GET requests. 这是传递数据以修改GET请求的简便方法。


In most HTTP requests, like POST/PUT/DELETE/etc., you have the ability to send data along with the request. 在大多数HTTP请求中,例如POST / PUT / DELETE /等,您都可以随请求一起发送数据。 In GET requests, you should only be asking for content from a certain URL. 在GET请求中,您只应从某个URL询问内容。 But sometimes, you would like to request a URL but receive slightly modified contents. 但是有时,您想请求一个URL,但收到的内容略有修改。 Think about searches or query parameters: 考虑搜索或查询参数:

GET /search?q=example
GET /search?q=example&p=2
GET /search?q=new%20word

My final note more so applies to the design of RESTful APIs. 我的最后一句话更适用于RESTful API的设计。 I say you can "pass data" with query strings in GET requests, but depending on the kind of data you pass it could be bad design to do so. 我说您可以在GET请求中使用查询字符串“传递数据”,但是根据传递的数据类型,这样做可能会很糟糕。 In general, query parameters should do something along the lines of: 通常,查询参数应遵循以下原则:

  • filtering 过滤
  • sorting 分类
  • modifying 修改

This would include things like ?q=example for filtering a set of results, ?sort=alphanumeric for sorting results, and ?return=json to modify the response type. 这将包括诸如?q=example来过滤一组结果, ?sort=alphanumeric来对结果进行排序以及?return=json来修改响应类型之类的内容。 If you are ever sending data to modify a DB , you should probably not be using a GET request. 如果要发送数据来修改DB ,则可能不应该使用GET请求。 For example, registrations should use a POST request (since it is creating something) and modifying a user's settings should use a PUT request (since it is updating something). 例如,注册应使用POST请求(因为它正在创建内容),而修改用户的设置应使用PUT请求(因为它正在更新内容)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM