Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

283
Visualizações
How to get value from HTML user input in django view?

Hello everyone I have an HTML form as follows: and after clicking on post i am redirecting it to views.py. can any one tell me how to get the field values of all the fields of the form into views.py. here's the output i want the field value in key value pair like shown in above pic i.e. API=hello&Area=hello1 so on...

i know we can do that using this if html:

<div class="form-group col-md-2">
                    <label for="param">TAF Parameter</label>
                    <input type="text" name="inputdata_API" class="form-control" id="inputapi_param" value="API" readonly>
                </div>

and view:

def register(request):
    api = request.GET.get['inputdata_API']

But in that case i have to write each and every input name in my view

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

To get form input without individual access, Django provide ModelForm

For resume :

  1. Define a model to store your form informations

    class MyModel(models.Model): api = models.CharField() # ...

  2. Define a model form linked to the previous model

     from django import forms
    
     class MyModelForm(forms.Form):
         class Meta:
             model = MyModel
             fields = ['api', # all others fields you want to display]
    
  3. In the views.py

     from django.http import JsonResponse
     from django.shortcuts import redirect
    
     def register(request):
         if request.method == 'POST':
              # Instanciate the form with posted data
              form = MyModelFor(request.POST)
              # Check if form is valid
              if form.is_valid:
                  # Create a new MyModel object if the form is valid
                  form.save()  # This is the most benefit line, save you from request.POST['field_name'] 
                  # You can eventually return to the same page
                  return redirect('.') 
              else:  # The form is invalid return a json response
                  return JsonResponse({"Error": "Form is invalid"}, status=400)
    
  4. At the end render the form fields in the template like this :

      <form action="{% url 'url_to_register' %}" method="post" novalidate>
         {% csrf_token %}
         {{ form.as_p }}
    
         <button type="submit" class="btn btn-success">Register</button>
      </form>
    

But the downside of this approach is the style of the form on the frontend : You need to add some Bootstrap class for example in the right place to make it nice to look, it is a a counterpart...

Django form documentation.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda