简体   繁体   English

如何将html中表单的值获取到django

[英]How to get the value of a form in html to django

<form action="{% url 'search' %}" method="get">
       <input class="search" type="text" name="q" placeholder="Search Encyclopedia"> 
</form>

How can I get the value of q to views.py如何将 q 的值获取到 views.py

def search(request):
    return render(request, "encyclopedia/search.html")

Should I make it a post request instead of a get and then take the value.我是否应该将其设为发布请求而不是获取,然后获取该值。 Help me plz请帮帮我

You use GET method, so you can use the HttpRequest.GET dictionary containing all given HTTP GET parameters.您使用 GET 方法,因此您可以使用包含所有给定 HTTP GET 参数的 HttpRequest.GET 字典。

A simple :一个简单的 :

request.GET.get('q')

will do the job.将完成这项工作。

See https://docs.djangoproject.com/en/4.0/ref/request-response/请参阅https://docs.djangoproject.com/en/4.0/ref/request-response/

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

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