0
I am starting studies on the use of the Django framework and I am trying to query the database using a form to return a specific value in the database. Follow my template:
<form method="GET">
{% csrf_token %}
<div class="card-body">
<h3 style="text-align:center;">Preencha os dados para pesquisar os dados!</h3>
<input class="input col-md-12" type="text" name="cpf" placeholder="CPF" maxlength="11" style="margin: 4% 0% 2% 0%;">
<button>Pesquisar</button>
</div>
</form>
<table>
<tr>
<th>RESULTADO</th>
</tr>
{% for results in result %}
<tr>
<td>{{ results }}</td>
</tr>
{% endfor %}
</table>
Follow my view:
def consulta(request):
cpf = request.GET('cpf')
result = Login.objetos.filter(cpf)
return render(request, 'pesquisa.html',{'result': result})
When I fill in the form and click to search the page reloads but does not return the result of the query.
Your template is missing for inserting the result content
– Willian Jesus Da Silva
I was making other changes to try to make it work and I put the form in the template, it was like this now:
– Jefferson silva pereira
<method="GET"> {% csrf_token %} <div class="card-body"> <H3 style="text-align:center;">Fill in the data to search the data! </H3> <input class="input col-Md-12" type="text" name="Cpf" placeholder="CPF" maxlength="11" style="margin: 4% 0% 2%0%"> <button>Search</button> </div> </form> <> <tr> <th>RESULT</th> </tr> {% for Results in result %} <tr> <td>{{ Results }}</td> </tr> {% endfor %} </table>
– Jefferson silva pereira
does not return anything? inside the view try to print the result to see if it is restoring something from the database
– Willian Jesus Da Silva
It does not return, I put the print inside the view, but does not print anything, by the console I see that is going the content typed in the input, however, does not come any return
– Jefferson silva pereira
Have you tried to see if there is data inside the Cpf variable ? if it is in the right format when you query the login screen
– Willian Jesus Da Silva
By default requests.GET returns None if you have a problem(do not find the key )
– Willian Jesus Da Silva
I don’t know if there is another way, but by the terminal I see that the variable receives the data, the strange thing is that the GET does not return None or null.
– Jefferson silva pereira
You must fix objects with Objects.
– scavjr