Python 如何在Django模板页面中设置href
我对Django相当陌生。我的主页模板如下所示:Python 如何在Django模板页面中设置href,python,django,web-applications,Python,Django,Web Applications,我对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 tha
{% 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 %}
{%block content%}
{%for EmbeddedLinks%中的链接}
你觉得最高法院不喜欢我吗
&mdash;唐纳德·J·特朗普(@realDonaldTrump)
{%endfor%}
{%endblock%}
循环通过我的网站的链接列表来正确显示嵌入的推文。这个html将被放在一个基本模板,我有。唯一的问题是我在这一行得到一个错误:
<a href="{{%link%}}">June 18, 2020</a>
href链接是在另一个python文件中生成的,类型为str。唯一的问题是我不确定如何使用Django设置href 我对Django没有经验,但我对Flask有经验
是否显示一个变量将是
{{link}
而不是{{%link%}
?我对Django没有经验,但我对Flask有经验
是否显示变量将是
{{link}
而不是{{%link%}
?在Django中,您可以添加href=“{%url'url\u name%}”等链接。
请记住,要像这样使用url/路径,必须为其命名。
例:
在Django中,您可以添加诸如href=“{%url”url\u name“%”之类的链接。 请记住,要像这样使用url/路径,必须为其命名。 例:
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/