简体   繁体   English

通过按钮渲染另一个模板,在Django中单击

[英]Rendering another template from a button click in Django

I have following template files in my django project: 我的Django项目中有以下模板文件:

  1. base.html base.html
  2. base_test1.html base_test1.html
  3. base_test2.html base_test2.html

Here both base_test1.html and base_test2.html extends base.html . 在这里, base_test1.htmlbase_test2.html 扩展了 base.html My home page renders base_test1.html . 我的主页呈现了base_test1.html In my home page, I have a button which when pressed should render base_test2.html . 在我的主页中,我有一个按钮,当按下该按钮时,应该呈现base_test2.html How should I achieve this? 我应该如何实现呢?

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. 我一直在尝试使用jQuery ajax调用,以便在按下按钮时,可以在应用程序的views.py中调用相应的视图。 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调用只是将html页面返回给请求,而不是呈现它。

  • Create another url, view for 2nd template which I think you have done. 创建另一个URL,查看第二模板,我认为您已经完成了。
  • Make button as link to this url. 将按钮设为此URL的链接。 So that when the button is pressed, page with 2nd template is rendered. 这样,当按下按钮时,将呈现带有第二个模板的页面。

If you want to use ajax, the base_test2.html should not return complete HTML page. 如果要使用ajax,则base_test2.html不应返回完整的HTML页面。 But just some block in the html page, which you replace in the original page. 但是html页面中只有一些块,您可以在原始页面中替换掉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM