Django – 从在循环中生成的HTML选择选项中获取值

huangapple go评论60阅读模式
英文:

Django - get value from html select options that have been generated in a loop

问题

我有一个HTML页面,在页面上设置了一个循环来列出乐队的名字。我还有一个选择框,让用户对他们看过的乐队进行评分。问题是,因为每个选择框选项都是在我的循环中生成的,我不知道如何在我的views.py文件中访问每个评分。请帮忙?

HTML代码

    {%  for band in  thursday_apex %}
    <li> {{ band.band_name }}
        {{  }}
     <select name="thursday_apex">
        <option value="0">没看过</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
        <option value="8">8</option>
        <option value="9">9</option>
        <option value="10">10</option>
        </select>
        
    </li>
    {% endfor %}
英文:

i have a html page where i have set up a loop to list band names on a page. I also have a select option box so users can rate bands they have seen. Problem is because each of the select options is generated in my loop i don't know how i'm going to access each rating back in my views.py file. Please help?

html code

{%  for band in  thursday_apex %}
<li> {{ band.band_name }}
    {{  }}
 <select name="thursday_apex">
    <option value="0">Didn't watch</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
    <option value="10">10</option>
    </select>
    
</li>
{% endfor %}

答案1

得分: 0

你在for循环中对每个迭代的数据都使用了相同的name属性,要访问每个select元素的value,需要像这样操作:

你的HTML:

{% for band in thursday_apex %}
    <li>{{ band.band_name }}
        <select name="thursday_apex_{{ band.id }}">
            // 选项
        </select>  
    </li>
{% endfor %}

你的views.py:

def function(request):
    thursday_apex = YOUR_BAND_CLASS.objects.all()
    my_list = []
    if request.method == 'POST':
        for item in thursday_apex:
            select_value = request.POST.get('thursday_apex_' + str(item.id))
            if select_value:
                my_list.append({item.id: select_value})
    print(my_list)

现在,你已经将所有select的值存储在变量my_list中。

英文:

you are using the same name attribute for each iterated data in for loop, to access the value of each select element you need to do it like this:

your html:

{%  for band in  thursday_apex %}
&lt;li&gt; {{ band.band_name }}
 &lt;select name=&quot;thursday_apex_{{band.id}}&quot;&gt;
    // options
 &lt;/select&gt;  
&lt;/li&gt;
{% endfor %}

your views.py:

def function(request):
    thursday_apex = YOUR_BAND_CLASS.objects.all()
    list = []
    if request.method == &#39;POST&#39;:
        for item in thursday_apex:
            select_value = request.POST.get(&#39;thursday_apex_&#39; + str(item.id))
            if select_value:
                list.append({item.id: select_value})
    print(list)

now you have all the select values inside the variable list

huangapple
  • 本文由 发表于 2023年6月1日 16:22:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76379963.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定