Rendering another template from a button click in Django

∥☆過路亽.° 提交于 2019-12-08 13:31:06

问题


I have following template files in my django project:

  1. base.html
  2. base_test1.html
  3. 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?

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.


回答1:


  • Create another url, view for 2nd template which I think you have done.
  • Make button as link to this 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. But just some block in the html page, which you replace in the original page.



来源:https://stackoverflow.com/questions/16330615/rendering-another-template-from-a-button-click-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!