简体   繁体   English

如何将 CharField 操作为字符串 Django

[英]How to manipulate CharField as a string Django

I have a charfield in which the user inputs a range of numbers which will correspond to a list of numbers.我有一个字符字段,用户在其中输入一系列数字,这些数字将对应于数字列表。 For example the user should be able to input '1, 2, 3, 4, 5' or '1-5', and I want to be able to convert that into a list of all those numbers in the range.例如,用户应该能够输入“1、2、3、4、5”或“1-5”,我希望能够将其转换为范围内所有这些数字的列表。 When I grab the field's cleaned_data in the views.py, however, the value does not behave like a string.但是,当我在views.py 中获取该字段的clean_data 时,该值的行为不像字符串。 The.split() does not convert to a list, and the for loop loops through individual characters. .split() 不会转换为列表,并且 for 循环会遍历单个字符。 How can I convert the field into a usable string?如何将字段转换为可用的字符串?

In the views.py:在views.py中:

def my_view(request):
    if request.method == 'POST':
        if form.is_valid():
            nums = form.cleaned_data['numbers']
            nums.split(',')
            num_list = []
            for item in nums:
                if '-' in item:
                    item.split('-')
                    for x in range(item[0], item[1]):
                        num_list.append(x)
                else:
                    num_list.append(item)

If the input is '1-160', I get an error because the single character '-' can't be split into two items and be indexed.如果输入是“1-160”,我会收到一个错误,因为单个字符“-”不能分成两个项目并被索引。 The expected num_list is a list of all the integers between 1 and 160.预期的 num_list 是 1 到 160 之间的所有整数的列表。

If the input is '1,2,3' num_list is a list of all the characters, not just the numbers.如果输入是 '1,2,3' num_list 是所有字符的列表,而不仅仅是数字。

split() does not work in-place, it returns a list. split()不能就地工作,它返回一个列表。

This means you need to assign a variable for the return value.这意味着您需要为返回值分配一个变量。

nums = nums.split(',')

def my_view(request):
    if request.method == 'POST':
        if form.is_valid():
            nums = form.cleaned_data['numbers']
            nums = nums.split(',')
            num_list = []
            for item in nums:
                if '-' in item:
                    item.split('-')
                    for x in range(item[0], item[1]):
                        num_list.append(x)
                else:
                    num_list.append(item)

nums.split(',') does not convert nums of strings, it returns a list of strings, so you should work with: nums.split(',')转换nums的数量,它返回一个字符串列表,所以你应该使用:

def my_view(request):
    if request.method == 'POST':
        if form.is_valid():
            nums = form.cleaned_data['numbers']
            # ↓ assign the list, for example to nums
            nums = nums.split(',')
            num_list = []
            for item in nums:
                if '-' in item:
                    item.split('-')
                    for x in range(item[0], item[1]):
                        num_list.append(x)
                else:
                    num_list.append(item)

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

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