将html的值传递给django视图(用于查询数据库模型)
我在HTML页面内有一个表单输入,并希望将表单内输入的图ID传递到我的Django视图中以进行查询,并使用匹配ID显示图形的信息。
我的形式:
<form metohd="GET" id="figure_choice_form">
<label for="figure_id">Enter ID</label>
<input type="text" id="figure_id" name="figure_id">
<button> Submit </button>
</form>
我的views.py
def from_DB(request):
#request being the ID entered from the form
figures_list = Figure.objects.filter(id=request)
context = {"figures_list":figures_list}
return render(request,"app1/data_from_DB.html",context)
I have a form input inside my HTML page and want the figure ID that is entered inside the form to be passed into my django view to do a query, displaying info for the figure with the matching ID.
My form:
<form metohd="GET" id="figure_choice_form">
<label for="figure_id">Enter ID</label>
<input type="text" id="figure_id" name="figure_id">
<button> Submit </button>
</form>
My views.py
def from_DB(request):
#request being the ID entered from the form
figures_list = Figure.objects.filter(id=request)
context = {"figures_list":figures_list}
return render(request,"app1/data_from_DB.html",context)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,将您的HTML代码段更新以纠正“ metohd”属性为“方法”。
您正在通过获取请求发送数据。因此,您可以通过request.get方法访问它。
Django视图的代码片段。
这是您所需的工作。
Firstly , Update your html code snippet to correct form attribute "metohd" to "method" .
You are sending data via GET request . So you can access it via request.GET method .
Code snippet of django view.
This does your required work .