gpt4 book ai didi

python - 无效的 block 标记 : 'keywords_for_order' , 预期 'endblock'

转载 作者:太空宇宙 更新时间:2023-11-04 06:16:41 25 4
gpt4 key购买 nike

抱歉,我已经无计可施了。我有这个代码在本地工作,但是由于某种原因在服务器上当我尝试运行这个标签时我得到了一个错误,我不知道为什么。这是模板代码:

{% load blog_tags keyword_tags i18n future %}

{% block blog_keywords %}
{% keywords_for_order blog.blogpost as tags %}
{% if tags %}
<h3>{% trans "Tags" %}</h3>
<ul class="unstyled tags">
{% for tag in tags %}
<li>
<a href="{% url "blog_post_list_tag" tag.slug %}"
class="tag-weight-1">{{ tag }}</a>
({{ tag.item_count }})
</li>
{% endfor %}
</ul>
{% endif %}
{% endblock %}

这是 keyword_tags.py 文件:

from django.contrib.contenttypes.models import ContentType
from django.db.models import Model, Count

from mezzanine import template
from mezzanine.conf import settings
from mezzanine.generic.fields import KeywordsField
from mezzanine.generic.models import AssignedKeyword, Keyword


register = template.Library()

@register.as_tag
def keywords_for(*args):
"""
Return a list of ``Keyword`` objects for the given model instance
or a model class. In the case of a model class, retrieve all
keywords for all instances of the model and apply a ``weight``
attribute that can be used to create a tag cloud.
"""

# Handle a model instance.
if isinstance(args[0], Model):
obj = args[0]
if hasattr(obj, "get_content_model"):
obj = obj.get_content_model() or obj
# There can only be one ``KeywordsField``, find it.
for field in obj._meta.many_to_many:
if isinstance(field, KeywordsField):
break
else:
return []
keywords_manager = getattr(obj, field.name)
return [a.keyword for a in keywords_manager.select_related("keyword")]

# Handle a model class.
try:
app_label, model = args[0].split(".", 1)
except ValueError:
return []

content_type = ContentType.objects.get(app_label=app_label, model=model)
assigned = AssignedKeyword.objects.filter(content_type=content_type)
keywords = Keyword.objects.filter(assignments__in=assigned)
keywords = keywords.annotate(item_count=Count("assignments"))
if not keywords:
return []
settings.use_editable()
counts = [keyword.item_count for keyword in keywords]
min_count, max_count = min(counts), max(counts)
sizes = settings.TAG_CLOUD_SIZES
step = (max_count - min_count) / (sizes - 1)
if step == 0:
steps = [sizes / 2]
else:
steps = range(min_count, max_count, step)[:sizes]
for keyword in keywords:
c = keyword.item_count
diff = min([(abs(s - c), (s - c)) for s in steps])[1]
keyword.weight = steps.index(c + diff) + 1
return keywords



@register.as_tag
def keywords_for_order(*args):


# Handle a model instance.
if isinstance(args[0], Model):
obj = args[0]
if hasattr(obj, "get_content_model"):
obj = obj.get_content_model() or obj
# There can only be one ``KeywordsField``, find it.
for field in obj._meta.many_to_many:
if isinstance(field, KeywordsField):
break
else:
return []
keywords_manager = getattr(obj, field.name)
return [a.keyword for a in keywords_manager.select_related("keyword")]

# Handle a model class.
try:
app_label, model = args[0].split(".", 1)
except ValueError:
return []

content_type = ContentType.objects.get(app_label=app_label, model=model)
assigned = AssignedKeyword.objects.filter(content_type=content_type)
keywords = Keyword.objects.filter(assignments__in=assigned)
keywords = keywords.annotate(item_count=Count("assignments")).order_by('-item_count')[:2]
if not keywords:
return []

return keywords

这是我得到的错误:

Error during template rendering

In template /home/USERNAME/webapps/lion_dev/lion/roar/templates/blog/includes/tagcloud_panel.html, error at line 4
Invalid block tag: 'keywords_for_order', expected 'endblock'

1 {% load blog_tags keyword_tags i18n future %}
2
3 {% block blog_keywords %}
4 {% keywords_for_order blog.blogpost as tags %}
5 {% if tags %}
6 <h3>{% trans "Tags" %}</h3>
7 <ul class="unstyled tags">
8 {% for tag in tags %}
9 <li>
10 <a href="{% url "blog_post_list_tag" tag.slug %}"
11 class="tag-weight-1">{{ tag }}</a>
12 ({{ tag.item_count }})
13 </li>
14 {% endfor %}

是的,我不知道为什么看不到这个模板标签文件。它也有一个 init.py 文件,所以不是那个。最好,感谢您的帮助,

jack

最佳答案

您是否直接在 Mezzanine 中编辑 keyword_tags.py 文件?

你不应该 - 所以如果你做了正确的事情并以某种方式将其复制到你的项目或应用程序中,那么加载的可能是夹层版本,而不是你的版本。尝试给它一个不同的名字,这样它们就不会发生冲突。

关于python - 无效的 block 标记 : 'keywords_for_order' , 预期 'endblock',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15121266/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com