模板变量在django中重定向后不更新吗?

发布于 2025-02-11 18:03:23 字数 1884 浏览 2 评论 0原文

我正在构建简单的Django应用程序,当用户单击前端按钮时,我想在其中进行一些解析。

我有模板变量{{{parsing}},我正在使用内部index.html进行禁用按钮以进行解析以进行解析时,当用户单击下一个

<div class="btn-group mr-2" role="group" aria-label="Parsing group">
  <button class="btn btn-dark btn-lg" id="parseButton" {% if parsing %} disabled {% endif %}>
    <i class="fa fa-terminal gradient-text"></i>
      <span class="gradient-text">| Parse</span>
  </button>
</div>

我要做的是jQuery方法,它将ajax请求发送到我的后端,以初始化从视图解析和方法的变量来初始化变量。 .py返回重定向到同一页面(index.html)。

    $('#parseButton').click(function () {
        $.ajax({
            type: 'POST',
            url: 'initialize_parsing/',
            headers: {"X-CSRFToken": $.cookie("csrftoken")},
            data: {}
        });

然后我的views.py:

def initialize_before_parsing(request):
    if request.method == 'POST':
        frontendTracker = FrontendTracker()
        frontendTracker.progress = 0
        frontendTracker.parsing = True

    return redirect("index")


class IndexView(TemplateView):
    template_name = 'index.html'

    def get_context_data(self, **kwargs):
        frontendTracker = FrontendTracker()
        context = super(IndexView, self).get_context_data(**kwargs)
        context["showAnnotation"] = frontendTracker.showAnnotationButton
        context["parsing"] = frontendTracker.parsing
        context["progress"] = frontendTracker.progress

        return context

和urls.py

urlpatterns = [
    path('', IndexView.as_view(), name="index"),
    path("initialize_parsing/", initialize_before_parsing, name="initialize_before_parsing"),
]

终于困扰着我的是,当我发送ajax请求时,当我的页面重定向{{{progres}} template变量时,一切正常,或者在我刷新之前没有更改或其他任何其他变量。 当Ajax成为成功时,请与JS进行刷新,这不是我想做的事情,因为在AJAX请求之后我想执行的JS中还有其他一些方法,并且可以重置我的JS代码。 没有刷新的情况下,如何实现解析变量的价值?

I am building simple django app where I want to do some parsing when user click button on the frontend.

I have template variable {{ parsing }} which I am using inside index.html to disable button for parsing when user click on it

<div class="btn-group mr-2" role="group" aria-label="Parsing group">
  <button class="btn btn-dark btn-lg" id="parseButton" {% if parsing %} disabled {% endif %}>
    <i class="fa fa-terminal gradient-text"></i>
      <span class="gradient-text">| Parse</span>
  </button>
</div>

Next what I do is JQuery method which sends ajax request to my backend to initialize variables for parsing and method from views.py returns redirect to the same page (index.html).

    $('#parseButton').click(function () {
        $.ajax({
            type: 'POST',
            url: 'initialize_parsing/',
            headers: {"X-CSRFToken": $.cookie("csrftoken")},
            data: {}
        });

Then my views.py:

def initialize_before_parsing(request):
    if request.method == 'POST':
        frontendTracker = FrontendTracker()
        frontendTracker.progress = 0
        frontendTracker.parsing = True

    return redirect("index")


class IndexView(TemplateView):
    template_name = 'index.html'

    def get_context_data(self, **kwargs):
        frontendTracker = FrontendTracker()
        context = super(IndexView, self).get_context_data(**kwargs)
        context["showAnnotation"] = frontendTracker.showAnnotationButton
        context["parsing"] = frontendTracker.parsing
        context["progress"] = frontendTracker.progress

        return context

and urls.py

urlpatterns = [
    path('', IndexView.as_view(), name="index"),
    path("initialize_parsing/", initialize_before_parsing, name="initialize_before_parsing"),
]

Finally what is bother me is that when I send that ajax request and everything works fine when my page being redirected {{progres}} template variable isn't changed or any other until I do refresh.
Doing refresh with js when ajax is success isn't something which I want to do because I have some other methods inside js which I want to execute after that ajax request and that will reset my js code.
How can achieve to change value of parsing variable without refresh?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

峩卟喜欢 2025-02-18 18:03:23

您在2个位置有状态:在浏览器和服务器中。

当您从服务器加载页面时,它将在服务器上解析状态并相应地填写模板。当您将Ajax调用时,您将状态传递到服务器,但不要对浏览器中页面状态做任何事情。在随后的重新加载下,您会看到浏览器中的反射变化,因为您再次通过模板通过该状态。

我对frontendTracker或您的特定问题不太了解,所以我不知道最好的解决方案是什么。一种解决方案是添加.done(function(data){/ *您的代码 */})处理程序 $。$。ajax 呼叫,然后操纵页面的内容为了反映您希望该页面更改的方式。但是,似乎您有某种progress值,因此您可能需要进行随后的AJAX调用以从服务器中获取更新以获取其更新值。

You have state in 2 places: in the browser and on the server.

When you load a page from the server then it parses the state on the server and fills in the template accordingly. When you make the AJAX call you pass state to the server, but don't do anything to the state of the page in the browser. On a subsequent reload you see the reflected change in the browser because you passed that state through the template again.

I don't know too much about FrontendTracker or your specific problem, so I don't know what the best solution is. One solution is add a .done(function( data ) { /* your code */ }) handler to the $.ajax call and then manipulate the content of the page to reflect how you want the page to change. However, it seems like you have some sort of progress value, so you likely need to make subsequent AJAX calls to get updates from the server for updated values of that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文