Pass HTML for value to django view (for querying database models)

Viewed 37

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)
1 Answers

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.

def from_DB(request):
    id = request.GET.get("figure_id")
    figures_list = Figure.objects.filter(id=id)
    context = {"figures_list":figures_list}
    return render(request,"app1/data_from_DB.html",context)

This does your required work .

Related