在 Django 模板中将字符串值转换为整数

convert string value to integer in Django template

在home.html

<div class="container">
    <div class="row">
        <div class="col-md-6">
            <h3>Select products:</h3>
            <form id="selectProduct" role="search" method="get" action="{% url 'home' %}">
                
                <select name="parameters" data-placeholder="Choose products" class="chosen-select" multiple tabindex="4">
                    {% for p in productnames %}
                        {% if k == p %}
                            <option value="{{ p.productnames }}" selected> {{ p.productnames }} </option>
                        {% else%} 
                            <option value="{{ p.id }}"> {{ p.productnames }} </option>
                            
                        {% endif %}
                    {% endfor %}
                </select><br/>
                <label for="submit"></label><button id="submit" type="submit" class="btn btn-default">Submit</button>
            </form>
        </div>
    </div>
    <div class="row"></div><br />
        <h3> Distribution of sales in the products:</h3>
    </div>
</div>
{% for p in productList %}
    {% for pin in productnames %}
        <p>{{pin.id}}  {{p}}</p>
        {% if p == pin.id %}
            <p>exists</p>
        {% else %} 
            <p>not exist</p>
        {% endif %}
    {% endfor %}
{% endfor %}
<p>{{ productList }}</p>

在这个 html 文件中 'p' 总是 returns ex 的字符串值:它 returns 像 '10' 而不是 10. all我想要的是将这个“10”转换为 10 或将返回的其他 p_in 值转换为 10 到“10”。

在views.py

def productList(request):
if request.method == 'GET':
    p = request.GET.get('parameters')
    print(p)
    #k = request.GET('parameters[]')
    productnames = Products.objects.all()
    context = {
        'productList': p, 'productnames': productnames,
    }
       


return render(request, 'home.html', context)

我尝试将产品列表中 p 的值转换为整数。因为它不符合 pin.id

的格式

您使用 if-else 过滤模板中的查询集,这并不理想。相反,您应该在视图本身中执行此过滤。此外,您的 parameters 是一个 select 标签,它可能有多个 selected 值,但您使用 .get('parameters') 只会给您一个值,而不是您应该使用 getlist method [Django docs] 的 QueryDict:

def productList(request):
    if request.method == 'GET': # Do you even need to check this? You appear to only use a GET request...
        p = request.GET.getlist('parameters')
        productnames = Products.objects.all()
        filtered_products = Products.objects.filter(pk__in=p)
        context = {
            'productList': p, 'productnames': productnames, 'filtered_products': filtered_products
        }
    return render(request, 'home.html', context)

在模板中,您的循环将简单地变为:

{% for product in filtered_products %}
    {{ product.productnames }}
{% endfor %}

Note: You should use a form class instead of manually making a form. See Building a form in Django. Also a models name should be singular hence instead of Products you should name it Product. In general in your code you also break various naming conventions in Python, I would suggest you to look at PEP 8 -- Style Guide for Python Code

在views.py

def productList(request):
if request.method == 'GET':
    p = request.GET.getlist('parameters')
    print(p)
    #k = request.GET('parameters[]')
    productnames = Products.objects.all()
    context = {
        'productList': p, 'productnames': productnames,
    }
    # --- logic later for chart ------    


return render(request, 'home.html', context)

在home.html

<div class="container">
    <div class="row">
        <div class="col-md-6">
            <h3>Select products:</h3>
            <form id="selectProduct" role="search" method="get" action="{% url 'home' %}">
                
                <select name="parameters" data-placeholder="Choose products" class="chosen-select" multiple tabindex="4">
                    {% for p in productnames %}
                        {% if k == p %}
                            <option value="{{ p.productnames }}" selected> {{ p.productnames }} </option>
                        {% else%} 
                            <option value="{{ p.id }}"> {{ p.productnames }} </option>
                            
                        {% endif %}
                    {% endfor %}
                </select><br/>
                <label for="submit"></label><button id="submit" type="submit" class="btn btn-default">Submit</button>
            </form>
        </div>
    </div>
    <div class="row"></div><br />
        <h3> Distribution of sales in the products:</h3>
    </div>
</div>
{% for p in productList %}
    {% for pin in productnames %}
        <p>{{pin.id|stringformat:"s"}}  {{p}}</p>
        {% if p == pin.id|stringformat:"s" %}
            <p>exists</p>
        {% else %} 
            <p>not exist</p>
        {% endif %}
    {% endfor %}
{% endfor %}
<p>{{ productList }}</p>

Note {{value|stringformat:"s"}} can be used to convert integer value to string value