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

292
Views
Raise an error in Django on specific field in form

In my Django/Python application I have fields which are necessary or not, depending on what is selected in previous fields. Thats why those are not mandatory by default. I would like to run a script which would raise an error on the chosen field, depending on the selection. I would like to do that when 'Submit' button is pressed, via script.

My html code:

{% extends "blog/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
    <div class="content-section">

        <form method="POST" id="PostForm" data-sektor-url="{% url 'ajax_load_sektors' %}" novalidate enctype="multipart/form-data">
            {% csrf_token %}
            <fieldset class="form-group">
                <legend class="border-bottom mb-4">Report</legend>
                {{ form|crispy }}
            </fieldset>
            <div class="form-group">
                <button class="btn btn-outline-info" id="submit" type="submit">Submit</button>
            </div>
        </form>
    </div>

    <script>
            $("#submit").click(function () {

                if (document.getElementById('id_res_person_1').value == '') {
                      HERE I WOULD RAISE AN ERROR ON THAT FIELD
                    }
                            });
    </script>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You need to call a function when form is submitted, make your validation and if all good submit to the view

Here is an example:

JS

function validate(url) {
  // do your validation 
  if(allIsOk){
    // get the form
     my_form.submit();
   }
 } 

On Submit or link

 <fom onsubmit="validate()">
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!