Django通过一个Charfield获得多个输入

时间:2019-01-08 07:05:33

标签: python django python-3.x django-forms django-views

我在Django视图中创建了一个列表,我在edit_files.html中的该列表上进行迭代,并将CharField放在列表的每个项目附近,并显示在HTML页面中。因此,当要在字段中输入文本时,我单击提交,仅获取字段的输入。我的问题是如何获取列表中的所有输入? https://github.com/Arabyan/N_pr/commits/master github

  

edit_files.html
   因此,在{{each_var}}
{{form.as_p}}中,我有了带有CharField的列表项的名称

{% block content %}

<form action="." method="POST" enctype="multipart/form-data">{% csrf_token %}

    {% if variables %}

        {% for each_variable in variables %}

            {% for each_var in each_variable %}

                {{ each_var }} <br/>{{ form.as_p }}

            {% endfor %}

        {% endfor %}

    {% endif %}
    <input type="submit" value="Save" class="btn btn-primary"/>
</form>

{% endblock %}
  

views.py
   您只能查看视图的第二部分

 def edit_files(request, file_id):
        instance = get_object_or_404(DocFile, id=file_id)
        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')


        # print(variables)
        my_form = RawProductionForm()
        if request.method== "POST":
            my_form = RawProductionForm(request.POST)
            if my_form.is_valid():
                print(my_form.cleaned_data['title_forms'])
            else:
                print(my_form.errors)
        context = {
            "form": my_form
        }
        return render(request, 'edit_files.html', {'variables': variables, "form": my_form })
  

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='')

0 个答案:

没有答案