在Django中链接页面的最佳方法 [英] Best way of linking to a page in Django

查看:299
本文介绍了在Django中链接页面的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法为索引创建了URL标签。但是,现在我很困惑如何添加到其他页面的链接。

I managed to create a URL tag for my index. But right now I'm confused how to add links to other pages.

我将其放在我的 urls.py

url(r'^$', 'index', name='index'),

接下来,我将此标签放入href中:

The next thing I put this tag into the href:

{% url 'index' %}

但是如果我想创建一个新页面并将链接到它。我该怎么做才是最好的方法?

But what if I wanted to create a new page and would be linking to it. How would I do it the best way?

推荐答案

所以接下来您将扩展您的 urls.py 看起来像这样:

So next you would extend your urls.py to look something like this:

url(r'^$', 'index', name='index'),
url(r'^blog$', 'blog', name='blog'),

然后在您的html中可以使用以下任意一个:

Then in your html you can use either one:

<a href="{% url 'index' %}">Home</a>
<a href="{% url 'blog' %}">Blog</a>

您当然可以使用模板年龄 {%url'index'% } 在任何模板中需要的次数。

You can of course use the template tage {% url 'index' %} as many times as you need in any template.

这篇关于在Django中链接页面的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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