gpt4 book ai didi

django - get vs put http 请求对象

转载 作者:塔克拉玛干 更新时间:2023-11-01 19:10:45 24 4
gpt4 key购买 nike

我有一些关于 http get 和 post 的常见问题,如果有人能用例子澄清一下就好了......

例子:-

这是我的看法

View .py

def search(request):
if request.method == 'POST':
print 'request.post=', request.POST
print 'request.get=', request.GET
print 'request.method=', request.META.get('REQUEST_METHOD')
if 'q' in request.GET:
message = 'You searched for :%r' % request.POST['q']
else:
message = 'You submitted an empty form'
return render(request, 'search_form.html', {'message': message})
return render(request, 'search_form.html')

search_form.html

<body>
{% if message %}
<div> {{ message }} </div>
{% else %}
<form action="/polls/search/" method="post">{% csrf_token %}
<input type="text" name="q">
<input type="submit" value="Search">
</form>
{% endif %}
</body>
  1. POST 请求是否仅用于 HTML 表单?如果不能,还有哪些其他方法可以做到?
  2. 任何没有表单的请求始终是 GET 请求?
  3. 如何使用表单处理 GET 和 POST..

谢谢。

最佳答案

Will POST request used only with the HTML forms? If not, what are the other ways to do it?

您也可以发布 json 或 xml。比如jquery使用json或者xml数据类型的ajax post

Any request without a form is always a GET request?

http方法有4种,GET、POST、DELETE和PUT。根据您的使用方式,您可以使用其中的任何一个。

关于此的文章。 http://bradley-holt.com/2009/07/html-5-http-methods-rest/

.

关于django - get vs put http 请求对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15102662/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com