如何在Django模板页面中设置href
How to set href in Django template page
我是 Django 的新手。我的主页模板如下所示:
{% block content %}
{% for link in embededLinks %}
<div class="row justify-content-center">
<blockquote class="twitter-tweet">
<p lang="en" dir="ltr">Do you get the impression that the Supreme Court doesn’t like me?</p>
— Donald J. Trump (@realDonaldTrump)
<a href="{{%link%}}">June 18, 2020</a>
</blockquote>
<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
</div>
{% endfor %}
{% endblock %}
循环遍历 link 的列表,以便我的网站正确显示嵌入的推文。这个 html 将放在我的基本模板中。唯一的问题是我在这一行出错:
<a href="{{%link%}}">June 18, 2020</a>
href link 在另一个 python 文件中生成,类型为 str。唯一的问题是我不确定如何使用 Django 设置 href。
我对 Django 没有经验,但我对 Flask 很熟悉。
难道显示一个变量会是{{ link }}
而不是{{%link%}}
?
在 Django 中,您可以添加类似 href= "{% url 'url_name' %}" 的链接。
请记住,您必须为 url/path 命名才能像这样使用它。
例如:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
# let's say this index view renders home page and name is given 'index' to this urls(this name will be used for recalling urls paths and it can be anything).
]
Then your link looks like href = "{% url 'index' %}" . For more info check ,
https://docs.djangoproject.com/en/3.0/intro/tutorial03/
我是 Django 的新手。我的主页模板如下所示:
{% block content %}
{% for link in embededLinks %}
<div class="row justify-content-center">
<blockquote class="twitter-tweet">
<p lang="en" dir="ltr">Do you get the impression that the Supreme Court doesn’t like me?</p>
— Donald J. Trump (@realDonaldTrump)
<a href="{{%link%}}">June 18, 2020</a>
</blockquote>
<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
</div>
{% endfor %}
{% endblock %}
循环遍历 link 的列表,以便我的网站正确显示嵌入的推文。这个 html 将放在我的基本模板中。唯一的问题是我在这一行出错:
<a href="{{%link%}}">June 18, 2020</a>
href link 在另一个 python 文件中生成,类型为 str。唯一的问题是我不确定如何使用 Django 设置 href。
我对 Django 没有经验,但我对 Flask 很熟悉。
难道显示一个变量会是{{ link }}
而不是{{%link%}}
?
在 Django 中,您可以添加类似 href= "{% url 'url_name' %}" 的链接。 请记住,您必须为 url/path 命名才能像这样使用它。 例如:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
# let's say this index view renders home page and name is given 'index' to this urls(this name will be used for recalling urls paths and it can be anything).
]
Then your link looks like href = "{% url 'index' %}" . For more info check ,
https://docs.djangoproject.com/en/3.0/intro/tutorial03/