Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用多个选择选项并选择这些选项以保持选中状态?_Python_Django - Fatal编程技术网

Python 如何使用多个选择选项并选择这些选项以保持选中状态?

Python 如何使用多个选择选项并选择这些选项以保持选中状态?,python,django,Python,Django,使用复选框selectmultiple()保存数据选项后未选择表单小部件值 forms.py class Client_ProcessForm(forms.Form): process = forms.ModelChoiceField(queryset= Process.objects.all(), widget=forms.CheckboxSelectMultiple) def Edit_Client(request, id): form = Client_ProcessFo

使用
复选框selectmultiple()保存数据选项后未选择表单小部件值

forms.py

class Client_ProcessForm(forms.Form):
    process = forms.ModelChoiceField(queryset= Process.objects.all(), widget=forms.CheckboxSelectMultiple)
def Edit_Client(request, id):
    form = Client_ProcessForm(request.POST)
    edit_record = Client.objects.get(pk=id)
    obj = Client.objects.all()
    cp = Client_Process.objects.filter(client_id=edit_record)
    cp1 = cp.values_list('process')
    obj1 = {}
    for i in range(len(cp1)):    
        ps = Process.objects.filter(id__in=cp1[i])
        print(ps)
        obj1[cp1[i][0]] = list(ps)
    return redirect('/client_list/')
    return render(request, 'edit_client.html',{'edit_record': edit_record, 'form' : form, 'obj' : obj, 'cp' : obj1})
视图.py

class Client_ProcessForm(forms.Form):
    process = forms.ModelChoiceField(queryset= Process.objects.all(), widget=forms.CheckboxSelectMultiple)
def Edit_Client(request, id):
    form = Client_ProcessForm(request.POST)
    edit_record = Client.objects.get(pk=id)
    obj = Client.objects.all()
    cp = Client_Process.objects.filter(client_id=edit_record)
    cp1 = cp.values_list('process')
    obj1 = {}
    for i in range(len(cp1)):    
        ps = Process.objects.filter(id__in=cp1[i])
        print(ps)
        obj1[cp1[i][0]] = list(ps)
    return redirect('/client_list/')
    return render(request, 'edit_client.html',{'edit_record': edit_record, 'form' : form, 'obj' : obj, 'cp' : obj1})
html

<select name="process">
                {% for entry in form %}
                <option value="{{ entry }}">{{ entry.process }}</option>
                {% endfor %}
            </select>

{%表格%中的条目为%1}
{{entry.process}
{%endfor%}

可以使用javascript完成。检查以下示例:

烧瓶试验

from flask import Flask, request, render_template

app = Flask(__name__)
app.config['DEBUG'] = True


@app.route('/')
    def index():
    nl = ['option 1', 'option 5', 'option 6' ]
    form = ['option 1', 'option 2', 'option 3', 'option 4', 'option 5', 'option 6']
    return render_template('index.html', nl=nl, form=form)


if __name__ == "__main__":
    app.run()
index.html

<html>
<body onload="runme();">
    <select multiple>
{% for entry in form %}
        <option value="{{ entry }}">{{ entry }}</option>
        {% endfor %}
    </select>

</body>
<script>
function runme() {
    var nl = [];
    var options = document.getElementsByTagName('select')[0].options;
    {% for vl in nl %}
        nl.push('{{vl}}');
    {% endfor %}
    for (i=0;i<options.length;i++){
        for (j=0;j<nl.length;j++){
            if (options[i].value == nl[j]){
                options[i].selected = 'selected';
            }
        }
    }
};
</script>
</html>

{%表格%中的条目为%1}
{{entry}}
{%endfor%}
函数runme(){
var nl=[];
var options=document.getElementsByTagName('select')[0].options;
{nl%中vl的%1}
nl.push('{vl}}');
{%endfor%}

对于(i=0;i,可以使用javascript完成。请检查以下示例:

烧瓶试验

from flask import Flask, request, render_template

app = Flask(__name__)
app.config['DEBUG'] = True


@app.route('/')
    def index():
    nl = ['option 1', 'option 5', 'option 6' ]
    form = ['option 1', 'option 2', 'option 3', 'option 4', 'option 5', 'option 6']
    return render_template('index.html', nl=nl, form=form)


if __name__ == "__main__":
    app.run()
index.html

<html>
<body onload="runme();">
    <select multiple>
{% for entry in form %}
        <option value="{{ entry }}">{{ entry }}</option>
        {% endfor %}
    </select>

</body>
<script>
function runme() {
    var nl = [];
    var options = document.getElementsByTagName('select')[0].options;
    {% for vl in nl %}
        nl.push('{{vl}}');
    {% endfor %}
    for (i=0;i<options.length;i++){
        for (j=0;j<nl.length;j++){
            if (options[i].value == nl[j]){
                options[i].selected = 'selected';
            }
        }
    }
};
</script>
</html>

{%表格%中的条目为%1}
{{entry}}
{%endfor%}
函数runme(){
var nl=[];
var options=document.getElementsByTagName('select')[0].options;
{nl%中vl的%1}
nl.push('{vl}}');
{%endfor%}

对于(i=0;i也被同样的问题困扰着我也被同样的问题困扰着你的答案和时间,但它不起作用properly@dhina什么是工作不正常?它显示select objectUpdate上的第二个列表工作正常,但一旦查看更新的详细信息复选框勾号未显示,如何显示勾号w在选项[i]中显示传递的值,如“谢谢你的回答和时间,但它不起作用properly@dhina什么是工作不正常?它显示select objectUpdate上的第二个列表工作正常,但一旦查看更新的详细信息复选框勾号未显示,如何在显示选项[i]值时显示勾号,如“