2

I am trying to access the values of a Bootstrap btn-group from Django and from the documentation I have found, it seems that you should use Forms in Django for such tasks.

This is what the html looks like, right now:

        <div class="col-md-6">
        {% for metric in metrics %}
            <input name="{{ metric.name }}" type="hidden" value="0"/>
        {% endfor %}
            <div class="btn-group" data-toggle="buttons">
            {% for metric in metrics %}
                <button type="button" class="btn btn-default" data-checkbox-name="{{ metric.name }}">{{ metric.name }}</button>
            {% endfor %}
           </div>
        </div>

How can I use forms to get the values of the input fields?

3
  • 2
    Have you looked at the django docs? Commented Jul 8, 2014 at 14:18
  • Yes... but I can't understand how to link the form method in views.py to my html template. (The form method is called get_name in their example). Commented Jul 8, 2014 at 14:23
  • This might help you. Commented Jul 8, 2014 at 14:28

2 Answers 2

2

Here it is a basic example about using a form in django

views.py:

@login_required
def your_view(request):  # Add this code into your view
    if request.method == 'POST':       
        # So here you can do a loop over POST fields like this
        data_list = []  # We will insert all the inputs in this array
        for key in request.POST:  
            data_list.append(request.POST[key])
        # Here you can manage the the data_list and do whatever you need
        # The content of the data_list depend on your inputs
        # It could be string, integer....

    # YOUR VIEW CODE        

template (form example):

<form action="." method="post" id="add_user_form"> 
    {% csrf_token %} 
    {% for metric in metrics %}
        <input type="text" name="{{ metric.name }}" placeholder="whatever you want"> 
    {% endfor %}

    <input type="submit" value="submit" class="default"/>  # Submit button

</form>

{% csrf_token %} : You need to put this in every form you use

action="." : This make the post to the actual page

But anyway I strongly recommend you to check this Django Forms Documentation to unterstand better the logic, and also check the ModelForms because can save you a lot of time when you need to make a form for a model that exists in your Django Models

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

Comments

1

You are'n forced to use django forms, this is just a way to get a sort of organization.

in you views toy can get the values sent to the server by using request.GET or request.POST, depending of the method of the form.

to get a list of values you have received just do a

print request.POST 

request.POST is a dictionary, so you can get any value fron them by its key:

print request.POST['<key>']

Comments

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.