By jmd_10

2019-03-14 19:55:23 8 Comments

I am building a tool in django. It allows myself, and others, to make entries (imagine a journal) for various 'topics'. When the topic is clicked a page opens ('topic' page) that shows the entries. I am trying to limit the length of that text to 200 characters such that if someone wants to see the whole entry, they need to click on that entry to open it up to its specific page.

I have tried putting a maxlength="200" in a few different places. But nothing worked. The only thing I did that kind of worked was to create a 'textarea' with a maxlength="200" but that was visually odd and unappealing. I know there has to be some way to limit the length in the existing code, without creating a new area and putting the entry in there.

Below is the code for the 'topic.html' page. I am thinking that should be enough. If you need the or, let me know.

{% extends "AARs/base.html" %}

{% block header %}
  <h2>{{ topic }}</h2>
{% endblock header %}

{% block content %}
    <a href="{% url 'AARs:new_action' %}">Add new action</a>

  {% for action in actions %}
    <div class="panel panel-default">
      <div class="panel-heading">
          {{ action.date_added|date:'M d, Y H:i' }}
        <small>  <a href="{% url 'AARs:edit_action' %}">Read or Edit Action</a></small>
      <div class="panel-body">
          <input type="text" name="action" maxlength="10">
            {{ action.text|linebreaks }}
  {% empty %}
    No actions have been added yet.
  {% endfor %}

{% endblock content %}


@Endre Both 2019-03-14 20:22:33

You might be looking for the truncation filters in Django's template language:

  • truncatechars
  • truncatechars_html
  • truncatewords
  • truncatewords_html

As an example, quoting from the docs:

{{ value|truncatewords:2 }}

If value is "Joel is a slug", the output will be "Joel is …".

@jmd_10 2019-03-16 00:05:41

Awesome!! Thanks. Seems I was searching for an html solution to a django issue.

Related Questions

Sponsored Content

21 Answered Questions

[SOLVED] What are valid values for the id attribute in HTML?

  • 2008-09-16 09:08:52
  • Mr Shark
  • 406738 View
  • 1860 Score
  • 21 Answer
  • Tags:   html

30 Answered Questions

[SOLVED] Convert HTML + CSS to PDF with PHP?

26 Answered Questions

[SOLVED] Redirect from an HTML page

24 Answered Questions

[SOLVED] HTML 5: Is it <br>, <br/>, or <br />?

  • 2009-12-22 13:39:08
  • Eikern
  • 1260751 View
  • 1871 Score
  • 24 Answer
  • Tags:   html html5

9 Answered Questions

[SOLVED] Why does HTML think “chucknorris” is a color?

18 Answered Questions

[SOLVED] How do I modify the URL without reloading the page?

38 Answered Questions

[SOLVED] How do I vertically center text with CSS?

28 Answered Questions

[SOLVED] How do I give text or an image a transparent background using CSS?

  • 2009-04-30 09:00:02
  • Stijn Sanders
  • 1504584 View
  • 2138 Score
  • 28 Answer
  • Tags:   html css opacity

3 Answered Questions

[SOLVED] Cannot display HTML string

15 Answered Questions

[SOLVED] How do I reformat HTML code using Sublime Text 2?

Sponsored Content