Making queries in the bank through form using Django

Asked

Viewed 433 times

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

  • I was making other changes to try to make it work and I put the form in the template, it was like this now:

  • <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>

  • does not return anything? inside the view try to print the result to see if it is restoring something from the database

  • 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

  • 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

  • By default requests.GET returns None if you have a problem(do not find the key )

  • 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.

  • You must fix objects with Objects.

Show 4 more comments

1 answer

1

I think that line:

result = Login.objetos.filter(cpf)

is not correct, you should point out which field or attribute of your model should be equal to the value of the CPF variable.

result = Login.objetos.filter(<nome_atributo_model>=cpf)

I don’t remember seeing the . filter() function being used the way you used it.

  • I tried to put it that way, but I had no response. From the examples I saw on the net in the form no action is defined to point to a specific page or url, then I get confused if the url is accessing the function inside the view or not

  • the url that loads the HTML you reported is pointing to the function you created?

  • I can’t tell you, since I’m starting school now, I’m still a little lost. As I do not define any action in the form it is difficult to say whether the function is being called.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.