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 views.py or models.py, let me know.

{% 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 %}

1 comments

@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

18 Answered Questions

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

9 Answered Questions

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

38 Answered Questions

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

30 Answered Questions

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

24 Answered Questions

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

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

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
  • 1500965 View
  • 2109 Score
  • 28 Answer
  • Tags:   html css opacity

22 Answered Questions

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

  • 2008-09-16 09:08:52
  • Mr Shark
  • 399363 View
  • 1824 Score
  • 22 Answer
  • Tags:   html

25 Answered Questions

[SOLVED] Redirect from an HTML page

3 Answered Questions

[SOLVED] Cannot display HTML string

15 Answered Questions

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

Sponsored Content