django 模板锚标记不起作用
django template anchor tag not working
我的网页中有一个 header,那个 header 中有一个徽标。我想在该徽标中插入一个锚标记,该标记指向我的主页 index.html 页面,尽管调用了哪个视图或页面。我创建了一个 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='#'>
- 这试图 link 到您页面上的一个 id 元素,但由于您没有提供 id 名称,它只引用页面本身。它不会重定向到任何地方
<a href="index.html">
- 您试图直接引用模板,您没有 url 模式将此 url 映射到随后显示的视图那个模板
<a href=''>
- 这与使用 #
几乎相同,因为您没有更改要附加到 的 link
你可能在 <a href="/">
上取得了一些成功,但同样,这不是在 django 中执行此操作的正确方法,因为你可能会在将来决定更改 url 模式匹配的内容一个特定的视图(即你决定添加 i18n 模式)所以你应该 总是 尝试使用 url
模板标签。
我的网页中有一个 header,那个 header 中有一个徽标。我想在该徽标中插入一个锚标记,该标记指向我的主页 index.html 页面,尽管调用了哪个视图或页面。我创建了一个 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='#'>
- 这试图 link 到您页面上的一个 id 元素,但由于您没有提供 id 名称,它只引用页面本身。它不会重定向到任何地方<a href="index.html">
- 您试图直接引用模板,您没有 url 模式将此 url 映射到随后显示的视图那个模板<a href=''>
- 这与使用#
几乎相同,因为您没有更改要附加到 的 link
你可能在 <a href="/">
上取得了一些成功,但同样,这不是在 django 中执行此操作的正确方法,因为你可能会在将来决定更改 url 模式匹配的内容一个特定的视图(即你决定添加 i18n 模式)所以你应该 总是 尝试使用 url
模板标签。