html 在Django中动态加载数据字段的示例文件

iqxoj9l9  于 11个月前  发布在  Go
关注(0)|答案(3)|浏览(72)

我在Django模板中有一个文件上传按钮。当用户单击“选择文件”选项时,可以加载选定的文件。
我试图添加一个额外的选项,用户可以加载存储在后端的示例文件。
当用户单击#1时,必须自动加载文件example1.xlsx。#2,3,4也是如此。
代码:

<div id=main class="cont right">
                <h2>Submit new task</h2>
                <form action="{% url 'add_file' %}" method="post" enctype="multipart/form-data">
                    {%  csrf_token %}
                    <!-- <input type="text" name="filename" placeholder="Enter File Name"><br>
                    <input type="file" name="file"><br>-->
                    <table style="margin:auto; display: block; width: 350px;">
                        <div class="form-group">
                            <tr>
                                <td class="td_1"><label for="{{form.file_name.id_for_label}}">Job Name</label></td>
                                <td>
                                <div class="cut example-cut">
                                    <label for="examples" class="placeholder">
                                        Examples
                                        <span id="example1" class="example" onclick="setExampleValue('example1.xlsx')"> #1</span>
                                        <span id="example2" class="example" onclick="setExampleValue('example2.xlsx')"> #2</span>
                                        <span id="example3" class="example" onclick="setExampleValue('example3.xlsx')"> #3</span>
                                        <span id="example3" class="example" onclick="setExampleValue('example4.xlsx')"> #4</span>
                                    </label>
                                    </div>
                                    {{form.file_name}}
                                </td>
                            </tr>
                            <tr style="padding:5px;">
                                <td style="padding:5px;"><label for="{{form.files.id_for_label}}">File Name</label></td>
                                <td onclick="myFunction()">{{form.files_data}}</td>
                            </tr>
                        </div>
                    </table>
                    <button type="submit" class="submit" onclick="checkExtension()">Submit</button>
                </form>
            </div>

<script>
            function setExampleValue(value) {
                document.getElementById("examples").value = value;
            }
</script>

字符串


的数据
如果单击#1,我想从后端加载example1.xlsx并显示example1.xlsx来代替文本No file chosen
在www.example.com中views.py,包含以下内容用于加载文件路径。filename,并设置文件路径。

def example_file(request):
    filename = request.session.get('filename')
    fpath = f"job_{filename}" / "example1.xlsx"


这是包含在urls.py

path('example/', example_file, name='example'),


关于如何加载此文件路径中存在的文件的建议将非常有帮助。

qvk1mo1f

qvk1mo1f1#

您可以使用 AJAX 动态加载文件。您需要为此创建一个视图。请使用此文档并根据您的需要进行调整。How to Implement Dependent/Chained Dropdown List with Django
另一种更简单的方法是创建一个REST API并在前端使用React。这将简化围绕任务的麻烦。

h9vpoimq

h9vpoimq2#

您可以将excel文件存储在静态目录中,然后通过上下文或静态模板标记在HTML上显示指向该目录的链接。
如果你想加载它们,你可以使用pyexcel加载条目,将它们作为字典添加到列表中,然后使用Model.objects.bulk_create(the_list_of_dicts)创建它们。

luaexgnf

luaexgnf3#

path('example/',example_file,name ='example')这是您的urls.py。
更改为:path('example/',views.example_file,name='example'),

相关问题