Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

282
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda