我正在用Django构建工具。它使我自己和其他人可以为各种“主题”输入条目(想象日记)。单击主题后,将打开一个显示条目的页面(“主题”页面)。我试图将文本的长度限制为200个字符,这样,如果有人想查看整个条目,则需要单击该条目以将其打开到特定页面。
我尝试将maxlength =“ 200”放在几个不同的地方。但是没有任何效果。我做这种工作的唯一一件事就是创建一个maxlength =“ 200”的'textarea',但这在视觉上是奇怪的且没有吸引力。我知道必须有某种方法来限制现有代码的长度,而无需创建新区域并将条目放置在那里。
下面是“ topic.html”页面的代码。我认为这应该足够了。如果您需要views.py或models.py,请告诉我。
{% extends "AARs/base.html" %}
{% block header %}
<h2>{{ topic }}</h2>
{% endblock header %}
{% block content %}
<p>
<a href="{% url 'AARs:new_action' topic.id %}">Add new action</a>
</p>
{% for action in actions %}
<div class="panel panel-default">
<div class="panel-heading">
<small>
{{ action.date_added|date:'M d, Y H:i' }}
</small>
<small> <a href="{% url 'AARs:edit_action' action.id %}">Read or Edit Action</a></small>
</div>
<div class="panel-body">
<small>
<input type="text" name="action" maxlength="10">
{{ action.text|linebreaks }}
</small>
</div>
</div>
{% empty %}
No actions have been added yet.
{% endfor %}
{% endblock content %}
答案 0 :(得分:0)
您可能正在寻找Django模板语言中的truncation filters:
例如,引用文档中的内容:
{{ value|truncatewords:2 }}
如果value
是“乔尔是个ug”,则输出将是“乔尔是…”。