由于{%for%}循环,重复了我在HTML中的文本。我已经尝试将{%if%}移出{%for%}。然后我消失了。
这是我的html模板
{% for city in cities %}
{% if city.author.access_challenge %}
<p class="small text-center"> In order to get information about this contact us through almaz@protonmail.com </p>
{% else %}
<table class="table table-hover text-left col-sm-12" style="table-layout: fixed; word-wrap: break-word;">
<tbody>
<tr>
<td><a class="text-uppercase" href="{% url 'users:address' city.pk %}">{{ city.city }}</a></td>
</tr>
</tbody>
</table>
{% endif %}
{% endfor %}
这是我的views.py
def cities(request, pk):
country = Post.objects.get(id=pk).country
cities = Post.objects.filter(country=country).distinct('city')
context = {
'cities':cities,
'country':country
}
return render(request, 'users/cities.html', context)
我也试图这样更改views.py:
def cities(request, pk):
country = Post.objects.get(id=pk).country
ci = Post.objects.filter(country=country).distinct('city')
cit = list(ci)
for city in cit:
for cities in cit:
context = {
'cities':cities,
'country':country
}
return render(request, 'users/cities.html', context)
但是当我使用for循环时,我收到一个错误,指出Post是不可迭代的。
答案 0 :(得分:0)
在您的代码中尝试forloop.first
。例如:
输入:
{% for product in products %}
{% if forloop.first == True %}
First time through!
{% else %}
Not the first time.
{% endif %}
{% endfor %}
输出:
First time through!
Not the first time.
Not the first time.
针对您的情况:
{% for city in cities %}
{% if city.author.access_challenge and forloop.first == True %}
<p class="small text-center"> In order to get information about this contact us through almaz@protonmail.com </p>
{% else %}
<table class="table table-hover text-left col-sm-12" style="table-layout: fixed; word-wrap: break-word;">
<tbody>
<tr>
<td><a class="text-uppercase" href="{% url 'users:address' city.pk %}">{{ city.city }}</a></td>
</tr>
</tbody>
</table>
{% endif %}
{% endfor %}
答案 1 :(得分:0)
完成时
cities = Post.objects.filter(country=country).distinct('city')
您尝试获取的是所有不同城市的列表,但是您获取的是Post对象的查询集。
要获取发布模型中所有不同城市的列表,请执行以下操作:
Post.objects.all().values_list('city').distinct()
答案 2 :(得分:0)
我这样解决了:
我的views.py现在:
def cities(request, pk):
country = Post.objects.get(id=pk).country
cities = Post.objects.filter(country=country).distinct('city')
author = Post.objects.get(id=pk).author
context = {
'cities':cities,
'country':country,
'author':author,
}
return render(request, 'users/cities.html', context)
我的views.py曾经是:
def cities(request, pk):
country = Post.objects.get(id=pk).country
cities = Post.objects.filter(country=country).distinct('city')
context = {
'cities':cities,
'country':country
}
return render(request, 'users/cities.html', context)
在使用相同代码之前,我的HTML有错误。但是现在它可以正常工作了:
{% if author.access_challenge %}
<p class="text-center col-sm-12"> In order to get info about this country contact <strong>emeupci@protonmail.com</strong></p>
{% else %}
{% for city in cities %}
<table class="table table-hover text-left col-sm-12" style="table-layout: fixed; word-wrap: break-word;">
<tbody>
<tr>
<td><a class="text-uppercase" href="{% url 'users:address' city.pk %}">{{ city.city }}</a></td>
</tr>
</tbody>
</table>
{% endfor %}
{% endif %}