Django:单击按钮即可加载另一个模板 [英] Django: Loading another template on click of a button

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

问题描述

我已经在一个django项目上工作了几周,只是在玩耍,这样我就可以掌握它.我有点困惑.我现在有一个名为"home.html"的模板.我想知道是否还有我需要将另一个名为"profile.html"的模板设置为home.html模板上的链接?我有一个按钮,单击该按钮后,应直接将我带到profile.html页面,但是在我对此主题进行研究时,我得到的答案参差不齐.有些人表示要创建视图并使用javascript加载html,而另一些人则表示仅在标签中写入第二页模板的路径会更容易.

I've been working on a django project for a few weeks now, just playing around so that I can get the hang of it. I am a little bit confused. I have a template now called "home.html". I was wondering if there is anyway for me to have another template called "profile.html" to be set as a link on the home.html template? I have a button that when clicked, should take me directly to the profile.html page, but as I was doing research on this topic, I was getting mixed answers. Some people stated to create a view and have that load the html using javascript, while others stated that just writing the path to the 2nd page template in tags would be easier.

完成此操作的最正确方法是什么?

What is the most correct way of getting this done?

我查看了以下链接:从在Django中单击按钮试图获得更好的理解,因为这是与我所问的问题最接近的另一个问题,但由于django lingo令我难以理解,这让我更加困惑初学者.

I looked at this link: Rendering another template from a button click in Django to try to get a better understanding, as it was the one other question that was closest to what I was asking, but it confused me even more as the django lingo is somewhat confusing for me to understand as a beginner.

在此先感谢您提供的帮助.

Thank you in advance for any help you can provide.

编辑:总结:我目前在主页上.有一个显示查看您的个人资料"的按钮.单击该按钮后,我要离开主页并在屏幕上加载新页面"profile.html".

推荐答案

最正确的方法"是创建将加载"profile.html"模板的视图.我建议使用通用TemplateView.

"Most correct way" is to create view which will load your "profile.html" template. I suggest to use generic TemplateView.

将此行添加到您的 urls.py :

from django.views.generic import TemplateView

urlpatterns = patterns('',
    url(r'^profile/', TemplateView.as_view(template_name='profile.html'),
                      name='profile'),
)

并在 home.html 中使用以下代码段:

And in home.html use this snippet:

<a href="{% url 'profile' %}">Profile</a>

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

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