简体   繁体   English

将逗号分隔的字符串转换为列表,然后在Django模板中显示列表

[英]Converting comma separated string to list, then display list in Django template

I'm trying to take an input that is a comma separated string value, store those items in a list for temporary display and use in a template. 我正在尝试使用逗号分隔的字符串值作为输入,将这些项目存储在列表中以供临时显示并在模板中使用。 I'm currently having trouble storing each comma separated string value in a list, then displaying that list in a template. 我目前在将每个逗号分隔的字符串值存储在列表中,然后在模板中显示该列表时遇到麻烦。

A user inputs something similar into a database ModelForm field (without the quotes): 用户在数据库的ModelForm字段中输入类似的内容(不带引号):

example input from a ModelForm 来自ModelForm示例输入

inputs = models.CharField(max_length=200)

Then the user would input something to the effect of 然后用户将输入一些效果

"length_ft,depth_in,width_in" “length_ft,depth_in,width_in”

minus the quotes. 减去引号。

The following then sort-of works in the template: 然后在模板中进行以下排序:

{% for i in inputs_list %}
    {{ i }}
{% endfor %}

Instead of displaying a list of list items, it simply lists each letter of each list item separately. 它没有显示列表项列表,而是仅单独列出每个列表项的每个字母。 I realize why, because I am stripping out commas, but then I'm trying to append the blank list with each csv item. 我知道为什么,因为我要去除逗号,但是随后我尝试将空白列表附加到每个csv项目中。

I don't understand how to get items out of the input string to a list, then to display in the template. 我不明白如何从输入字符串中获取项目到列表,然后显示在模板中。

views.py views.py

def test_formula(request, formula_id):
    formula = Formula.objects.get(id=formula_id)
    inputs_list = []
    for inputs in formula.inputs:
        strip = inputs.strip(",")
        inputs_list.append(strip)

    context = {'formula': formula, 'inputs_list': inputs_list}
    return render(request, 'formulas/test_formula.html', context)

formulas/test_formula.html 公式/ test_formula.html

(this just breaks up each character into it's own item) (这只是将每个字符分解成自己的项目)

{% load static %}
{% block content %}

{% for i in inputs_list %}
{{ i }}
{% endfor %}


{% endblock content %}

formulas\\urls.py 公式\\ urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('test_formula/<int:formula_id>', views.test_formula, name='test_formula'),
]

Assuming you have a string of comma separated values such as "length_ft,depth_in,width_in" , update your code to: 假设您有一串用逗号分隔的值,例如"length_ft,depth_in,width_in" ,请将代码更新为:

def test_formula(request, formula_id):
    formula = Formula.objects.get(id=formula_id)

    context = {'formula': formula, 'inputs_list': formula.inputs.split(",")}
    return render(request, 'formulas/test_formula.html', context)

Basically, you need to us the str method split which returns a list of the words in the string, using the parameter that you pass as the delimiter string. 基本上,您需要使用str方法split ,该方法使用作为定界符字符串传递的参数返回字符串中单词的列表。

I would also suggest changing the variable i to input in the for in the template. 我还建议更改变量i以在模板的for中input

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM