Как использовать Django Formsets с разными метками? - PullRequest
0 голосов
/ 13 января 2019

Как создать различные метки для моих Django Formsets? У меня есть поля ввода на моей html-странице, созданные с помощью форм Charfield, и каждый раз количество полей ввода различается в зависимости от моего списка. Я хочу, чтобы каждый раз, когда у моих входов были разные ярлыки или названия?

edit_files.html

{

    % block content %}

    <form action="." method="POST">{% csrf_token %}

            {{ form.management_form }}

            {% for each_f in form %}

                <br/>{{ each_f.as_p }}   {{ 1|add:1}}

            {% endfor %}

        <input type="submit" value="Save" name="lines" />
    </form>

    {% endblock %}

forms.py

from django import forms
from .models import DocFile

class DocumentForm(forms.ModelForm):
    class Meta:
        model = DocFile
        fields = ('description','document')


class RawProductionForm(forms.Form):
    title_forms = forms.CharField(label='')

views.py

def edit_files(request, file_id):
    instance = get_object_or_404(DocFile, id=file_id) # finding variables in the doc
    exact_file = Document(instance.document)
    variables = []
    for paragraph in exact_file.paragraphs:
        match = re.findall(r"\{(.*?)\}", paragraph.text)
        variables.append(match)
    for table in exact_file.tables:
        for row in table.rows:
            for cell in row.cells:
                match = re.findall(r"\{(.*?)\}", cell.text)
                variables.append(match)

    exact_file.save('green.pdf')
    temporary_list = []  # as with a Paragraph code we get lists of list , and have multiple little lists we get rid of empty lists
    for i in variables:
        for j in i :
            if len(j) > 0:
                temporary_list.append(j)
    variables = temporary_list

    inputs_amount = len(variables)  #getting length of list for   Formsets amount
    print(variables)

    my_form = formset_factory(RawProductionForm, extra=inputs_amount, )  #creating forms
    inputs_list = []     # getting data out of inputs and filling in a list
    if request.method== "POST":
        my_form = my_form(request.POST)
        if my_form.is_valid():
            for i in my_form.cleaned_data:
                inputs_list.append(i.get('title_forms'))
            print(inputs_list)
        else:
            print(my_form.errors)
    context = {
        "form": my_form
    }
    return render(request, 'edit_files.html', {'variables': variables, "form": my_form })
...