Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

281
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!