【问题标题】:Update value of a template tag from views in django从 django 中的视图更新模板标签的值
【发布时间】:2020-02-21 02:10:17
【问题描述】:

我在 Django 中有一个表单来提供有关特定日期的各种任务的信息,这些任务被保存为模型对象。我在 HTML 页面上呈现任务,如下所示在网格视图中。 (Click Here for the Grid View) 但是现在我希望能够仅在特定的那一天显示任务。使用右上角的箭头,我希望能够过滤任务并显示它们。

我已经从日历的 javascript 中提取了当前日期,并通过我的 views.py 中的 ajax 调用发送它。从那里我根据日期过滤任务,但是当我更改视图(即,左右角箭头的日期)时,我能够捕获更新的日期但无法更新模板标签JS端。

views.py

# The default view when the page loads for the first time
# As it loads by default to the current date, so filtering tasks on the current date.
def schedule(request):

    today = datetime.datetime.today()
    tasks = Tasks.objects.filter(schedule_date=today.strftime("%Y-%m-%d"))

    context = {
        "tasks": processes,
    }

    return render(request, r'dhtmlx_grid_view.html',context)
.
.
.
.
#Calling this function when there is a view change (ie., when you navigate to a new date)
@csrf_exempt
def load_tasks(request):
    if request.method == "POST":
        view_date = request.POST.get('new_date')
        date = datetime.datetime.strptime(view_date, '%b %d %Y')

        tasks= Tasks.objects.filter(schedule_date= date.strftime("%Y-%m-%d"))

        context = {
            'tasks': task
        }
        return render(request, r'dhtmlx_grid_view.html',context)

dhtmlx_grid_view.html(在 HTML 文件的 JS 部分)

{# Using ajax post call to send the changed date #}
scheduler.attachEvent("onViewChange", function (new_mode, new_date) {

            $.ajax({
                url: {% url 'tasks:load-tasks' %},
                type: "POST",
                data: {
                    'new_date': String(new_date).substring(4,15),
                    'new_mode': new_mode,

                },
                success: function (result) {
                    alert("Successful")
                }
            });
        })
.
.
.
.

{# Using the template tags as follows to render the variable #}
        var tasks = [

            {% for i in tasks %}
                {
                    key: "{{ i.grouper }}", label: "{{ i.grouper }}", open: true, children: [
                            {% for j in i.list %}
                                {key: {{ j.sub_process_id }}, label: "{{ j.sub_process }}"},
                                {% endfor %}
                    ]
                },
            {% endfor %}
        ];

如何从load-tasks方法更改模板端tasks的值?

有没有办法做到这一点,因为即使我存储了更改的日期和过滤器,我也无法在模板端更新<QuerySet>tasks 的值。

如果您需要任何其他信息,请告诉我

【问题讨论】:

    标签: javascript python django django-models django-templates


    【解决方案1】:

    模板中加载的标签仅在最初生成 HTML 文件时进行评估。这些标签将不再适用于未来的 AJAX 请求。您需要在 ajax 调用的回调中更改 DOM。

    scheduler.attachEvent("onViewChange", function (new_mode, new_date) {
    
                $.ajax({
                    url: {% url 'tasks:load-tasks' %},
                    type: "POST",
                    data: {
                        'new_date': String(new_date).substring(4,15),
                        'new_mode': new_mode,
    
                    },
                    success: function (result) {
                        // Set the new DOM up in here
                    }
                });
            })
    

    【讨论】:

    • 在成功函数中,如何更新现有模板标签{{ tasks }}的值??
    • 您需要使用 JavaScript 来执行此操作,因为这是在浏览器中运行的编程语言。例如,你可以写document.getElementById('tasksElement').innerHTML = result; 之类的东西(不完全是这样,但希望你明白这一点)。
    • 是的,我明白,但在我的情况下 {{ tasks }} 是一个带有模型对象的 并使用 Django 模板进行循环,我将它们呈现在 dhtmlx 网格视图上,没有这样的特定 Html我给它的组件。如果不是这样,我可以在 javascript 端进行过滤吗?通过 {{ Tasks.filter(date==variable) }} 并渲染这个对象,如果我这样做,我如何在模板标签 {{ ''''' }} 中传递一个变量?
    猜你喜欢
    • 1970-01-01
    • 2015-02-02
    • 1970-01-01
    • 2011-07-28
    • 2017-05-15
    • 1970-01-01
    • 1970-01-01
    • 2014-07-25
    • 2017-02-16
    相关资源
    最近更新 更多