Home > database >  Access data from html form in Django
Access data from html form in Django

Time:04-13

I have created a form in an application that goes like this :

    <form action="" style="max-width:500px;margin:auto"> 
            
              <div >
                <div >
                    <div >
                    <input  type="text" placeholder="Sensor name" name="sensor_name">
                    </div>
                    <div >
                    <span >
                        <select name="sensor_form_sensor_category"  id="sensor_form_sensor_category"  aria-hidden="true" data-select2-id="sensor_form_sensor_category">
                        <option></option>
                        <option name="tree_sensor" >Tree Sensor</option>
                        <option name="weather_sensor" >Weather Station</option>
                        </select>
                      </span>
                    </div>
                </div>
              </div>
                
                <div >
                    <div >
                    <input  type="text" id="latitude" placeholder="Latitude" name="latitude">
                    </div>
                    <div >
                    <input  type="text" id="longitude" placeholder="Longitude" name="longitude">
                    </div>
                </div>
              </div>
                
              <br>
              <div id="map_sensor_form"></div>
              <br>
              <input type="hidden" id="field_id" name="field_id" value="">
              <button type="submit" >Register</button>
        </form>

with the following form :

class AddSensor(forms.Form):
   
    sensor_name = forms.CharField(max_length=200 )
    choice = forms.ChoiceField()
    longitude = forms.DecimalField(max_digits=22, decimal_places=16)
    latitude = forms.DecimalField(max_digits=22, decimal_places=16)

How do i match the inputs with the form ? I've seen in the django doc that its referencing it through label but I do not have any. I want to keep the form as it is .

CodePudding user response:

you can make use of form.cleaned_data
create a model instance and assign values from form.cleaned_data

to form.is_valid() work, you can make sure that the html field id is same as that of AddSensor form field.
for instance: AddSensor form field sensor_name and html field with id sensor_name

# views.py
   
from .models import YourModel # import model
from .forms import AddSensor # import form

def your_view(request):
    if request.method == 'POST':
        form = AddSensor(request.POST)
        if form.is_valid():
            form_data = form.cleaned_data
            obj = YourModel()
            obj.sensor_name = form_data.get("sensor_name")
            # other fields
            obj.save()
            # return or redirect
    else:
        form = AddSensor()

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

here, instead of rendering the form using django forms, the form is hardcoded with the same id's that a django form would render. By matching the id's in the hardcoded html and django form, form.is_valid() can be called. thus form.cleaned_data can be accessed with the form field names

  • Related