我的网页中有一个标题,该标题中有一个 Logo 。我想在该 Logo 中插入一个 anchor 标记,该标记指向我的主页,即 index.html 页面,尽管调用了哪个 View 或页面。我创建了一个 base.html,其中的一个片段是:
<div class="header">
<div class="inner_header">
<div class="logo">
<a href='#'><img src="{% static 'images/logo.png' %}" alt="logo" /> </a>
</div>
我已经将这个 base.html 扩展到我所有的其他模板文件
我指向我的索引页面和其他页面的 url 模式是这样的:
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^category/(?P<category_name_slug>[\w\-]+)/$', views.category, name='category'),
url(r'^latest/(?P<latest_title_slug>[\w\-]+)/$', views.latest, name='latest'),
url(r'^headline/(?P<heading_title_slug>[\w\-]+)/$', views.headline, name='headline'),)
我试过使用 <a href="index.html">,<a href=''>
以及其他一些无法产生预期结果或出错的东西。在不编写大量代码的情况下执行此操作的合适方法是什么?
最佳答案
Rohit Jain 关于使用 url
的建议模板标签正确
<a href="{% url 'index' %}">
但这可能有助于了解您当前的方法在做什么
-
<a href='#'>
- 这是试图链接到您页面上的 id 元素,但由于您没有提供 id 名称,它仅引用页面本身。它不会重定向到任何地方 -
<a href="index.html">
- 您正在尝试直接引用模板,您没有将此 url 映射到随后将显示该模板的 View 的 urlpattern -
<a href=''>
- 这与使用#
几乎相同因为您没有更改要附加到的链接
您可能会在 <a href="/">
上取得一些成功但同样,这不是在 django 中执行此操作的正确方法,因为您将来可能会决定更改与特定 View 匹配的 url 模式(即您决定添加 i18n 模式),因此您应该始终 尝试使用 url
模板标签。
关于python - django 模板 anchor 标记不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34306886/