Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django template keyword `choice_value` in no longer work in 1.11

There is a multiple checkbox in template, if value contain in render the choice will checked by default. It works well with 1.10.

form.py:

class NewForm(forms.Form):
    project = forms.ModelMultipleChoiceField(
        widget=forms.CheckboxSelectMultiple, 
        queryset=Project.objects.filter(enable=True)
    )

template:

{% for p in form.project %}
<label for="{{ p.id_for_label }}">
    <input type="checkbox" name="{{ p.name }}" id="{{ p.id_for_label }}"
        value="{{ p.choice_value }}"
        {% if p.choice_value|add:"0" in form.project.initial %} checked{% endif %}>
    <p>{{ p.choice_label }}</p>
</label>
{% endfor %}

views.py:

def order_start(request, order_id):
    if request.method == 'POST':
        form = NewForm(request.POST)
        if form.is_valid():
            order.end_time = timezone.now()
            order.save()
            order.project = form.cleaned_data['project']
            order.save()
            return HttpResponsec(order.id)
    else:
        form = NewForm(initial={
            'project': [p.pk for p in order.project.all()],
        })

    return render(request, 'orders/start.html', {'form': form, 'order': orderc})

When I upgrade to Django 1.11, {{ p.name }} and {{ p.choice_value }} return nothing. I know 1.11 has removed choice_value, but how to solve this problem?

1.10 https://docs.djangoproject.com/en/1.10/_modules/django/forms/widgets/
1.11 https://docs.djangoproject.com/en/1.11/_modules/django/forms/widgets/

like image 795
fen Avatar asked Apr 28 '17 07:04

fen


People also ask

What is Is_valid () do in Django?

The is_valid() method is used to perform validation for each field of the form, it is defined in Django Form class. It returns True if data is valid and place all data into a cleaned_data attribute. Let's see an example that takes user input and validate input as well.

What does {% %} mean in Django?

{% %} and {{ }} are part of Django templating language. They are used to pass the variables from views to template. {% %} is basically used when you have an expression and are called tags while {{ }} is used to simply access the variable.

What is Forloop counter in Django?

A for loop is used for iterating over a sequence, like looping over items in an array, a list, or a dictionary.

What does the built in Django template tag Lorem do?

lorem. Displays random “lorem ipsum” Latin text. This is useful for providing sample data in templates. A number (or variable) containing the number of paragraphs or words to generate (default is 1).


1 Answers

As @L_S 's comments. I debug with dir(form), all value contained in form.project.data here's the correct code:

{% for choice in form.project %}
<labelc for="{{ choice.id_for_label }}">
    <input type="checkbox" name="{{ choice.data.name }}" id="{{ choice.id_for_label }}" 
    value="{{ choice.data.value }}"{% if choice.data.selected %} checked{% endif %}>
    {{ choice.data.label }}
</label>
{% endfor %}
like image 92
fen Avatar answered Oct 17 '22 20:10

fen