1

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

    <form action="" style="max-width:500px;margin:auto"> 
            
              <div class="input-container_sensor_form">
                <div class="row">
                    <div class="col-6">
                    <input class="input_field_sensor_form" type="text" placeholder="Sensor name" name="sensor_name">
                    </div>
                    <div class="col-6">
                    <span class="text-center">
                        <select name="sensor_form_sensor_category" class="sensor_form_sensor_category" id="sensor_form_sensor_category" class="select2-hidden-accessible" 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 class="row">
                    <div class="col-6">
                    <input class="input_field_sensor_form" type="text" id="latitude" placeholder="Latitude" name="latitude">
                    </div>
                    <div class="col-6">
                    <input class="input_field_sensor_form" 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" class="btn_sensor_form">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 .

5
  • Basically you would like to save the form data into database successfully, with html form rather than django's form? Commented Apr 13, 2022 at 11:18
  • I'd like to pass the form's input to my AddSensor form and save it to a model instance in my database yes. Im not using forms.ModelForm as there are some differences to my model and those are the only values i want to save Commented Apr 13, 2022 at 11:26
  • 1
    You can make view and get form's data using request.GET.get('any_name') and save it in the model. One more thing remove the empty action attribute, then also it will take current page route. Commented Apr 13, 2022 at 11:28
  • Is there a way to connect my AddSensor fields with the html form's though as to make the validation with .is_valid() easier? Django docs use forms.CharField(label='Your name', max_length=100) is there something similar for my case? Commented Apr 13, 2022 at 11:38
  • That's what i was thinking, you can only use is_valid(), if you render django's form but in your case, you cannot use that, but you can use html functionality in forms for restrictions. Do you want me to write an answer, that how can you save data to model using html's form? Commented Apr 13, 2022 at 11:46

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Well, it is through django's form.
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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.