Pass template content to Django view


Well I know it's possible to get content from a view and pass it to a template.

But the question would be how to get the "value" of an input and pass it to a view, an example of use would be.

Get the value of an html input:

<input type="text" name="celular" id="id_celular" value= "xx-xxx" class="form-control">

And pass this value to a Query in the example view:

teste = x.objects.all().filter(celular= "COLOCAR O VALUE AQUI")

If necessary, post the full code.


The recommended way to do this is using Django's forms , it would look something like this.

from django import forms

class NameForm(forms.Form):
    celular = forms.IntegerField(label='Número de telefone', max_length=11)
from django.shortcuts import render
from .models import Celular
from .forms import NameForm

def get_name(request):
    # se for uma requisição POST
    if request.method == 'POST':
        # cria um formulario NameForm com os campos que foram digitados
        form = NameForm(request.POST)
        # checa se é um formulário válido:
        if form.is_valid():
            # pega o número digitado
            number = form.cleaned_data['celular']
            teste = x.objects.all().filter(celular=number)
            # ...

    # se a validação der erro vai aqui
        form = NameForm()

    return render(request, 'name.html', {'form': form})

Now just adapt to your case, take a look at the link I passed there, there are more complete examples, another way would be to associate a model with a form

Scroll to Top