0

I am looking for more elegant way to solve this problem. Say I have say two buttons x, y in main.html:

<input class= "btn-check", name = "options", id="x">
<label class="btn btn-lg btn-info", for="x">x</label>
    
<input class= "btn-check", name = "options", id="y">
<label class="btn btn-lg btn-success", for="y">y</label>

What I want to do is after the button is clicked, I will do something in python and so in django views I will create a view function for each of the buttons (my current implementation):

def funcX(request):
    booleanX = doSomethingforX()
    return JsonResponse({"success": booleanX})

def funcY(request):
    booleanY = doSomethingforY()
    return JsonResponse({"success": booleanY})

and the ajax calls would be:

$("[id='x']").on("click", function(e){
    e.preventDefault();
    
    $.ajax({
        type:"GET",
        url: "{% url 'funcX' %}",           
        success: function(response){
            if(response.success == true){
                //Do something
            }
        }       
    })
});

The ajax call will be the same for button Y.

Now, I was wondering if it is possible to do this with forms? Say the html becomes:

<form method="POST", class="form-group", id="post-form">
    <input type="submit", value="x", class= "btn-check", name = "options", id="x">
    <label class="btn btn-lg btn-info", for="x">x</label>

    <input type="submit", value="y", class= "btn-check", name = "options", id="y">
    <label class="btn btn-lg btn-success", for="y">y</label>

</form>

Then in django views I have a view for the main.html. This way it saves a lot of views written in django.

def main(request):
    
    if request.method == "POST" and request.POST["options"] == "x":
        booleanX = doSomethingforX()
        return JsonResponse({"success": booleanX})
    
    if request.method == "POST" and request.POST["options"] == "y":
        booleanY = doSomethingforY()
        return JsonResponse({"success": booleanY)})
    return render(request, "main.html")

Now the problem is I don't know how to write the ajax calls to receive from views and get the JsonResponse return for X and Y respectively...

Any ideas?

1 Answer 1

1

HTML

<form method="POST", class="form-group", id="post-form">
        {% csrf_token %}
        <input type="text" class= "btn-check" name = "options" id="options_input">
        <button type="submit" id="x">X</button>
        <button type="submit" id="y">Y</button>
</form>

JS

$('#x').click(function(){
    $('#options_input').val("X")
})
$('#y').click(function(){
    $('#options_input').val("Y")
})


$('body').on('submit','#post-form',function(e){
e.preventDefault();
var formData = new FormData(this);
$.ajax({
    type:"POST",
    url: "/url/",
    data: formData, 
    success: function(response){
    },
    error: function (response) {
    },
    cache: false,
    contentType: false,
    processData: false
})

});

VIEWS

def main(request):
   if request.method == "POST":
       submitted_type = request.POST.get("options")
       if submitted_type == "X":
         doSomethingforX()
         return JsonResponse({"success": X)})
       else:
         doSomethingforY()
         return JsonResponse({"success": Y)})
   
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your solution, however doing so lose the "radio buttion" functionality as a group?I was following the bootstrap5 way of writing this: getbootstrap.com/docs/5.1/forms/checks-radios/…
It seems that using ajax to submit "checks-radio-group" do not submit any options, when I print request.POST in django views it only shows csrf token in the dictionary but without any options. But if I remove ajax as middleman it works...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.