从Django中的按钮点击渲染另一个模板 [英] Rendering another template from a button click in Django

查看:415
本文介绍了从Django中的按钮点击渲染另一个模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的django项目中有以下模板文件:

I have following template files in my django project:


  1. base.html

  2. base_test1 .html

  3. base_test2.html

这两个 base_test1.html base_test2.html extends base.html 。我的主页呈现 base_test1.html 。在我的主页上,我有一个按钮,按下时应该呈现 base_test2.html 。我应该如何实现这一点?

Here both base_test1.html and base_test2.html extends base.html. My home page renders base_test1.html. In my home page, I have a button which when pressed should render base_test2.html. How should I achieve this?

我一直在尝试使用jQuery ajax调用,所以当按下按钮时,我调用相应的视图在 views.py 。但是,这似乎没有工作,因为ajax调用只是将html页面返回给请求,而不是渲染它。

I have been trying to use jQuery ajax call so that when the button is pressed, I call the corresponding view in the views.py of my application. But that doesn't seem to be working since the ajax call just returns the html page to the request rather than rendering it.

推荐答案


  • 创建另一个网址,查看我认为您已经完成的第二个模板。 >
  • 将按钮设为链接到此网址。所以当按下按钮时,会渲染具有第二个模板的页面。

  • 如果要使用ajax, base_test2.html 不应该返回完整的HTML页面。但是在html页面中只是一些块,您可以在原始页面中替换。

    If you want to use ajax, the base_test2.html should not return complete HTML page. But just some block in the html page, which you replace in the original page.

    这篇关于从Django中的按钮点击渲染另一个模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆