简体   繁体   中英

Django: How can I create a dynamic form that changes on user click?

I'm making a workout calendar website where a user can add workouts with varying amounts of lift, sets and reps, etc. Thus, I need a form that adds a field when a user clicks a button. I've made a template and some javascript to describe what it is I want to achieve exactly:

url:

url(r'^add/(?P<year>[0-9]+)/(?P<month>[0-9]+)/(?P<day>[0-9]+)/$', views.add_workout, name = 'add_workout')

template:

{% block hidden %}
{% include "workoutcal/liftrow.html" %} {# To be used by Javascript #}
{% include "workoutcal/cardiorow.html" %}
{% endblock %}

<form action="{% url 'add_workout' date.year date.month date.day %}" method="post">

    <div class="row">
        <div class="col-xs-2">
            <p id="date">{{ date.year }}-{{ date.month }}-{{ date.day }}</p>
            <input type="hidden" name="date" value="{{ date }}">
        </div>
    </div>

    <h2 class="col-xs-12">Lifts</h2>
    <div id="liftrows">
        {% for i in range %}
          {% include "workoutcal/liftrow.html" %}
        {% endblock %}
    </div>

    <div class="row">
        <div class="col-xs-0"></div>
        <label class="col-xs-2"><button type="button" id="addliftbutton">One more lift</button></label>
    </div>



    <h2 class="col-xs-12">Cardio</h2>

    <div id="cardiorows">
        {% include "workoutcal/cardiorow.html" %}
    </div>

    <div class="row">
        <label class="col-xs-2"><button type="button" id="addcardiobutton">One more cardio</button></label>
    </div>
    <div class="row">
        <div class="col-xs-10"></div>
        <label class="col-xs-2"><input type="submit" id="submitbutton" value="Save Workout"></label>
    </div>
</form>

javascript:

//Adding onclick to buttons
document.getElementById('addliftbutton').onclick = addLiftRow;
document.getElementById('addcardiobutton').onclick = addCardioRow;

for (var i=0; i<setsBoxes.length; i++){
    setsBox = setsBoxes[i];
    setsBox.onchange = insertRepFields;
}

function addLiftRow(){
    var liftRowElements = document.getElementById('liftrows');

    var hidden_liftrow = document.getElementById('hidden').getElementsByClassName('lift')[0];
    var new_liftrow = hidden_liftrow.cloneNode(true);

    liftRowElements.appendChild(new_liftrow);
}

function addCardioRow(){
    var cardiorows = document.getElementById('cardiorows');

    var hidden_cardiorow = document.getElementById('hidden').getElementsByClassName('cardio')[0];
    var new_cardiorow = hidden_cardiorow.cloneNode(true);

    cardiorows.appendChild(new_cardiorow);
}

function insertRepFields(){} // big function that inserts as many input fields as the number inside the box whose event called the function.

2 questions: 1. Is there a better way to do this in Django? 2. If this is the best way, how do I go about sending the data of my massive form back to django? Since I don't know exactly how many fields there will be, I don't know how to create a form that accepts a variable amount of fields, and fields within fields.

Here's how a filled-in form could look:

在此处输入图片说明

The best way to accomplish that is inserting inputs with the same name and then in Django get all those inputs as a list like:

def view(request):
    inputs = request.POST.getlist('your_input_name')
    for i in inputs:
        Model.objects.create() # Save your model

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM