[英]Passing variable to href django template
I have some problem and maybe I can give an example of two views below what I want to achieve.我有一些问题,也许我可以在下面给出我想要实现的两个视图的示例。
class SomeViewOne(TemplateView):
model = None
template_name = 'app/template1.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
# The downloads view contains a list of countries eg France, Poland, Germany
# This returns to context and lists these countries in template1
class ItemDetail(TemplateView):
model = None
template_name = 'app/template2.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
countries_name = kwargs.get('str')
The view should get the passed "x" with the name of the country where I described it
below.
Then on the page I have a list of these countries.然后在页面上我有这些国家的列表。 After clicking on the selected country, a new tab should open and show a list of cities in the selected country.
单击所选国家/地区后,应打开一个新选项卡并显示所选国家/地区的城市列表。
So I am using in loop template1.html as below所以我在循环中使用 template1.html 如下
{% for x in list_countries %}
<li>
<a href="{% url 'some-name-url' '{{x}}' %}" class="target='_blank'">{{ x }}</a><br>
</li>
{% endfor %}
I can't pass "x" this way.我不能这样传递“x”。 Why?
为什么?
The url for the next view looks like this下一个视图的 url 如下所示
path('some/countries/<str:x>/',views.ItemDetail.as_view(), name='some-name-url'),
And I can't get that 'x' given in the template in the href而且我无法在 href 的模板中给出“x”
If Manoj's solution doesn't work, try removing the single quotes AND {{ }}.如果 Manoj 的解决方案不起作用,请尝试删除单引号和 {{ }}。 In my program, my integer doesnt need to be wrapped with {{ }}, so maybe neither does your string.
在我的程序中,我的 integer 不需要用 {{ }} 包裹,所以您的字符串可能也不需要。 I have this in my code:
我的代码中有这个:
{% for item in items %}
<div class="item-title">
{{ item }}<br>
</div>
<a href="{% url 'core:edit_item' item.id %}">Edit {{ item }}</a>
{% endfor %}
It works just fine.它工作得很好。 Try:
尝试:
<a href="{% url 'some-name-url' x %}" class="target='_blank'">{{ x }}</a>
There are several mistakes such as:有几个错误,例如:
It should be only x
in url tag neither {{x}}
nor '{{x}}'
它应该只是 url 标签中的
x
,既不是{{x}}
也不是'{{x}}'
you have passed the value as x
in url params ( some/countries/<str:x>/
) and accessing it using kwargs.get('str')
which is not correct it should be kwargs.get('x')
.您已在 url 参数(
some/countries/<str:x>/
)中将值作为x
传递并使用kwargs.get('str')
访问它,这是不正确的,它应该是kwargs.get('x')
。
Also you are not including variable countries_name
in context and not even returning context.此外,您没有在上下文中包含变量
countries_name
,甚至没有返回上下文。
Note: Assuming that you are already getting some companies in
template1.html
template that's why you are running loop.注意:假设您已经在
template1.html
模板中获得一些公司,这就是您运行循环的原因。
Try below code:试试下面的代码:
views.py视图.py
class ItemDetail(TemplateView):
template_name = 'template2.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['countries_name'] = self.kwargs.get('x')
return context
Template1.html file Template1.html文件
{% for x in list_countries %}
<li>
<a onclick="window.open('{% url 'some-name-url' x %}', '_blank')" style='cursor:pointer;'>{{ x }}</a><br>
</li>
{% endfor %}
Then you can this countries_name
value passed from template1.html
in template2.html.然后你可以在模板2.html中从
template1.html
1.html传递的这个countries_name
值。
template2.html模板2.html
<p>The clicked country is {{countries_name}}</p>
You don't need pass that variable with single quotes.您不需要用单引号传递该变量。
<a href="{% url 'some-name-url' {{ x }} %}" #Just removed single quotes from variable x.
And see if it shows on template看看它是否显示在模板上
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.